七万号·数据平台实战手记

Back

serde#

Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.

仅依赖syn ,quote ,proc-macro2

基本操作#

Deserialize#

quote#

This crate provides the quote! macro for turning Rust syntax tree data structures into tokens of source code. The idea of quasi-quoting is that we write code that we treat as data.

基本类型#

重复、循环#

注释#


let token1 = quote!{
/* comment */
};
let token2 = quote!{
// comment
};

assert_eq!(token1.to_string(),token2.to_string())
rust

syn#

Syn is a parsing library for parsing a stream of Rust tokens into a syntax tree of Rust source code.

syn::File#

let mut file = File::open(&filename).expect("Unable to open file");

let mut src = String::new();
file.read_to_string(&mut src).expect("Unable to read file");

let ast = syn::parse_file(&src).unwrap();
if let Some(shebang) = ast.shebang {
    println!("{}", shebang);
}
println!("{} items", ast.items.len());
rust

DeriveInput for proc_macro_derive#

fold 可以在语法转换时hook#

关于Serde Syn quote那些不得不说的故事
https://realcpf.tech/blog/serde-syn-quote
Author 刘佳成
Published at 2024年2月29日