Skip to content

Instantly share code, notes, and snippets.

@yuk1ty
Created November 25, 2017 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuk1ty/a0636d82ed50afeba2a6dbcef6d20163 to your computer and use it in GitHub Desktop.
Save yuk1ty/a0636d82ed50afeba2a6dbcef6d20163 to your computer and use it in GitHub Desktop.
Request を作ってみたサンプル
pub struct Request {
head: Parts,
}
pub struct Parts {
pub method: String,
pub uri: String,
pub http_version: String,
}
impl Request {
pub fn builder() -> Builder {
Builder::new()
}
}
pub struct Builder {
head: Option<Parts>,
}
impl Builder {
pub fn new() -> Builder {
Builder::default()
}
pub fn method(&mut self, method: String) -> &mut Builder {
if let Some(parts) = head(&mut self.head) {
parts.method = method
}
self
}
pub fn uri(&mut self, uri: String) -> &mut Builder {
if let Some(parts) = head(&mut self.head) {
parts.uri = uri
}
self
}
pub fn http_version(&mut self, http_version: String) -> &mut Builder {
if let Some(parts) = head(&mut self.head) {
parts.http_version = http_version
}
self
}
pub fn build(&mut self) -> Request {
Request {
head: self.take_parts(),
}
}
fn take_parts(&mut self) -> Parts {
self.head.take().unwrap()
}
}
impl Default for Builder {
fn default() -> Builder {
Builder {
head: None
}
}
}
fn head<'a>(head: &'a mut Option<Parts>) -> Option<&'a mut Parts> {
head.as_mut()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment