Crates.io | tlkit-proc |
lib.rs | tlkit-proc |
version | 0.3.0 |
source | src |
created_at | 2024-02-15 02:07:11.043319 |
updated_at | 2024-04-27 10:40:34.58837 |
description | Tool Kit expand proc-macro |
homepage | |
repository | |
max_upload_size | |
id | 1140565 |
size | 4,485 |
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(),
}
}
}
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(()),
}
}
}