Skip to content

Commit 30dffd4

Browse files
committed
Refactor: Rename Container to StructInfo
The terminology "Container" is "inside baseball" for things in Rust that can contain something else: A tuple struct, a named structure, an enum, etc. In our case we only deal with named structs so this can be specialized to something people who aren't intimately familiar with proc macro terminology can visualize a bit better.
1 parent 13de059 commit 30dffd4

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

cache_diff_derive/src/fields.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl CacheDiffField {
2828
fn new(
2929
field: &Field,
3030
attributes: CacheDiffAttributes,
31-
container: &Container,
31+
container: &StructInfo,
3232
) -> syn::Result<Option<Self>> {
3333
if let Some(ignore) = attributes.ignore {
3434
if ignore == "custom" && container.custom.is_none() {
@@ -81,29 +81,42 @@ fn is_pathbuf(ty: &syn::Type) -> bool {
8181
false
8282
}
8383

84-
/// Represents a single attribute on a container AKA a struct
84+
/// Represents a single (comma delimited) `cache_diff` attribute on the Struct
85+
///
86+
/// These are also called "container attributes" in proc-macro terminology however
87+
/// the only container we support are structs.
88+
///
89+
/// A [StructInfo] can be built out of one or more attributes
8590
#[derive(Debug, Default)]
86-
struct ContainerAttribute {
91+
struct StructAttribute {
92+
/// #[cache_diff(custom = <diff function path>)]
8793
custom: Option<syn::Path>,
8894
}
8995

90-
/// Represents the Struct
91-
struct Container {
96+
/// Represents the configuration on the Struct
97+
///
98+
/// Proc macros can be configured by using attributes like `#[cache_diff(...)]`. These can apply to
99+
/// fields or the container that holds those fields. This proc macro only supports
100+
/// named Struct containers so this finalized attribute information is stored in [StructInfo].
101+
struct StructInfo {
102+
/// Name of the struct i.e. `struct Foo {}` would be `Foo`
92103
identifier: Ident,
104+
/// Path to a custom diff function, comes from [StructAttribute] parsing
93105
custom: Option<syn::Path>,
106+
/// The fields of a struct i.e. `struct Example { name: String }` would hold `[name: String]` (more or less)
94107
fields: Punctuated<Field, Comma>,
95108
}
96109

97-
impl Container {
110+
impl StructInfo {
98111
fn from_ast(input: &syn::DeriveInput) -> syn::Result<Self> {
99112
let identifier = input.ident.clone();
100113
let attrs = input.attrs.clone();
101114

102115
let attributes = attrs
103116
.iter()
104117
.filter(|attr| attr.path().is_ident("cache_diff"))
105-
.map(|attr| attr.parse_args_with(ContainerAttribute::parse))
106-
.collect::<syn::Result<Vec<ContainerAttribute>>>()?;
118+
.map(|attr| attr.parse_args_with(StructAttribute::parse))
119+
.collect::<syn::Result<Vec<StructAttribute>>>()?;
107120

108121
if attributes.len() > 1 {
109122
return Err(syn::Error::new(
@@ -122,22 +135,22 @@ impl Container {
122135
}
123136
.to_owned();
124137

125-
Ok(Container {
138+
Ok(StructInfo {
126139
identifier,
127140
custom,
128141
fields,
129142
})
130143
}
131144
}
132145

133-
impl Parse for ContainerAttribute {
146+
impl Parse for StructAttribute {
134147
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
135148
let name: Ident = input.parse()?;
136149
let name_str = name.to_string();
137150
match name_str.as_ref() {
138151
"custom" => {
139152
input.parse::<syn::Token![=]>()?;
140-
Ok(ContainerAttribute { custom: Some(input.parse()?) })
153+
Ok(StructAttribute { custom: Some(input.parse()?) })
141154
}
142155
_ => Err(syn::Error::new(
143156
name.span(),
@@ -151,10 +164,10 @@ impl Parse for ContainerAttribute {
151164

152165
pub fn create_cache_diff(item: TokenStream) -> syn::Result<TokenStream> {
153166
let ast: DeriveInput = syn::parse2(item).unwrap();
154-
let container = Container::from_ast(&ast)?;
155-
let struct_identifier = &container.identifier;
167+
let struct_info = StructInfo::from_ast(&ast)?;
168+
let struct_identifier = &struct_info.identifier;
156169

157-
let custom_diff = if let Some(ref custom_fn) = container.custom {
170+
let custom_diff = if let Some(ref custom_fn) = struct_info.custom {
158171
quote! {
159172
let custom_diff = #custom_fn(old, self);
160173
for diff in &custom_diff {
@@ -166,9 +179,9 @@ pub fn create_cache_diff(item: TokenStream) -> syn::Result<TokenStream> {
166179
};
167180

168181
let mut comparisons = Vec::new();
169-
for f in container.fields.iter() {
182+
for f in struct_info.fields.iter() {
170183
let attributes = CacheDiffAttributes::from(f)?;
171-
let field = CacheDiffField::new(f, attributes, &container)?;
184+
let field = CacheDiffField::new(f, attributes, &struct_info)?;
172185

173186
if let Some(CacheDiffField {
174187
field_identifier: field_ident,

0 commit comments

Comments
 (0)