Created
November 25, 2017 14:53
-
-
Save yuk1ty/a0636d82ed50afeba2a6dbcef6d20163 to your computer and use it in GitHub Desktop.
Request を作ってみたサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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