Review generated catalog API

from Modules and crates
Rust 1.98 advanced 6 min 4 issues to find

Review this generated catalog library API.

Expose Product at the crate root without exposing its implementation module, accept only a non-empty SKU and positive cents, return malformed input as Result without panic, avoid a temporary field collection, and quote by borrowing with checked arithmetic.

Rust
pub mod internal {
    pub struct Product {
        pub sku: String,
        pub cents: u32,
    }
}

use internal::Product;

pub fn parse_product(line: &str) -> Product {
    let fields: Vec<&str> = line.split(',').collect();
    Product {
        sku: fields[0].to_string(),
        cents: fields[1].parse().unwrap(),
    }
}

pub fn quote(product: &Product, count: u32) -> Option<u32> {
    product.cents.checked_mul(count)
}

generated code is illustrative, not from any one model

Open in playground
Report an error