Skip to content

Commit 12bfc73

Browse files
committed
feat(bindings): expose recipe reference fields for ingredients
Add RecipeReference struct to bindings with name and components fields, allowing consumers to access recipe dependencies when an ingredient references another recipe file (e.g., @./pasta/spaghetti).
1 parent 3311c51 commit 12bfc73

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

bindings/src/lib.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,8 @@ a test @step @salt{1%mg} more text
581581
quantity: Value::Number { value: 1.0 },
582582
units: Some("mg".to_string())
583583
}),
584-
descriptor: None
584+
descriptor: None,
585+
reference: None
585586
})
586587
);
587588

@@ -619,15 +620,17 @@ a test @step @salt{1%mg} more text
619620
Ingredient {
620621
name: "step".to_string(),
621622
amount: None,
622-
descriptor: None
623+
descriptor: None,
624+
reference: None
623625
},
624626
Ingredient {
625627
name: "salt".to_string(),
626628
amount: Some(Amount {
627629
quantity: Value::Number { value: 1.0 },
628630
units: Some("mg".to_string())
629631
}),
630-
descriptor: None
632+
descriptor: None,
633+
reference: None
631634
},
632635
]
633636
);
@@ -809,6 +812,7 @@ dried oregano
809812
units: Some("g".to_string()),
810813
}),
811814
descriptor: None,
815+
reference: None,
812816
},
813817
Ingredient {
814818
name: "pepper".to_string(),
@@ -817,6 +821,7 @@ dried oregano
817821
units: Some("mg".to_string()),
818822
}),
819823
descriptor: None,
824+
reference: None,
820825
},
821826
Ingredient {
822827
name: "salt".to_string(),
@@ -825,6 +830,7 @@ dried oregano
825830
units: Some("kg".to_string()),
826831
}),
827832
descriptor: None,
833+
reference: None,
828834
},
829835
Ingredient {
830836
name: "pepper".to_string(),
@@ -833,6 +839,7 @@ dried oregano
833839
units: Some("tsp".to_string()),
834840
}),
835841
descriptor: None,
842+
reference: None,
836843
},
837844
];
838845

@@ -1212,4 +1219,31 @@ Combine @cheese{100%g} and @spinach{50%g}, then season to taste.
12121219
}
12131220
);
12141221
}
1222+
1223+
#[test]
1224+
fn test_recipe_reference() {
1225+
use crate::{parse_recipe, RecipeReference};
1226+
1227+
let recipe = parse_recipe(
1228+
r#"
1229+
Serve the @./pasta/spaghetti{1%portion} with sauce
1230+
"#
1231+
.to_string(),
1232+
1.0,
1233+
);
1234+
1235+
let ingredient = recipe
1236+
.ingredients
1237+
.get(0)
1238+
.expect("No ingredients found");
1239+
1240+
assert_eq!(ingredient.name, "spaghetti");
1241+
assert_eq!(
1242+
ingredient.reference,
1243+
Some(RecipeReference {
1244+
name: "spaghetti".to_string(),
1245+
components: vec![".".to_string(), "pasta".to_string()],
1246+
})
1247+
);
1248+
}
12151249
}

bindings/src/model.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use cooklang::metadata::{
44
NameAndUrl as OriginalNameAndUrl, RecipeTime as OriginalRecipeTime,
55
Servings as OriginalServings, StdKey as OriginalStdKey,
66
};
7-
use cooklang::model::Item as OriginalItem;
7+
use cooklang::model::{Item as OriginalItem, RecipeReference as OriginalRecipeReference};
88
use cooklang::quantity::{Quantity as OriginalQuantity, Value as OriginalValue};
99
use cooklang::Recipe as OriginalRecipe;
1010

@@ -84,12 +84,39 @@ pub struct BlockNote {
8484
pub text: String,
8585
}
8686

87+
/// Represents a reference to another recipe file
88+
#[derive(uniffi::Record, Debug, PartialEq, Clone)]
89+
pub struct RecipeReference {
90+
/// The recipe file name (without directory path)
91+
pub name: String,
92+
/// Directory path components (e.g., [".", "pasta"] for "./pasta/recipe")
93+
pub components: Vec<String>,
94+
}
95+
96+
impl RecipeReference {
97+
/// Returns the full path as a string with the given separator
98+
pub fn path(&self, separator: &str) -> String {
99+
self.components.join(separator) + separator + &self.name
100+
}
101+
}
102+
103+
impl From<&OriginalRecipeReference> for RecipeReference {
104+
fn from(reference: &OriginalRecipeReference) -> Self {
105+
RecipeReference {
106+
name: reference.name.clone(),
107+
components: reference.components.clone(),
108+
}
109+
}
110+
}
111+
87112
/// Represents an ingredient in the recipe
88113
#[derive(uniffi::Record, Debug, PartialEq, Clone)]
89114
pub struct Ingredient {
90115
pub name: String,
91116
pub amount: Option<Amount>,
92117
pub descriptor: Option<String>,
118+
/// Reference to another recipe file, if this ingredient is a recipe reference
119+
pub reference: Option<RecipeReference>,
93120
}
94121

95122
/// Represents a piece of cookware used in the recipe
@@ -556,6 +583,7 @@ impl From<&cooklang::Ingredient> for Ingredient {
556583
name: ingredient.name.clone(),
557584
amount: ingredient.quantity.as_ref().map(|q| q.extract_amount()),
558585
descriptor: ingredient.note.clone(),
586+
reference: ingredient.reference.as_ref().map(|r| r.into()),
559587
}
560588
}
561589
}

0 commit comments

Comments
 (0)