Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix item names in rust (related to #45) #47

Merged
merged 8 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
64 changes: 63 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::{model::Item, misc};

/**
* 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,65 @@ 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
))
}

#[cfg(test)]
mod test {

Expand Down
Loading