This repository has been archived on 2026-03-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
mmtui/src/row.rs

95 lines
2.6 KiB
Rust
Raw Normal View History

pub struct Row {
2026-03-03 14:51:37 -06:00
pub name: String,
pub hut_type: String,
pub path: String,
pub tare: f64,
pub mat_id: String,
pub quantity: f64,
pub lot_id: String,
pub uom: String,
pub lot_status: String,
pub born: chrono::DateTime<chrono::Local>,
pub expire: chrono::DateTime<chrono::Local>,
}
impl Row {
2026-03-03 14:51:37 -06:00
pub const DF: &'static str = "%Y-%m-%d %H:%M";
2026-03-03 14:17:02 -06:00
pub fn new(
name: &str,
hut_type: &str,
path: &str,
tare: f64,
mat_id: &str,
quantity: f64,
uom: &str,
lot_status: &str,
checksum: bool,
) -> Self {
let born = chrono::Local::now();
let expire = born
.checked_add_days(chrono::Days::new(365))
.expect("A valid date time");
2026-03-03 14:51:37 -06:00
let name = if checksum {
make_checksum()
} else {
name.to_ascii_uppercase()
};
Self {
name: name.clone(),
hut_type: hut_type.to_ascii_uppercase(),
path: path.to_ascii_uppercase(),
tare,
mat_id: mat_id.to_ascii_uppercase(),
quantity,
lot_id: format!("{}-{}", name, mat_id).to_ascii_uppercase(),
uom: uom.to_ascii_uppercase(),
lot_status: lot_status.to_ascii_uppercase(),
born,
expire,
}
}
}
2026-03-03 14:51:37 -06:00
pub fn make_checksum() -> String {
let base = rand::random_range(10000..99999u32).to_string();
2026-03-03 14:26:22 -06:00
let split_expect = "a valid character in the base checksum";
let split = format!(
"{}{}{}{}{}",
2026-03-03 14:26:22 -06:00
base.chars().next().expect(split_expect),
base.chars().nth(2).expect(split_expect),
base.chars().nth(4).expect(split_expect),
base.chars().nth(1).expect(split_expect),
base.chars().nth(3).expect(split_expect)
)
.repeat(2);
let sum: u32 = split
.chars()
.map(|c| c.to_digit(10).expect("a valid integer value"))
.sum();
let last = (10 - (sum % 10)) % 10;
format!("{}{}", base, last)
}
2026-03-03 14:17:02 -06:00
impl std::fmt::Display for Row {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{},{},{},{},{},N,{},{},{},{},{},{},{},{}",
self.name,
self.name,
self.hut_type,
self.path,
self.tare,
self.mat_id,
self.quantity,
self.lot_id,
self.uom,
self.lot_status,
self.born.format(Self::DF),
self.born.format(Self::DF),
self.expire.format(Self::DF),
)
}
}