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

#14 in #i32

Download history 1/week @ 2024-09-18 3/week @ 2024-09-25 2/week @ 2024-10-02

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

~315–760KB
~18K SLoC