11 releases

0.3.0 Apr 27, 2024
0.2.9 Apr 1, 2024
0.2.7 Mar 31, 2024
0.2.1 Feb 26, 2024
0.1.0 Feb 15, 2024

#4 in #http-status

Download history 336/week @ 2024-03-13 60/week @ 2024-03-20 311/week @ 2024-03-27 103/week @ 2024-04-03 142/week @ 2024-04-10 120/week @ 2024-04-24 10/week @ 2024-05-01 3/week @ 2024-05-29

995 downloads per month

MIT license

4KB

tlkit

  • 派生宏EnumerGetter
fn test_getter_expand() -> syn::Result<()> {
    let input: DeriveInput = parse_quote! {
        #[props{
            status: i32,
            message: String,
        }]
        pub enum HttpStatus {
            #[values(200, "操作成功")]
            Success,

            #[values(500, format!("服务繁忙"))]
            ServerError { cause: String },
        }
    };

    let result = getter::expand(input);
    let token_stream = result.unwrap().to_string();
    println!("{token_stream}");
    Ok(())
}

输出结果:

impl HttpStatus {
    pub fn get_status(&self) -> i32 {
        match self {
            Self::Success => 200.into(),
            Self::ServerError { cause } => 500.into(),
        }
    }
    pub fn get_message(&self) -> String {
        match self {
            Self::Success => "操作成功".into(),
            Self::ServerError { cause } => format!("服务繁忙").into(),
        }
    }
}
  • 派生宏TriFromPrimite
fn test_try_from_expand() -> syn::Result<()> {
    let input: DeriveInput = parse_quote! {
        #[derive(Debug, EnumTryFrom)]
        pub enum Language {
            Chinese,

            English,
        }
    };
    let token_stream = try_from::expand(input).unwrap().to_string();
    println!("{token_stream}");
    Ok(())
}

输出结果:

impl TryFrom<&i32> for Language {
    type Error = ();
    fn try_from(value: &i32) -> Result<Self, Self::Error> {
        if (Self::Chinese as i32).eq(value) {
            return Ok(Self::Chinese);
        }
        if (Self::English as i32).eq(value) {
            return Ok(Self::English);
        }
        Err(())
    }
}
impl TryFrom<&String> for Language {
    type Error = ();
    fn try_from(value: &String) -> Result<Self, Self::Error> {
        match value.as_str() {
            "Chinese" => Ok(Self::Chinese),
            "English" => Ok(Self::English),
            _ => Err(()),
        }
    }
}

Dependencies

~0.4–0.8MB
~20K SLoC