Skip to content

Instantly share code, notes, and snippets.

@jmsdnns
Last active September 3, 2024 21:43
Show Gist options
  • Save jmsdnns/0f5ac64a9aba3922fdc12c165de6f370 to your computer and use it in GitHub Desktop.
Save jmsdnns/0f5ac64a9aba3922fdc12c165de6f370 to your computer and use it in GitHub Desktop.
Pretty printed the way Rust's parse_macro_input! parses a simple struct. Handy to know the full thing for pattern matching.
// rust has pretty print support for printing structures. this is how rust's proc_macros
// parse the following struct:
//
// pub struct Book {
// id: u64,
// title: String,
// pages: u64,
// author: String,
// }
//
// In this case, it was initialized like this:
//
// let b = Book {
// id: 1728,
// title: "My Story".to_string(),
// pages: 1337,
// author: "Jms Dnns".to_string(),
// };
DeriveInput {
attrs: [],
vis: Visibility::Public(
Pub,
),
ident: Ident {
ident: "Book",
span: #0 bytes(78..82),
},
generics: Generics {
lt_token: None,
params: [],
gt_token: None,
where_clause: None,
},
data: Data::Struct {
struct_token: Struct,
fields: Fields::Named {
brace_token: Brace,
named: [
Field {
attrs: [],
vis: Visibility::Inherited,
mutability: FieldMutability::None,
ident: Some(
Ident {
ident: "id",
span: #0 bytes(89..91),
},
),
colon_token: Some(
Colon,
),
ty: Type::Path {
qself: None,
path: Path {
leading_colon: None,
segments: [
PathSegment {
ident: Ident {
ident: "u64",
span: #0 bytes(93..96),
},
arguments: PathArguments::None,
},
],
},
},
},
Comma,
Field {
attrs: [],
vis: Visibility::Inherited,
mutability: FieldMutability::None,
ident: Some(
Ident {
ident: "title",
span: #0 bytes(102..107),
},
),
colon_token: Some(
Colon,
),
ty: Type::Path {
qself: None,
path: Path {
leading_colon: None,
segments: [
PathSegment {
ident: Ident {
ident: "String",
span: #0 bytes(109..115),
},
arguments: PathArguments::None,
},
],
},
},
},
Comma,
Field {
attrs: [],
vis: Visibility::Inherited,
mutability: FieldMutability::None,
ident: Some(
Ident {
ident: "pages",
span: #0 bytes(121..126),
},
),
colon_token: Some(
Colon,
),
ty: Type::Path {
qself: None,
path: Path {
leading_colon: None,
segments: [
PathSegment {
ident: Ident {
ident: "u64",
span: #0 bytes(128..131),
},
arguments: PathArguments::None,
},
],
},
},
},
Comma,
Field {
attrs: [],
vis: Visibility::Inherited,
mutability: FieldMutability::None,
ident: Some(
Ident {
ident: "author",
span: #0 bytes(137..143),
},
),
colon_token: Some(
Colon,
),
ty: Type::Path {
qself: None,
path: Path {
leading_colon: None,
segments: [
PathSegment {
ident: Ident {
ident: "String",
span: #0 bytes(145..151),
},
arguments: PathArguments::None,
},
],
},
},
},
Comma,
],
},
semi_token: None,
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment