tlkit-expand

Crates.iotlkit-expand
lib.rstlkit-expand
version0.3.0
sourcesrc
created_at2024-02-15 02:05:27.835528
updated_at2024-04-27 10:40:06.966887
descriptionTool kit
homepage
repository
max_upload_size
id1140564
size27,772
(git-lloyd)

documentation

README

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(()),
        }
    }
}
Commit count: 0

cargo fmt