cookcli 0.38.1

A command-line interface for managing and working with Cooklang recipes
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
//! Edits to `aisle.conf` made one line at a time.
//!
//! `cooklang::aisle::write` would rewrite the whole file from the parsed
//! configuration, dropping every comment and the blank lines people use to
//! group a long aisle. These edits touch only the lines they are about, so the
//! rest of the file stays as its owner wrote it.
//!
//! Lines are read the way `cooklang::aisle::parse` reads them: anything after
//! `//` is a comment, `[name]` starts an aisle, and any other line lists one
//! ingredient's names separated by `|`, the first being the name the shopping
//! list shows.

use std::collections::hash_map::DefaultHasher;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::Range;

/// Why an edit was refused. The file is left as it was.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EditError {
    /// A name the file could not hold as typed.
    InvalidName(String),
    /// The name is already used.
    Taken(String),
    /// Nothing has that name.
    NotFound(String),
}

impl fmt::Display for EditError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            EditError::InvalidName(message)
            | EditError::Taken(message)
            | EditError::NotFound(message) => f.write_str(message),
        }
    }
}

impl std::error::Error for EditError {}

/// A short fingerprint of the file's text. A page sends back the one it was
/// shown, so an edit made against a file someone else has changed since is
/// refused instead of applied to lines that moved.
///
/// `DefaultHasher::new` uses fixed keys, so the value is the same for the
/// same text for as long as the server runs, which is all it is compared
/// across.
pub fn revision(text: &str) -> String {
    let mut hasher = DefaultHasher::new();
    text.hash(&mut hasher);
    format!("{:016x}", hasher.finish())
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum Kind {
    Blank,
    Comment,
    Aisle(String),
    Ingredient(Vec<String>),
}

/// The line without its comment, and the comment from its `//` on.
fn split_comment(line: &str) -> (&str, Option<&str>) {
    match line.find("//") {
        Some(at) => (&line[..at], Some(&line[at..])),
        None => (line, None),
    }
}

fn kind(line: &str) -> Kind {
    let (code, comment) = split_comment(line);
    let code = code.trim_ascii();
    if code.starts_with('[') && code.ends_with(']') {
        // Not trimmed inside the brackets: neither is the parser.
        Kind::Aisle(code[1..code.len() - 1].to_string())
    } else if !code.is_empty() {
        Kind::Ingredient(
            code.split('|')
                .map(str::trim)
                .filter(|name| !name.is_empty())
                .map(str::to_string)
                .collect(),
        )
    } else if comment.is_some() {
        Kind::Comment
    } else {
        Kind::Blank
    }
}

/// `line` with `code` in place of what it had before its comment, keeping
/// the indentation and the comment.
fn replace_code(line: &str, code: &str) -> String {
    let (old, comment) = split_comment(line);
    let indent = &old[..old.len() - old.trim_start().len()];
    match comment {
        Some(comment) => {
            let gap = &old[old.trim_end().len()..];
            let gap = if gap.is_empty() { " " } else { gap };
            format!("{indent}{code}{gap}{comment}")
        }
        None => format!("{indent}{code}"),
    }
}

fn clean(name: &str, what: &str) -> Result<String, EditError> {
    let name = name.trim();
    let problem = if name.is_empty() {
        "cannot be empty"
    } else if name.contains('|') {
        "cannot contain '|'"
    } else if name.contains("//") {
        "cannot contain '//'"
    } else if name.contains(['\n', '\r']) {
        "must fit on one line"
    } else {
        return Ok(name.to_string());
    };
    Err(EditError::InvalidName(format!("{what} {problem}")))
}

fn aisle_name(name: &str) -> Result<String, EditError> {
    clean(name, "An aisle name")
}

fn ingredient_names(names: &[String]) -> Result<Vec<String>, EditError> {
    let names = names
        .iter()
        .map(|name| clean(name, "An ingredient name"))
        .collect::<Result<Vec<_>, _>>()?;
    let (Some(first), Some(last)) = (names.first(), names.last()) else {
        return Err(EditError::InvalidName(
            "An ingredient needs a name".to_string(),
        ));
    };
    if first.starts_with('[') && last.ends_with(']') {
        return Err(EditError::InvalidName(format!(
            "\"{}\" would be read as an aisle",
            names.join(" | ")
        )));
    }
    Ok(names)
}

/// An `aisle.conf` held as its lines.
#[derive(Debug, Clone)]
pub struct AisleFile {
    lines: Vec<String>,
    eol: &'static str,
}

impl AisleFile {
    pub fn parse(text: &str) -> Self {
        let eol = if text.contains("\r\n") { "\r\n" } else { "\n" };
        Self {
            lines: text.lines().map(str::to_string).collect(),
            eol,
        }
    }

    /// The file's text, ending with a line break.
    pub fn to_text(&self) -> String {
        let mut text = self.lines.join(self.eol);
        if !self.lines.is_empty() {
            text.push_str(self.eol);
        }
        text
    }

    /// Each aisle's header line and name, in file order.
    fn aisles(&self) -> Vec<(usize, String)> {
        self.lines
            .iter()
            .enumerate()
            .filter_map(|(at, line)| match kind(line) {
                Kind::Aisle(name) => Some((at, name)),
                _ => None,
            })
            .collect()
    }

    fn find_aisle(&self, name: &str) -> Result<usize, EditError> {
        self.aisles()
            .into_iter()
            .find(|(_, aisle)| aisle == name)
            .map(|(at, _)| at)
            .ok_or_else(|| EditError::NotFound(format!("There is no aisle called \"{name}\"")))
    }

    /// The aisle a line falls under, if any.
    fn aisle_of(&self, line: usize) -> Option<String> {
        self.lines[..line]
            .iter()
            .rev()
            .find_map(|line| match kind(line) {
                Kind::Aisle(name) => Some(name),
                _ => None,
            })
    }

    fn check_aisle_free(&self, name: &str, except: Option<usize>) -> Result<(), EditError> {
        let folded = name.to_lowercase();
        match self
            .aisles()
            .into_iter()
            .find(|(at, aisle)| Some(*at) != except && aisle.to_lowercase() == folded)
        {
            Some((_, aisle)) => Err(EditError::Taken(format!(
                "There is already an aisle called \"{aisle}\""
            ))),
            None => Ok(()),
        }
    }

    /// The lines that go with the aisle whose header is at `header`: the
    /// comments right above it, the header, and what follows up to the next
    /// aisle and the comments right above that one.
    fn block(&self, header: usize) -> Range<usize> {
        let is_comment = |at: usize| kind(&self.lines[at]) == Kind::Comment;
        let mut start = header;
        while start > 0 && is_comment(start - 1) {
            start -= 1;
        }
        let next = (header + 1..self.lines.len())
            .find(|&at| matches!(kind(&self.lines[at]), Kind::Aisle(_)))
            .unwrap_or(self.lines.len());
        let mut end = next;
        if next < self.lines.len() {
            while end > header + 1 && is_comment(end - 1) {
                end -= 1;
            }
        }
        start..end
    }

    /// Takes `range` out, returning its lines without the blank ones that
    /// ended it.
    fn take_block(&mut self, range: Range<usize>) -> Vec<String> {
        let at_end = range.end == self.lines.len();
        let mut block: Vec<String> = self.lines.drain(range).collect();
        while block.last().is_some_and(|line| kind(line) == Kind::Blank) {
            block.pop();
        }
        if at_end {
            while self
                .lines
                .last()
                .is_some_and(|line| kind(line) == Kind::Blank)
            {
                self.lines.pop();
            }
        }
        block
    }

    /// Puts `block` before the aisle now at `position`, or after the last one,
    /// with a blank line on either side.
    fn put_block(&mut self, mut block: Vec<String>, position: Option<usize>) {
        let before = position.and_then(|position| self.aisles().get(position).map(|a| a.0));
        match before {
            Some(header) => {
                let at = self.block(header).start;
                block.push(String::new());
                if at > 0 && kind(&self.lines[at - 1]) != Kind::Blank {
                    block.insert(0, String::new());
                }
                self.lines.splice(at..at, block);
            }
            None => {
                if self
                    .lines
                    .last()
                    .is_some_and(|line| kind(line) != Kind::Blank)
                {
                    self.lines.push(String::new());
                }
                self.lines.extend(block);
            }
        }
    }

    /// Adds an empty aisle at `position` among the aisles, or last.
    pub fn add_aisle(&mut self, name: &str, position: Option<usize>) -> Result<(), EditError> {
        let name = aisle_name(name)?;
        self.check_aisle_free(&name, None)?;
        self.put_block(vec![format!("[{name}]")], position);
        Ok(())
    }

    pub fn rename_aisle(&mut self, name: &str, new_name: &str) -> Result<(), EditError> {
        let header = self.find_aisle(name)?;
        let new_name = aisle_name(new_name)?;
        self.check_aisle_free(&new_name, Some(header))?;
        self.lines[header] = replace_code(&self.lines[header], &format!("[{new_name}]"));
        Ok(())
    }

    /// Removes an aisle along with the ingredients listed under it.
    pub fn remove_aisle(&mut self, name: &str) -> Result<(), EditError> {
        let header = self.find_aisle(name)?;
        let range = self.block(header);
        self.take_block(range);
        Ok(())
    }

    /// Moves an aisle so that it becomes the one at `position`, or the last
    /// when `position` is past the end. The shopping list follows the file's
    /// order.
    pub fn move_aisle(&mut self, name: &str, position: usize) -> Result<(), EditError> {
        let header = self.find_aisle(name)?;
        let aisles = self.aisles();
        let current = aisles.iter().position(|(at, _)| *at == header);
        if current == Some(position)
            || (current == Some(aisles.len() - 1) && position >= aisles.len())
        {
            return Ok(());
        }
        let range = self.block(header);
        let block = self.take_block(range);
        self.put_block(block, Some(position));
        Ok(())
    }

    /// Each ingredient line and the names on it.
    fn ingredients(&self) -> impl Iterator<Item = (usize, Vec<String>)> + '_ {
        self.lines
            .iter()
            .enumerate()
            .filter_map(|(at, line)| match kind(line) {
                Kind::Ingredient(names) => Some((at, names)),
                _ => None,
            })
    }

    /// The line naming `name`, compared ignoring case as the shopping list
    /// does.
    fn find_ingredient(&self, name: &str) -> Result<usize, EditError> {
        let folded = name.trim().to_lowercase();
        self.ingredients()
            .find(|(_, names)| names.iter().any(|n| n.to_lowercase() == folded))
            .map(|(at, _)| at)
            .ok_or_else(|| EditError::NotFound(format!("\"{}\" is not in any aisle", name.trim())))
    }

    /// Refuses names listed twice among `names`, or already on a line other
    /// than `except`. The shopping list looks names up ignoring case, so so
    /// does this.
    fn check_names_free(&self, names: &[String], except: Option<usize>) -> Result<(), EditError> {
        for (i, name) in names.iter().enumerate() {
            let folded = name.to_lowercase();
            if names[..i]
                .iter()
                .any(|other| other.to_lowercase() == folded)
            {
                return Err(EditError::Taken(format!("\"{name}\" is listed twice")));
            }
            let taken = self.ingredients().find(|(at, names)| {
                Some(*at) != except && names.iter().any(|n| n.to_lowercase() == folded)
            });
            if let Some((at, _)) = taken {
                return Err(EditError::Taken(match self.aisle_of(at) {
                    Some(aisle) => format!("\"{name}\" is already in the \"{aisle}\" aisle"),
                    None => format!("\"{name}\" is already in the file"),
                }));
            }
        }
        Ok(())
    }

    /// Puts an ingredient line under `aisle`, after its last ingredient.
    fn put_ingredient(&mut self, aisle: &str, line: String) -> Result<(), EditError> {
        let header = self.find_aisle(aisle)?;
        let range = self.block(header);
        let at = (header + 1..range.end)
            .rev()
            .find(|&at| matches!(kind(&self.lines[at]), Kind::Ingredient(_)))
            .map_or(header + 1, |at| at + 1);
        self.lines.insert(at, line);
        Ok(())
    }

    /// Lists an ingredient under `aisle`; the first of `names` is the one the
    /// shopping list shows, the others are its synonyms.
    pub fn add_ingredient(&mut self, aisle: &str, names: &[String]) -> Result<(), EditError> {
        let names = ingredient_names(names)?;
        self.find_aisle(aisle)?;
        self.check_names_free(&names, None)?;
        self.put_ingredient(aisle, names.join(" | "))
    }

    /// Gives the ingredient called `name` (by any of its names) the names
    /// `names`, and moves it to `aisle` when that is another aisle.
    pub fn update_ingredient(
        &mut self,
        name: &str,
        names: &[String],
        aisle: Option<&str>,
    ) -> Result<(), EditError> {
        let at = self.find_ingredient(name)?;
        let names = ingredient_names(names)?;
        self.check_names_free(&names, Some(at))?;
        let line = replace_code(&self.lines[at], &names.join(" | "));
        match aisle {
            Some(aisle) if Some(aisle) != self.aisle_of(at).as_deref() => {
                self.find_aisle(aisle)?;
                self.lines.remove(at);
                self.put_ingredient(aisle, line.trim_start().to_string())
            }
            _ => {
                self.lines[at] = line;
                Ok(())
            }
        }
    }

    pub fn remove_ingredient(&mut self, name: &str) -> Result<(), EditError> {
        let at = self.find_ingredient(name)?;
        self.lines.remove(at);
        Ok(())
    }

    /// The names on ingredient line `at`.
    fn names_at(&self, at: usize) -> Vec<String> {
        match kind(&self.lines[at]) {
            Kind::Ingredient(names) => names,
            _ => Vec::new(),
        }
    }

    /// Adds `names` to the names of the ingredient called `name`, so the
    /// shopping list merges them into its first name. Names it already has
    /// are skipped.
    pub fn add_names(&mut self, name: &str, names: &[String]) -> Result<(), EditError> {
        let at = self.find_ingredient(name)?;
        let added = ingredient_names(names)?;
        let mut all = self.names_at(at);
        for name in added {
            let folded = name.to_lowercase();
            if !all.iter().any(|n| n.to_lowercase() == folded) {
                all.push(name);
            }
        }
        self.check_names_free(&all, Some(at))?;
        self.lines[at] = replace_code(&self.lines[at], &all.join(" | "));
        Ok(())
    }

    /// Makes one ingredient of the ones `entries` name, one name for each,
    /// with `main` as the name the shopping list shows. It stays where the
    /// entry holding `main` was, in its aisle and with its comment; the other
    /// entries' lines go, comments included. Their names follow `main`'s own,
    /// in the order the entries were given.
    pub fn merge_ingredients(&mut self, entries: &[String], main: &str) -> Result<(), EditError> {
        let mut lines: Vec<usize> = Vec::new();
        for entry in entries {
            let at = self.find_ingredient(entry)?;
            if !lines.contains(&at) {
                lines.push(at);
            }
        }
        if lines.len() < 2 {
            return Err(EditError::InvalidName(
                "Choose at least two ingredients to group".to_string(),
            ));
        }

        let folded = main.trim().to_lowercase();
        let (keep, main) = lines
            .iter()
            .find_map(|&at| {
                self.names_at(at)
                    .into_iter()
                    .find(|n| n.to_lowercase() == folded)
                    .map(|name| (at, name))
            })
            .ok_or_else(|| {
                EditError::InvalidName(format!(
                    "\"{}\" is not one of the names being grouped",
                    main.trim()
                ))
            })?;

        let mut merged = vec![main];
        let others = std::iter::once(keep).chain(lines.iter().copied().filter(|&at| at != keep));
        for at in others {
            for name in self.names_at(at) {
                let folded = name.to_lowercase();
                if !merged.iter().any(|n| n.to_lowercase() == folded) {
                    merged.push(name);
                }
            }
        }
        let merged = ingredient_names(&merged)?;

        self.lines[keep] = replace_code(&self.lines[keep], &merged.join(" | "));
        let mut gone: Vec<usize> = lines.into_iter().filter(|&at| at != keep).collect();
        gone.sort_unstable_by(|a, b| b.cmp(a));
        for at in gone {
            self.lines.remove(at);
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const SAMPLE: &str = "\
// Store layout, front to back

[fruit and veg]
apples
avocado | avocados // ripe ones

onion | onions

// cold stuff
[dairy]
milk
butter

[bakery]
bread
";

    fn names(list: &[&str]) -> Vec<String> {
        list.iter().map(|s| s.to_string()).collect()
    }

    fn edited(edit: impl FnOnce(&mut AisleFile) -> Result<(), EditError>) -> String {
        let mut file = AisleFile::parse(SAMPLE);
        edit(&mut file).unwrap();
        let text = file.to_text();
        cooklang::aisle::parse(&text).expect("the edit left a file the parser rejects");
        text
    }

    fn aisle_order(text: &str) -> Vec<String> {
        cooklang::aisle::parse(text)
            .unwrap()
            .categories
            .iter()
            .map(|c| c.name.to_string())
            .collect()
    }

    #[test]
    fn an_untouched_file_comes_back_as_it_was() {
        assert_eq!(AisleFile::parse(SAMPLE).to_text(), SAMPLE);
    }

    #[test]
    fn windows_line_endings_are_kept() {
        let text = SAMPLE.replace('\n', "\r\n");
        let mut file = AisleFile::parse(&text);
        file.add_ingredient("dairy", &names(&["eggs"])).unwrap();
        let out = file.to_text();
        assert!(out.contains("butter\r\neggs\r\n"));
        assert!(!out.replace("\r\n", "").contains('\n'));
    }

    #[test]
    fn an_ingredient_goes_after_the_last_one_of_its_aisle() {
        let text = edited(|f| f.add_ingredient("fruit and veg", &names(&[" leek ", "leeks"])));
        assert_eq!(
            text,
            SAMPLE.replace("onion | onions\n", "onion | onions\nleek | leeks\n")
        );
    }

    #[test]
    fn an_ingredient_can_go_into_an_empty_aisle() {
        let mut file = AisleFile::parse("[empty]\n\n[other]\nx\n");
        file.add_ingredient("empty", &names(&["y"])).unwrap();
        assert_eq!(file.to_text(), "[empty]\ny\n\n[other]\nx\n");
    }

    #[test]
    fn names_already_listed_are_refused_ignoring_case() {
        let mut file = AisleFile::parse(SAMPLE);
        assert_eq!(
            file.add_ingredient("dairy", &names(&["Avocados"])),
            Err(EditError::Taken(
                "\"Avocados\" is already in the \"fruit and veg\" aisle".to_string()
            ))
        );
        assert!(matches!(
            file.add_ingredient("dairy", &names(&["cream", "Cream"])),
            Err(EditError::Taken(_))
        ));
        assert_eq!(file.to_text(), SAMPLE);
    }

    #[test]
    fn names_the_file_could_not_hold_are_refused() {
        let mut file = AisleFile::parse(SAMPLE);
        for bad in [
            names(&[]),
            names(&["  "]),
            names(&["a|b"]),
            names(&["a // b"]),
            names(&["a\nb"]),
            names(&["[x]"]),
            names(&["[x", "y]"]),
        ] {
            assert!(
                matches!(
                    file.add_ingredient("dairy", &bad),
                    Err(EditError::InvalidName(_))
                ),
                "{bad:?} was accepted"
            );
        }
        assert!(matches!(
            file.add_aisle("a|b", None),
            Err(EditError::InvalidName(_))
        ));
        assert_eq!(file.to_text(), SAMPLE);
    }

    #[test]
    fn renaming_an_ingredient_keeps_its_comment() {
        let text = edited(|f| {
            f.update_ingredient("AVOCADOS", &names(&["avocado", "avocados", "hass"]), None)
        });
        assert_eq!(
            text,
            SAMPLE.replace(
                "avocado | avocados // ripe ones",
                "avocado | avocados | hass // ripe ones"
            )
        );
    }

    #[test]
    fn an_ingredient_keeps_its_own_names_when_edited() {
        // Its current names are not "taken" by itself.
        edited(|f| {
            f.update_ingredient("onion", &names(&["Onion", "onions"]), Some("fruit and veg"))
        });
    }

    #[test]
    fn moving_an_ingredient_takes_its_comment_along() {
        let text = edited(|f| f.update_ingredient("avocado", &names(&["avocado"]), Some("bakery")));
        assert!(!text.contains("[fruit and veg]\napples\navocado"));
        assert!(text.ends_with("[bakery]\nbread\navocado // ripe ones\n"));
    }

    #[test]
    fn removing_an_ingredient_removes_only_its_line() {
        let text = edited(|f| f.remove_ingredient("butter"));
        assert_eq!(text, SAMPLE.replace("butter\n", ""));
        let mut file = AisleFile::parse(SAMPLE);
        assert!(matches!(
            file.remove_ingredient("caviar"),
            Err(EditError::NotFound(_))
        ));
    }

    #[test]
    fn a_new_aisle_goes_last_or_where_asked() {
        let text = edited(|f| f.add_aisle("frozen", None));
        assert!(text.ends_with("bread\n\n[frozen]\n"));

        let text = edited(|f| f.add_aisle("frozen", Some(1)));
        assert_eq!(
            aisle_order(&text),
            ["fruit and veg", "frozen", "dairy", "bakery"]
        );
        // Before the comment that belongs to "dairy", not between the two.
        assert!(text.contains("onion | onions\n\n[frozen]\n\n// cold stuff\n[dairy]"));
    }

    #[test]
    fn aisle_names_are_unique_ignoring_case() {
        let mut file = AisleFile::parse(SAMPLE);
        assert!(matches!(
            file.add_aisle("Dairy", None),
            Err(EditError::Taken(_))
        ));
        assert!(matches!(
            file.rename_aisle("bakery", "DAIRY"),
            Err(EditError::Taken(_))
        ));
        // A change of case of the aisle itself is fine.
        file.rename_aisle("dairy", "Dairy").unwrap();
        assert!(file.to_text().contains("// cold stuff\n[Dairy]\nmilk"));
    }

    #[test]
    fn removing_an_aisle_takes_its_comment_and_ingredients() {
        let text = edited(|f| f.remove_aisle("dairy"));
        assert_eq!(
            text,
            SAMPLE.replace("// cold stuff\n[dairy]\nmilk\nbutter\n\n", "")
        );
        let text = edited(|f| f.remove_aisle("bakery"));
        assert!(text.ends_with("milk\nbutter\n"));
    }

    #[test]
    fn moving_an_aisle_carries_its_block() {
        let text = edited(|f| f.move_aisle("dairy", 0));
        assert_eq!(aisle_order(&text), ["dairy", "fruit and veg", "bakery"]);
        // The file's own heading stays at the top, the aisle's comment moves.
        assert!(text.starts_with(
            "// Store layout, front to back\n\n// cold stuff\n[dairy]\nmilk\nbutter\n\n[fruit and veg]\n"
        ));
        assert!(text.contains("onion | onions\n\n[bakery]\nbread\n"));

        let text = edited(|f| f.move_aisle("fruit and veg", 9));
        assert_eq!(aisle_order(&text), ["dairy", "bakery", "fruit and veg"]);
        assert!(text.ends_with(
            "bread\n\n[fruit and veg]\napples\navocado | avocados // ripe ones\n\nonion | onions\n"
        ));

        assert_eq!(edited(|f| f.move_aisle("dairy", 1)), SAMPLE);
        assert_eq!(edited(|f| f.move_aisle("bakery", 7)), SAMPLE);
    }

    #[test]
    fn an_empty_file_gets_its_first_aisle() {
        let mut file = AisleFile::parse("");
        file.add_aisle("pantry", None).unwrap();
        file.add_ingredient("pantry", &names(&["rice"])).unwrap();
        assert_eq!(file.to_text(), "[pantry]\nrice\n");
    }

    #[test]
    fn the_revision_follows_the_text() {
        assert_eq!(revision(SAMPLE), revision(SAMPLE));
        assert_ne!(revision(SAMPLE), revision(&SAMPLE.replace("milk", "Milk")));
    }

    #[test]
    fn other_names_join_an_ingredient_and_keep_its_comment() {
        let text = edited(|f| f.add_names("AVOCADOS", &names(&["hass", " Avocados "])));
        assert_eq!(
            text,
            SAMPLE.replace(
                "avocado | avocados // ripe ones",
                "avocado | avocados | hass // ripe ones"
            )
        );

        let mut file = AisleFile::parse(SAMPLE);
        assert!(matches!(
            file.add_names("milk", &names(&["Apples"])),
            Err(EditError::Taken(_))
        ));
        assert!(matches!(
            file.add_names("cream", &names(&["double cream"])),
            Err(EditError::NotFound(_))
        ));
        assert_eq!(file.to_text(), SAMPLE);
    }

    #[test]
    fn grouping_keeps_the_main_entry_where_it_was() {
        // Across aisles: everything ends up on the avocado line, in its
        // aisle, with its comment, and the other lines go.
        let text =
            edited(|f| f.merge_ingredients(&names(&["butter", "avocados", "milk"]), "Avocado"));
        assert_eq!(
            text,
            SAMPLE
                .replace(
                    "avocado | avocados // ripe ones",
                    "avocado | avocados | butter | milk // ripe ones"
                )
                .replace("[dairy]\nmilk\nbutter\n", "[dairy]\n")
        );

        // The main name may come from any entry, even a later one.
        let text = edited(|f| f.merge_ingredients(&names(&["milk", "butter"]), "butter"));
        assert_eq!(
            text,
            SAMPLE.replace("[dairy]\nmilk\nbutter\n", "[dairy]\nbutter | milk\n")
        );
    }

    #[test]
    fn grouping_needs_two_entries_and_one_of_their_names() {
        let mut file = AisleFile::parse(SAMPLE);
        for (entries, main) in [
            (names(&["milk"]), "milk"),
            (names(&["milk", "MILK"]), "milk"),
            (names(&["milk", "butter"]), "cream"),
        ] {
            assert!(
                matches!(
                    file.merge_ingredients(&entries, main),
                    Err(EditError::InvalidName(_))
                ),
                "{entries:?} as {main}"
            );
        }
        assert!(matches!(
            file.merge_ingredients(&names(&["milk", "caviar"]), "milk"),
            Err(EditError::NotFound(_))
        ));
        assert_eq!(file.to_text(), SAMPLE);
    }
}