File: complex_generics.rs

package info (click to toggle)
rust-buildstructor 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 532 kB
  • sloc: makefile: 2
file content (46 lines) | stat: -rw-r--r-- 1,196 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use buildstructor::buildstructor;
use http::header::HeaderName;
use http::HeaderValue;
use std::error::Error;
#[derive(Debug)]
pub struct Request<T> {
    inner: http::Request<T>,
}

#[buildstructor]
impl<T> Request<T> {
    #[builder]
    pub fn fake_new<K, V>(
        headers: Vec<(K, V)>,
        uri: Option<http::Uri>,
        method: Option<http::Method>,
        body: T,
    ) -> http::Result<Request<T>>
    where
        HeaderName: TryFrom<K>,
        <HeaderName as TryFrom<K>>::Error: Into<http::Error>,
        HeaderValue: TryFrom<V>,
        <HeaderValue as TryFrom<V>>::Error: Into<http::Error>,
    {
        let mut builder = http::request::Builder::new()
            .method(method.unwrap_or_default())
            .uri(uri.unwrap_or_default());
        for (key, value) in headers {
            builder = builder.header(key, value);
        }
        let req = builder.body(body)?;

        Ok(Self { inner: req })
    }
}

fn main() -> Result<(), Box<dyn Error>> {
    let _ = Request::fake_builder()
        .header(("a".to_string(), "b".to_string()))
        .header(("a".to_string(), "b".to_string()))
        .body("")
        .build()
        .unwrap();

    Ok(())
}