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
//! Support for recipe scaling

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{
    convert::Converter,
    quantity::{ScalableQuantity, ScalableValue, ScaledQuantity, TextValueError, Value},
    Cookware, Ingredient, Quantity, ScalableRecipe, ScaledRecipe, Timer,
};

/// Configures the scaling target
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct ScaleTarget {
    base: u32,
    target: u32,
    index: Option<usize>,
}

impl ScaleTarget {
    /// Creates a new [`ScaleTarget`].
    /// - `base` is the number of servings the recipe was initially written for.
    ///   Usually this is the first value of `declared_servings` but doesn't
    ///   need to.
    /// - `target` is the wanted number of servings.
    /// - `declared_servigs` is the slice with all the servings of the recipe
    ///   metadata.
    ///
    /// Invalid parameters don't error here, but may do so in the
    /// scaling process.
    pub fn new(base: u32, target: u32, declared_servings: &[u32]) -> Self {
        ScaleTarget {
            base,
            target,
            index: declared_servings.iter().position(|&s| s == target),
        }
    }

    /// Get the calculated scaling factor
    pub fn factor(&self) -> f64 {
        self.target as f64 / self.base as f64
    }

    /// Get the index into a [`ScalableValue::ByServings`]
    pub fn index(&self) -> Option<usize> {
        self.index
    }

    /// Get the target servings
    pub fn target_servings(&self) -> u32 {
        self.target
    }
}

/// Possible scaled states of a recipe
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Scaled {
    /// The recipe was scaled to its based servings
    ///
    /// This is the values without scaling or if there are many values
    /// for a component, the first one.
    DefaultScaling,
    /// Scaled to a custom target
    Scaled(ScaledData),
}

/// Data from scaling a recipe
#[derive(Debug, Serialize, Deserialize)]
pub struct ScaledData {
    /// What the target was
    pub target: ScaleTarget,
    /// Outcome of scaling the ingredients. Use the same index as in the recipe.
    pub ingredients: Vec<ScaleOutcome>,
    /// Outcome of scaling the cookware items. Use the same index as in the recipe.
    pub cookware: Vec<ScaleOutcome>,
    /// Outcome of scaling the timers. Use the same index as in the recipe.
    pub timers: Vec<ScaleOutcome>,
}

/// Possible outcomes from scaling a component
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ScaleOutcome {
    /// Success
    Scaled,
    /// Not changed becuse it doesn't have to be changed
    Fixed,
    /// It has no quantity, so it can't be scaled
    NoQuantity,
    /// Error scaling
    Error(#[serde(skip)] ScaleError),
}

/// Possible errors during scaling process
#[derive(Debug, Error, Clone, Default)]
pub enum ScaleError {
    #[error(transparent)]
    TextValueError(#[from] TextValueError),

    #[error("Value not scalable: {reason}")]
    NotScalable {
        value: ScalableValue,
        reason: &'static str,
    },

    #[error("Value scaling not defined for target servings")]
    NotDefined {
        target: ScaleTarget,
        value: ScalableValue,
    },

    /// There has been an error but it can't be determined
    ///
    /// This is used when deserializing, because serializing the [`ScaleOutcome`]
    /// skips the error.
    #[default]
    #[error("Undefined scale error")]
    UndefinedError,
}

impl ScalableRecipe {
    /// Scale a recipe
    ///
    /// Note that this returns a [`ScaledRecipe`] wich doesn't implement this
    /// method. A recipe can only be scaled once.
    pub fn scale(self, target: u32, converter: &Converter) -> ScaledRecipe {
        let target = if let Some(servings) = self.metadata.servings.as_ref() {
            let base = servings.first().copied().unwrap_or(1);
            ScaleTarget::new(base, target, servings)
        } else {
            ScaleTarget::new(1, target, &[])
        };

        if target.index() == Some(0) {
            return self.default_scale();
        }

        let (ingredients, ingredient_outcomes): (Vec<_>, Vec<_>) = self
            .ingredients
            .into_iter()
            .map(|i| i.scale(target))
            .map(|(mut i, o)| {
                if let Some(q) = &mut i.quantity {
                    let _ = q.fit(converter);
                }
                (i, o)
            })
            .unzip();

        let (cookware, cookware_outcomes): (Vec<_>, Vec<_>) =
            self.cookware.into_iter().map(|c| c.scale(target)).unzip();

        let (timers, timer_outcomes): (Vec<_>, Vec<_>) = self
            .timers
            .into_iter()
            .map(|c| c.scale(target))
            .map(|(mut t, o)| {
                if let Some(q) = &mut t.quantity {
                    let _ = q.fit(converter);
                }
                (t, o)
            })
            .unzip();

        let data = ScaledData {
            target,
            ingredients: ingredient_outcomes,
            cookware: cookware_outcomes,
            timers: timer_outcomes,
        };

        ScaledRecipe {
            name: self.name,
            metadata: self.metadata,
            sections: self.sections,
            ingredients,
            cookware,
            timers,
            inline_quantities: self.inline_quantities,
            data: Scaled::Scaled(data),
        }
    }

    /// Scale the recipe to the default values
    ///
    /// The default values are the ones written in the recipe and the first one
    /// in [`ScalableValue::ByServings`].
    pub fn default_scale(self) -> ScaledRecipe {
        let ingredients = self
            .ingredients
            .into_iter()
            .map(Scale::default_scale)
            .collect();
        let cookware = self
            .cookware
            .into_iter()
            .map(Scale::default_scale)
            .collect();
        let timers = self.timers.into_iter().map(Scale::default_scale).collect();

        ScaledRecipe {
            name: self.name,
            metadata: self.metadata,
            sections: self.sections,
            ingredients,
            cookware,
            timers,
            inline_quantities: self.inline_quantities,
            data: Scaled::DefaultScaling,
        }
    }
}

trait Scale: Sized {
    type Output;

    fn scale(self, target: ScaleTarget) -> (Self::Output, ScaleOutcome);
    fn default_scale(self) -> Self::Output;
}

impl Scale for ScalableValue {
    type Output = Value;

    fn scale(self, target: ScaleTarget) -> (Self::Output, ScaleOutcome) {
        match self {
            Self::Fixed(value) => (value, ScaleOutcome::Fixed),
            Self::Linear(value) => match linear_scale(value.clone(), target.factor()) {
                Ok(v) => (v, ScaleOutcome::Scaled),
                Err(e) => (value, ScaleOutcome::Error(e)),
            },
            Self::ByServings(ref values) => {
                if let Some(index) = target.index {
                    let value = match values.get(index) {
                        Some(v) => v,
                        None => {
                            let value = self.clone();
                            return (
                                self.default_scale(),
                                ScaleOutcome::Error(ScaleError::NotDefined { target, value }),
                            );
                        }
                    };
                    (value.clone(), ScaleOutcome::Scaled)
                } else {
                    let value = self.clone();
                    (
                        self.default_scale(),
                        ScaleOutcome::Error(ScaleError::NotScalable {
                            value,
                            reason:
                                "tried to scale a value linearly when it has the scaling defined",
                        }),
                    )
                }
            }
        }
    }

    fn default_scale(self) -> Self::Output {
        match self {
            Self::Fixed(value) => value,
            Self::Linear(value) => value,
            Self::ByServings(values) => values
                .first()
                .expect("scalable value servings list empty")
                .clone(),
        }
    }
}

fn linear_scale(value: Value, factor: f64) -> Result<Value, ScaleError> {
    match value {
        Value::Number(n) => Ok(Value::Number((n.value() * factor).into())),
        Value::Range { start, end } => {
            let start = (start.value() * factor).into();
            let end = (end.value() * factor).into();
            Ok(Value::Range { start, end })
        }
        v @ Value::Text(_) => Err(TextValueError(v).into()),
    }
}

impl Scale for ScalableQuantity {
    type Output = ScaledQuantity;

    fn scale(self, target: ScaleTarget) -> (Self::Output, ScaleOutcome) {
        let Self { value, unit } = self;
        let (value, outcome) = value.scale(target);
        let scaled = ScaledQuantity { value, unit };
        (scaled, outcome)
    }

    fn default_scale(self) -> Self::Output {
        let Self { value, unit } = self;
        Self::Output {
            value: value.default_scale(),
            unit,
        }
    }
}

impl Scale for Ingredient<ScalableValue> {
    type Output = Ingredient<Value>;

    fn scale(self, target: ScaleTarget) -> (Self::Output, ScaleOutcome) {
        let (quantity, outcome) = self.quantity.map(|q| q.scale(target)).unzip();
        let outcome = outcome.unwrap_or(ScaleOutcome::NoQuantity);
        let scaled = Ingredient {
            name: self.name,
            alias: self.alias,
            quantity,
            note: self.note,
            relation: self.relation,
            modifiers: self.modifiers,
            defined_in_step: self.defined_in_step,
        };
        (scaled, outcome)
    }

    fn default_scale(self) -> Self::Output {
        Ingredient {
            name: self.name,
            alias: self.alias,
            quantity: self.quantity.map(Quantity::default_scale),
            note: self.note,
            relation: self.relation,
            modifiers: self.modifiers,
            defined_in_step: self.defined_in_step,
        }
    }
}

impl Scale for Cookware<ScalableValue> {
    type Output = Cookware<Value>;

    fn scale(self, target: ScaleTarget) -> (Self::Output, ScaleOutcome) {
        let (quantity, outcome) = self.quantity.map(|q| q.scale(target)).unzip();
        let outcome = outcome.unwrap_or(ScaleOutcome::NoQuantity);
        let scaled = Cookware {
            name: self.name,
            alias: self.alias,
            quantity,
            note: self.note,
            relation: self.relation,
            modifiers: self.modifiers,
        };
        (scaled, outcome)
    }

    fn default_scale(self) -> Self::Output {
        Cookware {
            name: self.name,
            alias: self.alias,
            quantity: self.quantity.map(ScalableValue::default_scale),
            note: self.note,
            relation: self.relation,
            modifiers: self.modifiers,
        }
    }
}

impl Scale for Timer<ScalableValue> {
    type Output = Timer<Value>;

    fn scale(self, target: ScaleTarget) -> (Self::Output, ScaleOutcome) {
        let (quantity, outcome) = self.quantity.map(|q| q.scale(target)).unzip();
        let outcome = outcome.unwrap_or(ScaleOutcome::NoQuantity);
        let scaled = Timer {
            name: self.name,
            quantity,
        };
        (scaled, outcome)
    }

    fn default_scale(self) -> Self::Output {
        Timer {
            name: self.name,
            quantity: self.quantity.map(Quantity::default_scale),
        }
    }
}

impl ScaledRecipe {
    /// Get the [`ScaledData`] from a recipe after scaling.
    ///
    /// Returns [`None`] if it was [`default scaled`](ScalableRecipe::default_scale).
    pub fn scaled_data(&self) -> Option<&ScaledData> {
        if let Scaled::Scaled(data) = &self.data {
            Some(data)
        } else {
            None
        }
    }

    /// Shorthand to check if [`Self::scaled_data`] is [`Scaled::DefaultScaling`].
    pub fn is_default_scaled(&self) -> bool {
        matches!(self.data, Scaled::DefaultScaling)
    }
}