summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: e961f1c57bb4495176494461273ddbac86c52346 (plain) (blame)
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
pub struct Ingredient {
    pub name: String,
    pub quantity: String,
    pub unit: Option<String>,
    pub addendum: Option<String>,
}

pub struct Recipe {
    pub title: Option<String>,
    pub ingredients: Vec<Ingredient>,
    pub process: Vec<String>,
}
impl Recipe {
    pub fn parse(recipe: &str) -> Self {
        let mut ingredients = Vec::new();
        let mut process = Vec::new();
        let mut paragraph = String::new();
        let mut title = None;

        for line in recipe.lines() {
            if line.trim().is_empty() {
                let paragraph = std::mem::take(&mut paragraph);
                if !paragraph.is_empty() {
                    process.push(paragraph);
                }
                continue;
            }
            if line.starts_with("# ") && title.is_none() {
                title = Some(line[2..].to_string());
                continue;
            }

            let chars: Vec<char> = line.chars().collect();
            let mut i = 0;
            while i < chars.len() {
                match capture(&chars[i..]) {
                    Some((ingredient, length)) => {
                        paragraph.push_str(&ingredient.name);
                        ingredients.push(ingredient);
                        i += length;
                    }
                    None => {
                        paragraph.push(*chars.get(i).unwrap());
                        i += 1;
                    }
                }
            }
        }

        let paragraph = std::mem::take(&mut paragraph);
        if !paragraph.is_empty() {
            process.push(paragraph);
        }

        Self {
            title,
            ingredients,
            process,
        }
    }
}

fn capture(chars: &[char]) -> Option<(Ingredient, usize)> {
    if chars.get(0) != Some(&'{') {
        return None;
    }
    let mut i = 1;
    let mut name = String::new();
    let mut quantity = String::new();
    let mut unit = None;
    let mut addendum = None;

    // Ingredient name
    loop {
        match chars.get(i) {
            Some(&',') => {
                i += 1;
                break;
            }
            Some(c) => {
                name.push(*c);
                i += 1;
            }
            None => return None,
        }
    }

    // Eat spaces
    loop {
        match chars.get(i) {
            Some(&' ') => i += 1,
            Some(_) => break,
            None => return None,
        }
    }

    // Quantity
    loop {
        match chars.get(i) {
            Some(&' ') => {
                i += 1;
                unit = Some(String::new());
                break;
            }
            Some(&',') => {
                i += 1;
                addendum = Some(String::new());
                break;
            }
            Some(&'}') => {
                i += 1;
                break;
            }
            Some(c) => {
                quantity.push(*c);
                i += 1;
            }
            None => return None,
        }
    }

    // Unit

    if let Some(ref mut unit) = unit {
        loop {
            match chars.get(i) {
                Some(&'}') => {
                    i += 1;
                    break;
                }
                Some(&',') => {
                    i += 1;
                    addendum = Some(String::new());
                    break;
                }
                Some(c) => {
                    unit.push(*c);
                    i += 1;
                }
                None => return None,
            }
        }
    }

    // Addendum
    if let Some(ref mut addendum) = addendum {
        loop {
            match chars.get(i) {
                Some(&'}') => {
                    i += 1;
                    break;
                }
                Some(c) => {
                    addendum.push(*c);
                    i += 1;
                }
                None => return None,
            }
        }
    }

    // Trim values
    let name = name.trim().to_string();
    let quantity = quantity.trim().to_string();
    let unit = unit.and_then(|s| Some(s.trim().to_string()));
    let addendum = addendum.and_then(|s| Some(s.trim().to_string()));

    Some((
        Ingredient {
            name,
            quantity,
            unit,
            addendum,
        },
        i,
    ))
}