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
47
48
49
50
51
52
#![allow(dead_code)]
use std::convert::TryFrom;
use std::future::Future;
use std::pin::Pin;

use crate::http;
use crate::http::header::{HeaderName, HeaderValue};
use rand::distributions::Alphanumeric;
use rand::Rng;

pub(crate) type BoxFuture<T, E> = Pin<Box<dyn Future<Output=Result<T, E>> + Send + 'static>>;
pub(crate) type StdError = Box<dyn std::error::Error + Send + Sync + 'static>;

pub(crate) fn parse_header<K, V>(
    name: K,
    value: V,
) -> Result<(HeaderName, HeaderValue), crate::errors::errors::Error>
    where
        HeaderName: TryFrom<K>,
        <HeaderName as TryFrom<K>>::Error: Into<http::Error>,
        HeaderValue: TryFrom<V>,
        <HeaderValue as TryFrom<V>>::Error: Into<http::Error>,
{
    let name = <HeaderName as TryFrom<K>>::try_from(name).map_err(|e| {
        let e = e.into();
        crate::error_message!("parse header name failed, err: {}", e)
    })?;

    let value = <HeaderValue as TryFrom<V>>::try_from(value).map_err(|e| {
        let e = e.into();
        crate::error_message!("parse header value failed, err: {}", e)
    })?;

    Ok((name, value))
}

pub(crate) fn gen_random_string(length: usize) -> String {
    rand::thread_rng()
        .sample_iter(&Alphanumeric)
        .map(char::from)
        .take(length)
        .collect::<String>()
}

#[macro_export]
macro_rules! register_method {
    ($func_name: ident, $method: expr) => {
        pub fn $func_name(&mut self, path: impl AsRef<str>, ep: impl Endpoint) {
            self.register($method, path, ep)
        }
    };
}