Skip to content

Commit

Permalink
Make articleid a String, like Vereinsflieger defines it
Browse files Browse the repository at this point in the history
  • Loading branch information
zargony committed Oct 29, 2024
1 parent f497c80 commit d4d3f03
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion firmware/config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"vf-cid": 0,

// Vereinsflieger article id to use for purchases
"vf-article-id": 0
"vf-article-id": "1234"
}
15 changes: 10 additions & 5 deletions firmware/src/article.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use alloc::string::String;
/// Article id
/// Equivalent to the Vereinsflieger `articleid` attribute
#[allow(clippy::module_name_repetitions)]
pub type ArticleId = u32;
pub type ArticleId = String;

/// Article information
#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -38,8 +38,8 @@ impl<const N: usize> Articles<N> {
}

/// Update article with given article id. Ignores article ids not in list.
pub fn update(&mut self, id: ArticleId, name: String, price: f32) {
if let Some(idx) = self.ids.iter().position(|i| *i == id) {
pub fn update(&mut self, id: &ArticleId, name: String, price: f32) {
if let Some(idx) = self.find(id) {
self.articles[idx] = Some(Article { name, price });
}
}
Expand All @@ -49,10 +49,15 @@ impl<const N: usize> Articles<N> {
self.articles.iter().filter(|a| a.is_some()).count()
}

/// Find index of article with given id
pub fn find(&self, id: &ArticleId) -> Option<usize> {
self.ids.iter().position(|i| i == id)
}

/// Look up id of article at given index
#[allow(dead_code)]
pub fn id(&self, index: usize) -> Option<ArticleId> {
self.ids.get(index).copied()
pub fn id(&self, index: usize) -> Option<&ArticleId> {
self.ids.get(index)
}

/// Look up article information at given index
Expand Down
6 changes: 3 additions & 3 deletions firmware/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ impl<'a, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, I2C, IRQ> {
let _user = self.users.get(userid);
// Ask for number of drinks
let num_drinks = self.get_number_of_drinks().await?;
// Get article price
let price = self.articles.get(0).ok_or(Error::ArticleNotFound)?.price;
// Get article information
let article = self.articles.get(0).ok_or(Error::ArticleNotFound)?;
// Calculate total price. It's ok to cast num_drinks to f32 as it's always a small number.
#[allow(clippy::cast_precision_loss)]
let total_price = price * num_drinks as f32;
let total_price = article.price * num_drinks as f32;
// Show total price and ask for confirmation
self.confirm_purchase(num_drinks, total_price).await?;

Expand Down
6 changes: 3 additions & 3 deletions firmware/src/vereinsflieger/proto_articles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<const N: usize> FromJsonObject for ArticleListResponse<N> {
// articles directly to the article lookup table and only keeps the articles
// needed, which heavily reduces memory consumption.
let mut articles = context.borrow_mut();
articles.update(article.articleid, article.designation.clone(), price);
articles.update(&article.articleid, article.designation, price);
} else {
warn!(
"Ignoring article with no valid price ({}): {}",
Expand All @@ -74,7 +74,7 @@ impl<const N: usize> FromJsonObject for ArticleListResponse<N> {
/// Article
#[derive(Debug, Default)]
pub struct Article {
pub articleid: u32,
pub articleid: String,
pub designation: String,
pub unittype: String,
pub prices: Vec<ArticlePrice>,
Expand All @@ -90,7 +90,7 @@ impl FromJsonObject for Article {
_context: &Self::Context<'_>,
) -> Result<(), json::Error<R::Error>> {
match &*key {
"articleid" => self.articleid = json.read_any().await?.try_into()?,
"articleid" => self.articleid = json.read().await?,
"designation" => self.designation = json.read().await?,
"unittype" => self.unittype = json.read().await?,
"prices" => self.prices = json.read().await?,
Expand Down

0 comments on commit d4d3f03

Please sign in to comment.