Builder Patterns
Rust Programming Best Practices
impl RequestBuilder {
pub fn new() -> Self {...}
pub fn url(mut Self, url: &str) -> Self {...}
pub fn method(mut Self, method: Method) -> Self {...}
pub fn body(mut Self, body: &str) -> Self {...}
...
pub fn build(self) -> Request {...}
}
impl Request {
// personal preference
// (allow caller to not have to import ...Builder type)
pub fn builder() -> RequestBuilder { RequestBuilder::new() }
}
pub struct NoUrl;
pub struct Url(String);
pub struct RequestBuilder<U> {...}
impl<U> RequestBuilder<U> {...}
impl RequestBuilder<NoUrl> {
pub fn new() -> Self {...}
}
impl RequestBuilder<Url> {
pub fn build(self) -> Result<Request> {...}
}