Skip to content

Commit

Permalink
Fix item names in rust (related to #50) (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
lollek authored Jan 17, 2025
1 parent 0c0c98b commit 3bd9fd7
Show file tree
Hide file tree
Showing 35 changed files with 1,882 additions and 276 deletions.
96 changes: 85 additions & 11 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pancurses = "0.16"
enum-iterator = "1.1"
backtrace = "0.3.66"

[dev-dependencies]
serial_test = "0.4.0"

[lib]
name = "omoria"
crate-type = ["staticlib"]
Expand Down
11 changes: 11 additions & 0 deletions src/conversion/item_subtype.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::TryInto;

use crate::model::{item_subtype::ItemSubType, ItemType};

pub mod amulet;
Expand Down Expand Up @@ -161,3 +163,12 @@ pub fn from_usize(item_type: ItemType, item_subtype: usize) -> Option<ItemSubTyp
_ => panic!("Unhandled item type {:?}", item_type),
}
}

pub fn from_i64(item_type: ItemType, item_subtype: i64) -> Option<ItemSubType> {
from_usize(
item_type,
item_subtype
.try_into()
.unwrap_or_else(|err| panic!("Failed to convert i64 to usize: {}", err)),
)
}
3 changes: 3 additions & 0 deletions src/data/item_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ mod main;

pub(crate) mod helpers;
pub(crate) mod subtypes;

#[cfg(test)]
mod test;
69 changes: 68 additions & 1 deletion src/data/item_name/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::borrow::Cow;

use crate::model::Item;
use crate::{misc, model::Item};

/**
* Returns the number of the given item. 1 returns an empty string
*/
pub(crate) fn number_of<'a>(item: &Item) -> Cow<'a, str> {
match item.number {
0 => Cow::from("no more "),
Expand All @@ -10,6 +13,70 @@ pub(crate) fn number_of<'a>(item: &Item) -> Cow<'a, str> {
}
}

/**
* Returns the number of the given item, including 1 if number is 1
*/
pub(crate) fn full_number_of<'a>(item: &Item) -> Cow<'a, str> {
match item.number {
0 => Cow::from("no more "),
_ => Cow::from(item.number.to_string() + " "),
}
}

/**
* Returns if there are no items left, or empty string
*/
pub(crate) fn no_more<'a>(item: &Item) -> Cow<'a, str> {
match item.number {
0 => Cow::from("no more "),
_ => Cow::from(""),
}
}

pub(crate) fn p1_plural_s<'a>(item: &Item) -> Cow<'a, str> {
if item.p1 == 1 {
Cow::Borrowed("")
} else {
Cow::Borrowed("s")
}
}

pub(crate) fn plural_s<'a>(item: &Item) -> Cow<'a, str> {
if item.number == 1 {
Cow::Borrowed("")
} else {
Cow::Borrowed("s")
}
}

pub(crate) fn plural_es<'a>(item: &Item) -> Cow<'a, str> {
if item.number == 1 {
Cow::Borrowed("")
} else {
Cow::Borrowed("es")
}
}

pub(crate) fn damage<'a>(item: &Item) -> Cow<'a, str> {
let raw_string = item.damage.iter().map(|&i| i as u8).collect::<Vec<u8>>();
let damage_string = misc::c_array_to_rust_string(raw_string);
Cow::from(format!(" ({})", damage_string))
}

pub(crate) fn attack_bonus<'a>(item: &Item) -> Cow<'a, str> {
let tohit_sign = if item.tohit > 0 { "+" } else { "" };
let todam_sign = if item.todam > 0 { "+" } else { "" };
Cow::from(format!(
" ({}{},{}{})",
tohit_sign, item.tohit, todam_sign, item.todam
))
}

pub(crate) fn p1_bonus<'a>(item: &Item) -> Cow<'a, str> {
let p1_sign = if item.p1 > 0 { "+" } else { "" };
Cow::from(format!(" ({}{})", p1_sign, item.p1))
}

#[cfg(test)]
mod test {

Expand Down
Loading

0 comments on commit 3bd9fd7

Please sign in to comment.