Skip to content

Commit

Permalink
fix: update borrow and owning of macro
Browse files Browse the repository at this point in the history
  • Loading branch information
marieaurore123 committed Oct 18, 2024
1 parent 4bc7d07 commit 4d2ffdb
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 41 deletions.
22 changes: 9 additions & 13 deletions rig-core/rig-core-derive/src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ use syn::{parse_quote, Attribute, DataStruct, Meta};
use crate::EMBED;

/// Finds and returns fields with simple #[embed] attribute tags only.
pub(crate) fn basic_embed_fields(data_struct: &DataStruct) -> impl Iterator<Item = syn::Field> {
data_struct.fields.clone().into_iter().filter(|field| {
field
.attrs
.clone()
.into_iter()
.any(|attribute| match attribute {
Attribute {
meta: Meta::Path(path),
..
} => path.is_ident(EMBED),
_ => false,
})
pub(crate) fn basic_embed_fields(data_struct: &DataStruct) -> impl Iterator<Item = &syn::Field> {
data_struct.fields.iter().filter(|field| {
field.attrs.iter().any(|attribute| match attribute {
Attribute {
meta: Meta::Path(path),
..
} => path.is_ident(EMBED),
_ => false,
})
})
}

Expand Down
10 changes: 4 additions & 6 deletions rig-core/rig-core-derive/src/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ const EMBED_WITH: &str = "embed_with";
/// Also returns the "..." part of the tag (ie. the custom function).
pub(crate) fn custom_embed_fields(
data_struct: &syn::DataStruct,
) -> syn::Result<Vec<(syn::Field, syn::ExprPath)>> {
) -> syn::Result<Vec<(&syn::Field, syn::ExprPath)>> {
data_struct
.fields
.clone()
.into_iter()
.iter()
.filter_map(|field| {
field
.attrs
.clone()
.into_iter()
.iter()
.filter_map(|attribute| match attribute.is_custom() {
Ok(true) => match attribute.expand_tag() {
Ok(path) => Some(Ok((field.clone(), path))),
Ok(path) => Some(Ok((field, path))),
Err(e) => Some(Err(e)),
},
Ok(false) => None,
Expand Down
4 changes: 2 additions & 2 deletions rig-core/rig-core-derive/src/embeddable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl StructParser for DataStruct {
.map(|field| {
add_struct_bounds(generics, &field.ty);

let field_name = field.ident;
let field_name = &field.ident;

quote! {
self.#field_name
Expand Down Expand Up @@ -106,7 +106,7 @@ impl StructParser for DataStruct {
// Iterate over every field tagged with #[embed(embed_with = "...")]
.into_iter()
.map(|(field, custom_func_path)| {
let field_name = field.ident;
let field_name = &field.ident;

quote! {
#custom_func_path(self.#field_name.clone())
Expand Down
3 changes: 2 additions & 1 deletion rig-core/src/one_or_many.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ mod test {

one_or_many.iter_mut().enumerate().for_each(|(i, item)| {
if i == 0 {
assert_eq!(item, "hello");
item.push_str(" world");
assert_eq!(item, "hello world");
}
if i == 1 {
assert_eq!(item, "word");
Expand Down
19 changes: 0 additions & 19 deletions rig-core/tests/embeddable_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ fn test_custom_embed() {
},
};

println!(
"FakeDefinition: {}, {}",
fake_definition.id, fake_definition.word
);

assert_eq!(
fake_definition.embeddable().unwrap(),
OneOrMany::one(
Expand Down Expand Up @@ -70,11 +65,6 @@ fn test_custom_and_basic_embed() {
},
};

println!(
"FakeDefinition: {}, {}",
fake_definition.id, fake_definition.word
);

assert_eq!(
fake_definition.embeddable().unwrap().first(),
"house".to_string()
Expand Down Expand Up @@ -104,11 +94,6 @@ fn test_single_embed() {
definition: definition.clone(),
};

println!(
"FakeDefinition3: {}, {}",
fake_definition.id, fake_definition.word
);

assert_eq!(
fake_definition.embeddable().unwrap(),
OneOrMany::one(definition)
Expand All @@ -131,8 +116,6 @@ fn test_multiple_embed_strings() {
employee_ages: vec![25, 30, 35, 40],
};

println!("Company: {}, {}", company.id, company.company);

let result = company.embeddable().unwrap();

assert_eq!(
Expand Down Expand Up @@ -171,8 +154,6 @@ fn test_multiple_embed_tags() {
employee_ages: vec![25, 30, 35, 40],
};

println!("Company2: {}", company.id);

assert_eq!(
company.embeddable().unwrap(),
OneOrMany::many(vec![
Expand Down

0 comments on commit 4d2ffdb

Please sign in to comment.