cooklang-find 0.2.2

Library for finding and managing Cooklang recipes in the filesystem
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use camino::{Utf8Path, Utf8PathBuf};
use cooklang::{CooklangParser, Metadata, Recipe};
use serde::{Deserialize, Serialize};
use std::sync::{Arc, OnceLock};
use thiserror::Error;

#[derive(Debug, Serialize, Deserialize)]
pub struct RecipeEntry {
    /// Optional path to the recipe file
    path: Option<Utf8PathBuf>,
    /// Cached string content of the recipe
    #[serde(skip)]
    content: String,
    /// Cached metadata
    // TODO: this is not correct, metadata can be different for different scaling factor
    metadata: Metadata,

    /// Cachedd name of the recipe (from file stem or title)
    #[serde(skip)]
    name: OnceLock<Option<String>>,
    /// Optional path to the title image
    // TODO some data structure for all images instead
    #[serde(skip)]
    title_image: OnceLock<Option<Utf8PathBuf>>,

    /// Scaling factor for the recipe
    #[serde(skip)]
    scaling_factor: OnceLock<f64>,

    /// Cached parsed recipe
    #[serde(skip)]
    recipe: OnceLock<Arc<Recipe>>,
}

impl RecipeEntry {
    /// Create a new Recipe instance from a path
    pub fn from_path(path: Utf8PathBuf) -> Result<Self, RecipeEntryError> {
        // Read the file content
        let content = std::fs::read_to_string(&path).map_err(RecipeEntryError::IoError)?;

        let (metadata, _warnings) = CooklangParser::canonical()
            .parse_metadata(&content)
            .into_result()
            .unwrap();

        Ok(RecipeEntry {
            path: Some(path.to_path_buf()),
            content,
            metadata,
            name: OnceLock::new(),
            title_image: OnceLock::new(),
            scaling_factor: OnceLock::new(),
            recipe: OnceLock::new(),
        })
    }

    /// Create a new Recipe instance from content
    pub fn from_content(content: String) -> Result<Self, RecipeEntryError> {
        let (metadata, _warnings) = CooklangParser::canonical()
            .parse_metadata(&content)
            .into_result()
            .unwrap();

        Ok(RecipeEntry {
            path: None,
            content,
            metadata,
            name: OnceLock::new(),
            title_image: OnceLock::new(),
            scaling_factor: OnceLock::new(),
            recipe: OnceLock::new(),
        })
    }

    pub fn name(&self) -> &Option<String> {
        self.name.get_or_init(|| {
            if let Some(title) = self.metadata.title() {
                Some(title.to_string())
            } else if let Some(path) = &self.path {
                Some(path.file_stem()?.to_string())
            } else {
                None
            }
        })
    }

    pub fn title_image(&self) -> &Option<Utf8PathBuf> {
        // todo also check metadata
        self.title_image.get_or_init(|| {
            if let Some(path) = &self.path {
                find_title_image(path)
            } else {
                None
            }
        })
    }

    pub fn recipe(&self, scaling_factor: f64) -> Arc<Recipe> {
        // TODO: not correct, cached recipe can be with different scaling factor
        self.recipe
            .get_or_init(|| {
                self.scaling_factor.set(scaling_factor).unwrap();

                let parser = CooklangParser::canonical();

                let (mut recipe, _warnings) = parser.parse(&self.content).into_result().unwrap();

                // Scale the recipe
                recipe.scale(*self.scaling_factor(), parser.converter());
                Arc::new(recipe)
            })
            .clone()
    }

    pub fn scaling_factor(&self) -> &f64 {
        self.scaling_factor.get_or_init(|| 1.0)
    }

    pub fn path(&self) -> &Option<Utf8PathBuf> {
        &self.path
    }
}

#[derive(Error, Debug)]
pub enum RecipeEntryError {
    #[error("Failed to read recipe file: {0}")]
    IoError(#[from] std::io::Error),

    #[error("Failed to get file stem from path: {0}")]
    InvalidPath(Utf8PathBuf),

    #[error("Failed to parse recipe: {0}")]
    ParseError(String),

    #[error("Failed to parse recipe metadata: {0}")]
    MetadataError(String),
}

fn find_title_image(path: &Utf8Path) -> Option<Utf8PathBuf> {
    // Look for an image with the same stem
    let possible_image_extensions = ["jpg", "jpeg", "png", "webp"];
    possible_image_extensions.iter().find_map(|ext| {
        let image_path = path.with_extension(ext);
        if image_path.exists() {
            Some(image_path)
        } else {
            None
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use indoc::indoc;
    use std::fs::File;
    use std::io::Write;
    use tempfile::TempDir;

    fn create_test_recipe(dir: &Utf8Path, name: &str, content: &str) -> Utf8PathBuf {
        let recipe_path = dir.join(format!("{}.cook", name));
        let mut file = File::create(&recipe_path).unwrap();
        write!(file, "{}", content).unwrap();
        recipe_path
    }

    fn create_test_image(dir: &Utf8Path, name: &str, ext: &str) -> Utf8PathBuf {
        let image_path = dir.join(format!("{}.{}", name, ext));
        File::create(&image_path).unwrap();
        image_path
    }

    #[test]
    fn test_recipe_creation() {
        let temp_dir = TempDir::new().unwrap();
        let temp_dir_path = Utf8PathBuf::from_path_buf(temp_dir.path().to_path_buf()).unwrap();
        let recipe_path = create_test_recipe(
            &temp_dir_path,
            "test_recipe",
            indoc! {r#"
                ---
                servings: 4
                ---

                Test recipe content"#},
        );

        let recipe = RecipeEntry::from_path(recipe_path.clone()).unwrap();
        assert_eq!(recipe.name().as_ref().unwrap(), "test_recipe");
        assert_eq!(recipe.path.as_ref().unwrap(), &recipe_path);
        assert!(recipe.title_image().is_none());
    }

    #[test]
    fn test_recipe_with_title_image() {
        let temp_dir = TempDir::new().unwrap();
        let temp_dir_path = Utf8PathBuf::from_path_buf(temp_dir.path().to_path_buf()).unwrap();
        let recipe_path = create_test_recipe(
            &temp_dir_path,
            "test_recipe",
            indoc! {r#"
                ---
                servings: 4
                ---

                Test recipe content"#},
        );
        let image_path = create_test_image(&temp_dir_path, "test_recipe", "jpg");

        let recipe = RecipeEntry::from_path(recipe_path).unwrap();
        assert_eq!(recipe.title_image().as_ref().unwrap(), &image_path);
    }

    #[test]
    fn test_recipe_content() {
        let temp_dir = TempDir::new().unwrap();
        let temp_dir_path = Utf8PathBuf::from_path_buf(temp_dir.path().to_path_buf()).unwrap();
        let content = indoc! {r#"
            ---
            servings: 4
            ---

            Test recipe content"#};
        let recipe_path = create_test_recipe(&temp_dir_path, "test_recipe", content);

        let recipe = RecipeEntry::from_path(recipe_path).unwrap();
        assert_eq!(&recipe.content, content);
    }

    #[test]
    fn test_recipe_metadata() {
        let temp_dir = TempDir::new().unwrap();
        let temp_dir_path = Utf8PathBuf::from_path_buf(temp_dir.path().to_path_buf()).unwrap();
        let content = indoc! {r#"
            ---
            servings: 4
            time: 30 min
            cuisine: Italian
            ---

            Test recipe content"#};
        let recipe_path = create_test_recipe(&temp_dir_path, "test_recipe", content);

        let recipe = RecipeEntry::from_path(recipe_path).unwrap();
        let metadata = &recipe.metadata;

        assert_eq!(metadata.get("servings").unwrap(), 4);
        assert_eq!(metadata.get("time").unwrap(), "30 min");
        assert_eq!(metadata.get("cuisine").unwrap(), "Italian");
    }

    #[test]
    fn test_recipe_parsing() {
        let temp_dir = TempDir::new().unwrap();
        let temp_dir_path = Utf8PathBuf::from_path_buf(temp_dir.path().to_path_buf()).unwrap();
        let content = indoc! {r#"
            ---
            servings: 4
            ---

            Add @salt{1%tsp} and @pepper{1%tsp}"#};
        let recipe_path = create_test_recipe(&temp_dir_path, "test_recipe", content);

        let recipe = RecipeEntry::from_path(recipe_path).unwrap();
        let parsed = recipe.recipe(1.0);

        assert_eq!(parsed.metadata.servings().unwrap()[0], 4);
        assert_eq!(parsed.ingredients.len(), 2);
    }

    #[test]
    fn test_recipe_equality() {
        let temp_dir = TempDir::new().unwrap();
        let temp_dir_path = Utf8PathBuf::from_path_buf(temp_dir.path().to_path_buf()).unwrap();
        let path1 = create_test_recipe(
            &temp_dir_path,
            "recipe1",
            indoc! {r#"
                ---
                servings: 4
                ---

                Test recipe content"#},
        );
        let path2 = create_test_recipe(
            &temp_dir_path,
            "recipe2",
            indoc! {r#"
                ---
                servings: 4
                ---

                Test recipe content"#},
        );

        let recipe1 = RecipeEntry::from_path(path1.clone()).unwrap();
        let recipe2 = RecipeEntry::from_path(path1).unwrap();
        let recipe3 = RecipeEntry::from_path(path2).unwrap();

        // Compare paths since we can't implement PartialEq for RecipeEntry
        assert_eq!(recipe1.path, recipe2.path);
        assert_ne!(recipe1.path, recipe3.path);
    }

    #[test]
    fn test_invalid_recipe_path() {
        let temp_dir = TempDir::new().unwrap();
        let temp_dir_path = Utf8PathBuf::from_path_buf(temp_dir.path().to_path_buf()).unwrap();
        let invalid_path = temp_dir_path.join("nonexistent.cook");

        let result = RecipeEntry::from_path(invalid_path);
        assert!(result.is_err());
    }

    #[test]
    fn test_find_title_image_no_image() {
        let temp_dir = TempDir::new().unwrap();
        let temp_dir_path = Utf8PathBuf::from_path_buf(temp_dir.path().to_path_buf()).unwrap();
        let recipe_path = create_test_recipe(&temp_dir_path, "test_recipe", "Test content");
        assert!(find_title_image(&recipe_path).is_none());
    }

    #[test]
    fn test_find_title_image_all_extensions() {
        let temp_dir = TempDir::new().unwrap();
        let temp_dir_path = Utf8PathBuf::from_path_buf(temp_dir.path().to_path_buf()).unwrap();
        let recipe_path = create_test_recipe(&temp_dir_path, "test_recipe", "Test content");

        // Test each supported extension
        for ext in ["jpg", "jpeg", "png", "webp"] {
            // Clean up any previous test images
            for old_ext in ["jpg", "jpeg", "png", "webp"] {
                let _ = std::fs::remove_file(recipe_path.with_extension(old_ext));
            }

            let image_path = create_test_image(&temp_dir_path, "test_recipe", ext);
            let found = find_title_image(&recipe_path);

            assert!(
                found.is_some(),
                "Failed to find image with extension {}",
                ext
            );
            assert_eq!(found.unwrap(), image_path);
        }
    }

    #[test]
    fn test_find_title_image_multiple_images() {
        let temp_dir = TempDir::new().unwrap();
        let temp_dir_path = Utf8PathBuf::from_path_buf(temp_dir.path().to_path_buf()).unwrap();
        let recipe_path = create_test_recipe(&temp_dir_path, "test_recipe", "Test content");

        // Create images with different extensions
        let jpg_path = create_test_image(&temp_dir_path, "test_recipe", "jpg");
        let _png_path = create_test_image(&temp_dir_path, "test_recipe", "png");
        let _webp_path = create_test_image(&temp_dir_path, "test_recipe", "webp");

        // Should return the first matching extension (jpg)
        let found_image = find_title_image(&recipe_path);
        assert!(found_image.is_some());
        assert_eq!(found_image.unwrap(), jpg_path);
    }

    #[test]
    #[ignore]
    fn test_find_title_image_case_sensitivity() {
        let temp_dir = TempDir::new().unwrap();
        let temp_dir_path = Utf8PathBuf::from_path_buf(temp_dir.path().to_path_buf()).unwrap();
        let recipe_path = create_test_recipe(&temp_dir_path, "test_recipe", "Test content");

        // Create an image with uppercase extension
        let image_path = temp_dir_path.join("test_recipe.JPG");
        File::create(&image_path).unwrap();
        let found_image = find_title_image(&recipe_path);

        // Should find the image with uppercase extension
        assert!(found_image.is_some());
    }

    #[test]
    fn test_recipe_from_content() {
        let content = indoc! {r#"
            ---
            servings: 4
            time: 30 min
            cuisine: Italian
            ---

            Add @salt{1%tsp} and @pepper{1%tsp}"#};

        let recipe = RecipeEntry::from_content(content.to_string()).unwrap();
        assert!(recipe.name().is_none());
        assert!(recipe.path.is_none());
        assert!(recipe.title_image().is_none());

        // Verify content is set
        assert_eq!(&recipe.content, content);

        // Verify metadata is parsed correctly
        let metadata = &recipe.metadata;
        assert_eq!(metadata.get("servings").unwrap(), 4);
        assert_eq!(metadata.get("time").unwrap(), "30 min");
        assert_eq!(metadata.get("cuisine").unwrap(), "Italian");

        // Verify recipe is parsed correctly
        let parsed = recipe.recipe(1.0);
        assert_eq!(parsed.metadata.servings().unwrap()[0], 4);
        assert_eq!(parsed.ingredients.len(), 2);
    }
}