From 229270f837487629e740564fab7052805d6f9835 Mon Sep 17 00:00:00 2001 From: Ben Lovy Date: Sun, 3 Jan 2021 11:04:41 -0500 Subject: [PATCH] Item file --- Cargo.toml | 17 ++++++++++++++- src/bin/fcalc.rs | 8 +++++-- src/error.rs | 13 ++++++++++++ src/item.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ src/items.toml | 10 +++++++++ src/lib.rs | 17 +++++++++------ src/recipe.rs | 3 +++ 7 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 src/error.rs create mode 100644 src/item.rs create mode 100644 src/items.toml create mode 100644 src/recipe.rs diff --git a/Cargo.toml b/Cargo.toml index 879b98c..b18d41b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,21 @@ authors = ["Ben Lovy "] edition = "2018" license = "BSD-3-Clause" +[features] +default = ["bin"] + +bin = [] # maybe structopt here + [dependencies] -lazy_static = "1.4" \ No newline at end of file +lazy_static = "1.4" +thiserror = "1.0" +toml = "0.5" + +[dependencies.serde] +version = "1.0" +features = ["derive"] + +[dev-dependencies] + +pretty_assertions = "0.6" \ No newline at end of file diff --git a/src/bin/fcalc.rs b/src/bin/fcalc.rs index 63a7fca..2bb9bd8 100644 --- a/src/bin/fcalc.rs +++ b/src/bin/fcalc.rs @@ -1,5 +1,9 @@ -use std::env::var; +//! Simple CLI to request recipes. + +#![cfg(feature = "bin")] + use lazy_static::lazy_static; +use std::env::var; lazy_static! { pub static ref VERSION: String = var("CARGO_PKG_VERSION").unwrap(); @@ -7,4 +11,4 @@ lazy_static! { fn main() { println!("fcalc {} - Factorio Calculator", *VERSION); -} \ No newline at end of file +} diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..26352fe --- /dev/null +++ b/src/error.rs @@ -0,0 +1,13 @@ +//! Top-level error struct + +use std::io; +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum FcalcError { + #[error("Could not read input data file")] + FileError(#[from] io::Error), +} + +/// Export convenient Result type carrying this error. +pub type Result = std::result::Result; diff --git a/src/item.rs b/src/item.rs new file mode 100644 index 0000000..39270aa --- /dev/null +++ b/src/item.rs @@ -0,0 +1,55 @@ +//! An item is something the player can hold. Some are raw, some are craftable. + +use lazy_static::lazy_static; +use serde::Deserialize; + +lazy_static! { + pub static ref ITEMS: Items = Items::new(); +} + +// /// All items, raw or otherwise, implement this trait. +// // TODO is this even necessary? +// pub trait Item: Display {} + +/// A base-level raw material, not craftable. +#[derive(Debug, Deserialize)] +pub struct RawMaterial { + name: String, +} + +#[derive(Debug, Deserialize)] +pub struct Product { + name: String, +} + +/// The top-level Items struct, containing all supported items read from TOML. +#[derive(Debug, Deserialize)] +pub struct Items { + raw_materials: Vec, + products: Vec, +} + +impl Items { + fn new() -> Self { + Self::default() + } +} + +impl Default for Items { + fn default() -> Self { + let input = include_str!("items.toml"); + toml::from_str(input).expect("Could not read items file") + } +} + +#[cfg(test)] +mod test { + use super::*; + use pretty_assertions::assert_eq; + #[test] + fn it_loads_all_items() { + let items = Items::new(); + assert_eq!(items.raw_materials.len(), 2); + assert_eq!(items.products.len(), 1); + } +} diff --git a/src/items.toml b/src/items.toml new file mode 100644 index 0000000..1b1811d --- /dev/null +++ b/src/items.toml @@ -0,0 +1,10 @@ +# All the items supported + +[[raw_materials]] +name = "Coal" + +[[raw_materials]] +name = "Iron Ore" + +[[products]] +name = "Iron Plate" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index d3126d3..08006a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,11 @@ +//! fcalc is a library for calculating Factorio recipe inputs. +//! +//! +//! When compiled with the "bin" feature, sepcified by default, +//! it ships a CLI binary called `fcalc` for convenient usage. -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } -} +mod error; +mod item; +mod recipe; + +pub use error::{FcalcError, Result}; diff --git a/src/recipe.rs b/src/recipe.rs new file mode 100644 index 0000000..4d949c4 --- /dev/null +++ b/src/recipe.rs @@ -0,0 +1,3 @@ +//! A recipe tracks the cost to produce an item. +#[derive(Debug)] +pub struct Recipe; -- 2.38.5