Skip to content

Commit

Permalink
Merge pull request #7 from cooklang/fix-empty-amount-crash
Browse files Browse the repository at this point in the history
Implement missing branch for GroupQuantity addition
  • Loading branch information
dubadub authored Nov 2, 2023
2 parents 9d9ca7d + e51dd42 commit b91e82e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 37 deletions.
21 changes: 11 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions bindings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ This library exports methods:
Empty,
}

struct HardToNameWTF {
struct GroupedQuantityKey {
name: String,
unit_type: QuantityType,
}

type GroupedQuantity = HashMap<HardToNameWTF, Value>;
type GroupedQuantity = HashMap<GroupedQuantityKey, Value>;


### Shopping list usage example
Expand Down
24 changes: 12 additions & 12 deletions bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ dried oregano

#[test]
fn test_combine_ingredient_lists() {
use crate::{combine_ingredient_lists, HardToNameWTF, QuantityType, Value};
use crate::{combine_ingredient_lists, GroupedQuantityKey, QuantityType, Value};
use std::collections::HashMap;

let combined = combine_ingredient_lists(vec![
Expand All @@ -199,14 +199,14 @@ dried oregano
"salt".to_string(),
HashMap::from([
(
HardToNameWTF {
GroupedQuantityKey {
name: "g".to_string(),
unit_type: QuantityType::Number,
},
Value::Number { value: 5.0 },
),
(
HardToNameWTF {
GroupedQuantityKey {
name: "tsp".to_string(),
unit_type: QuantityType::Number,
},
Expand All @@ -218,14 +218,14 @@ dried oregano
"pepper".to_string(),
HashMap::from([
(
HardToNameWTF {
GroupedQuantityKey {
name: "mg".to_string(),
unit_type: QuantityType::Number,
},
Value::Number { value: 5.0 },
),
(
HardToNameWTF {
GroupedQuantityKey {
name: "tsp".to_string(),
unit_type: QuantityType::Number,
},
Expand All @@ -238,14 +238,14 @@ dried oregano
"salt".to_string(),
HashMap::from([
(
HardToNameWTF {
GroupedQuantityKey {
name: "kg".to_string(),
unit_type: QuantityType::Number,
},
Value::Number { value: 0.005 },
),
(
HardToNameWTF {
GroupedQuantityKey {
name: "tsp".to_string(),
unit_type: QuantityType::Number,
},
Expand All @@ -259,21 +259,21 @@ dried oregano
*combined.get("salt").unwrap(),
HashMap::from([
(
HardToNameWTF {
GroupedQuantityKey {
name: "kg".to_string(),
unit_type: QuantityType::Number
},
Value::Number { value: 0.005 }
),
(
HardToNameWTF {
GroupedQuantityKey {
name: "tsp".to_string(),
unit_type: QuantityType::Number
},
Value::Number { value: 2.0 }
),
(
HardToNameWTF {
GroupedQuantityKey {
name: "g".to_string(),
unit_type: QuantityType::Number
},
Expand All @@ -286,14 +286,14 @@ dried oregano
*combined.get("pepper").unwrap(),
HashMap::from([
(
HardToNameWTF {
GroupedQuantityKey {
name: "mg".to_string(),
unit_type: QuantityType::Number
},
Value::Number { value: 5.0 }
),
(
HardToNameWTF {
GroupedQuantityKey {
name: "tsp".to_string(),
unit_type: QuantityType::Number
},
Expand Down
24 changes: 11 additions & 13 deletions bindings/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,25 @@ pub(crate) fn into_group_quantity(amount: &Option<Amount>) -> GroupedQuantity {
let units = amount.units.as_ref().unwrap_or(&empty_units);

match &amount.quantity {
Value::Number { .. } => HardToNameWTF {
Value::Number { .. } => GroupedQuantityKey {
name: units.to_string(),
unit_type: QuantityType::Number,
},
Value::Range { .. } => HardToNameWTF {
Value::Range { .. } => GroupedQuantityKey {
name: units.to_string(),
unit_type: QuantityType::Range,
},
Value::Text { .. } => HardToNameWTF {
Value::Text { .. } => GroupedQuantityKey {
name: units.to_string(),
unit_type: QuantityType::Text,
},
Value::Empty => HardToNameWTF {
Value::Empty => GroupedQuantityKey {
name: units.to_string(),
unit_type: QuantityType::Empty,
},
}
} else {
HardToNameWTF {
GroupedQuantityKey {
name: empty_units,
unit_type: QuantityType::Empty,
}
Expand All @@ -105,12 +105,12 @@ pub enum QuantityType {
}

#[derive(uniffi::Record, Debug, Clone, Hash, Eq, PartialEq)]
pub struct HardToNameWTF {
pub struct GroupedQuantityKey {
pub name: String,
pub unit_type: QuantityType,
}

pub type GroupedQuantity = HashMap<HardToNameWTF, Value>;
pub type GroupedQuantity = HashMap<GroupedQuantityKey, Value>;

#[derive(uniffi::Record, Debug, Clone, PartialEq)]
pub struct Amount {
Expand Down Expand Up @@ -212,8 +212,8 @@ pub(crate) fn merge_grouped_quantities(left: &mut GroupedQuantity, right: &Group
// - no amount
//
// \
// |- <litre,Number> => 1.2
// |- <litre,Text> => half
// |- <litre,Number> => 1.2 litre
// |- <litre,Text> => half litre
// |- <,Text> => pinch
// |- <,Empty> => Some
//
Expand All @@ -222,7 +222,7 @@ pub(crate) fn merge_grouped_quantities(left: &mut GroupedQuantity, right: &Group

right.iter().for_each(|(key, value)| {
left
.entry(key.clone())
.entry(key.clone()) // isn't really necessary?
.and_modify(|v| {
match key.unit_type {
QuantityType::Number => {
Expand All @@ -245,9 +245,7 @@ pub(crate) fn merge_grouped_quantities(left: &mut GroupedQuantity, right: &Group

*stored += assignable;
},
QuantityType::Empty => {
todo!();
},
QuantityType::Empty => {}, // nothing is required to do, Some + Some = Some

}
})
Expand Down

0 comments on commit b91e82e

Please sign in to comment.