morganlinton/BeautifulRust / skills/rust-review
rust-review
UnverifiedReview existing Rust code for idiom, API design, error handling, safety, and maintainability, and produce a prioritized report where every finding cites the rule it breaks and gives a concrete fix. Use this whenever the user asks to review, audit, critique, grade, or "check" Rust code, a crate, a module, a .rs file, a diff, or a pull request, or asks what is wrong with their Rust or how to make it more idiomatic, even if they don't say "review". Pairs with the rust-best-practices skill, which covers writing new code.
evals/review-inventory-module/files/inventory.rs
View on GitHub#![deny(warnings)]
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
#[derive(Debug, Clone)]
pub enum InventoryError {
NotFound,
Invalid,
Io,
Parse,
Other(String),
}
#[derive(Clone)]
pub struct Item {
pub name: String,
pub qty: i64,
pub price_cents: i64,
pub is_perishable: bool,
}
pub struct Inventory {
items: Rc<RefCell<HashMap<String, Item>>>,
}
impl Inventory {
pub fn new() -> Rc<RefCell<Inventory>> {
Rc::new(RefCell::new(Inventory {
items: Rc::new(RefCell::new(HashMap::new())),
}))
}
pub fn get_item(&self, name: &String) -> Option<Item> {
if self.items.borrow().get(name).is_some() {
Some(self.items.borrow().get(name).unwrap().clone())
} else {
None
}
}
pub fn add_item(&self, name: String, qty: i64, price: i64, perishable: bool) -> Result<(), InventoryError> {
if qty < 0 || price < 0 {
return Err(InventoryError::Invalid);
}
let item = Item { name: name.clone(), qty: qty, price_cents: price, is_perishable: perishable };
self.items.borrow_mut().insert(name, item);
Ok(())
}
pub fn total_value(&self, names: &Vec<String>) -> i64 {
let mut total = 0;
for n in names {
let item = self.items.borrow().get(n).unwrap().clone();
total += item.qty * item.price_cents;
}
total
}
pub fn restock(&self, name: &String, amount: i64) -> Result<(), InventoryError> {
if self.items.borrow().get(name).is_some() {
let mut item = self.items.borrow().get(name).unwrap().clone();
item.qty += amount;
self.items.borrow_mut().insert(name.clone(), item);
Ok(())
} else {
Err(InventoryError::NotFound)
}
}
pub fn parse_line(line: &String) -> Result<Item, InventoryError> {
let parts: Vec<&str> = line.split(',').collect();
if parts.len() != 4 {
return Err(InventoryError::Parse);
}
let qty = parts[1].trim().parse::<i64>().unwrap();
let price = parts[2].trim().parse::<i64>().unwrap();
let perishable = if parts[3].trim() == "true" { true } else { false };
Ok(Item { name: parts[0].trim().to_string(), qty: qty, price_cents: price, is_perishable: perishable })
}
pub fn load(path: &String) -> Result<Rc<RefCell<Inventory>>, InventoryError> {
let text = std::fs::read_to_string(path).unwrap();
let inv = Inventory::new();
for line in text.lines() {
if line.trim().len() == 0 {
continue;
}
let item = Inventory::parse_line(&line.to_string())?;
inv.borrow().items.borrow_mut().insert(item.name.clone(), item);
}
Ok(inv)
}
pub fn report(&self, verbose: bool) -> String {
let mut s = String::new();
for (k, v) in self.items.borrow().iter() {
s = s + &k + ": " + &v.qty.to_string() + "\n";
if verbose {
s = s + " price=" + &v.price_cents.to_string() + " perishable=" + &v.is_perishable.to_string() + "\n";
}
}
s
}
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |