diff --git a/Cargo.lock b/Cargo.lock index 26b5d5e..44d1da0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -590,7 +590,7 @@ dependencies = [ [[package]] name = "rusistor" -version = "0.3.0" +version = "0.3.1" [[package]] name = "rustc-demangle" diff --git a/rusistor/Cargo.toml b/rusistor/Cargo.toml index 850f378..6b79521 100644 --- a/rusistor/Cargo.toml +++ b/rusistor/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rusistor" -version = "0.3.0" +version = "0.3.1" authors.workspace = true license.workspace = true edition.workspace = true diff --git a/rusistor/src/lib.rs b/rusistor/src/lib.rs index 8bc1fe0..59a5609 100644 --- a/rusistor/src/lib.rs +++ b/rusistor/src/lib.rs @@ -147,6 +147,27 @@ impl From for Color { } } +impl From for Color { + fn from(value: usize) -> Self { + match value { + 0 => Color::Black, + 1 => Color::Brown, + 2 => Color::Red, + 3 => Color::Orange, + 4 => Color::Yellow, + 5 => Color::Green, + 6 => Color::Blue, + 7 => Color::Violet, + 8 => Color::Grey, + 9 => Color::White, + 10 => Color::Gold, + 11 => Color::Silver, + 12 => Color::Pink, + _ => panic!("invalid value {} given to Color::from", value), + } + } +} + impl Display for Color { fn fmt(&self, f: &mut Formatter) -> fmt::Result { let s = match self { diff --git a/tusistor-core/src/update.rs b/tusistor-core/src/update.rs index 2a024c8..7d75710 100644 --- a/tusistor-core/src/update.rs +++ b/tusistor-core/src/update.rs @@ -1,5 +1,3 @@ -use rusistor::{self}; - pub enum ColorCodesMsg { ThreeBands, FourBands, @@ -10,23 +8,3 @@ pub enum ColorCodesMsg { NextColor, PrevColor, } - -// todo move to rusistor -pub fn index_to_color(idx: usize) -> rusistor::Color { - match idx { - 0 => rusistor::Color::Black, - 1 => rusistor::Color::Brown, - 2 => rusistor::Color::Red, - 3 => rusistor::Color::Orange, - 4 => rusistor::Color::Yellow, - 5 => rusistor::Color::Green, - 6 => rusistor::Color::Blue, - 7 => rusistor::Color::Violet, - 8 => rusistor::Color::Grey, - 9 => rusistor::Color::White, - 10 => rusistor::Color::Gold, - 11 => rusistor::Color::Silver, - 12 => rusistor::Color::Pink, - _ => panic!("unknown color"), - } -} diff --git a/tusistor-web/Cargo.toml b/tusistor-web/Cargo.toml index 4db894b..baacc86 100644 --- a/tusistor-web/Cargo.toml +++ b/tusistor-web/Cargo.toml @@ -8,7 +8,7 @@ repository.workspace = true description = "This is a Ratzilla app to calculate the color code of electrical resistors." [dependencies] -rusistor = { path = "../rusistor", version = "0.3.0" } +rusistor = { path = "../rusistor", version = "0.3.1" } tusistor-core = { path = "../tusistor-core", version = "0.1.0" } engineering-repr = "1.1.0" ratzilla = "0.0.2" diff --git a/tusistor-web/src/update.rs b/tusistor-web/src/update.rs index a84ee58..c2f3de0 100644 --- a/tusistor-web/src/update.rs +++ b/tusistor-web/src/update.rs @@ -1,7 +1,7 @@ use ratzilla::event; -use rusistor::{self, Resistor}; +use rusistor::{self, Color, Resistor}; use tusistor_core::model::ColorCodesToSpecsModel; -use tusistor_core::update::{ColorCodesMsg, index_to_color}; +use tusistor_core::update::ColorCodesMsg; pub fn handle_event(model: &mut ColorCodesToSpecsModel, event: ratzilla::event::KeyEvent) { match event.code { @@ -70,7 +70,7 @@ pub fn update(model: &mut ColorCodesToSpecsModel, msg: ColorCodesMsg) { let mut resistor = Err("".to_string()); while resistor.is_err() { i += 1; - let next_color = index_to_color((current_idx + i) % 13); + let next_color = Color::from((current_idx + i) % 13); resistor = model.resistor.with_color(next_color, model.selected_band); } model.resistor = resistor.unwrap(); @@ -81,7 +81,7 @@ pub fn update(model: &mut ColorCodesToSpecsModel, msg: ColorCodesMsg) { let mut resistor = Err("".to_string()); while resistor.is_err() { i -= 1; - let next_color = index_to_color((current_idx + i) % 13); + let next_color = Color::from((current_idx + i) % 13); resistor = model.resistor.with_color(next_color, model.selected_band); } model.resistor = resistor.unwrap(); diff --git a/tusistor/Cargo.toml b/tusistor/Cargo.toml index 05b08fb..db1d024 100644 --- a/tusistor/Cargo.toml +++ b/tusistor/Cargo.toml @@ -12,6 +12,6 @@ crossterm = "0.28.1" ratatui = "0.29.0" color-eyre = "0.6.3" tui-input = "0.11.1" -rusistor = { path = "../rusistor", version = "0.3.0" } +rusistor = { path = "../rusistor", version = "0.3.1" } tusistor-core = { path = "../tusistor-core", version = "0.1.0" } engineering-repr = "1.1.0" diff --git a/tusistor/src/update.rs b/tusistor/src/update.rs index a17b0e0..f1a5df1 100644 --- a/tusistor/src/update.rs +++ b/tusistor/src/update.rs @@ -1,10 +1,10 @@ use crate::model::{InputFocus, Model, SelectedTab}; use color_eyre::Result; use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers}; -use rusistor::{self, Resistor}; +use rusistor::{Color, Resistor}; use std::str::FromStr; use tui_input::backend::crossterm::EventHandler; -use tusistor_core::update::{ColorCodesMsg, index_to_color}; +use tusistor_core::update::ColorCodesMsg; pub enum SpecsMsg { Determine, @@ -153,9 +153,9 @@ pub fn update(model: &mut Model, msg: Msg) { msg: ColorCodesMsg::ThreeBands, } => { model.color_codes_to_specs.resistor = Resistor::ThreeBand { - band1: rusistor::Color::Brown, - band2: rusistor::Color::Black, - band3: rusistor::Color::Black, + band1: Color::Brown, + band2: Color::Black, + band3: Color::Black, }; model.color_codes_to_specs.selected_band = model.color_codes_to_specs.selected_band.min(2) @@ -164,10 +164,10 @@ pub fn update(model: &mut Model, msg: Msg) { msg: ColorCodesMsg::FourBands, } => { model.color_codes_to_specs.resistor = Resistor::FourBand { - band1: rusistor::Color::Brown, - band2: rusistor::Color::Black, - band3: rusistor::Color::Black, - band4: rusistor::Color::Brown, + band1: Color::Brown, + band2: Color::Black, + band3: Color::Black, + band4: Color::Brown, }; model.color_codes_to_specs.selected_band = model.color_codes_to_specs.selected_band.min(3) @@ -176,11 +176,11 @@ pub fn update(model: &mut Model, msg: Msg) { msg: ColorCodesMsg::FiveBands, } => { model.color_codes_to_specs.resistor = Resistor::FiveBand { - band1: rusistor::Color::Brown, - band2: rusistor::Color::Black, - band3: rusistor::Color::Black, - band4: rusistor::Color::Black, - band5: rusistor::Color::Brown, + band1: Color::Brown, + band2: Color::Black, + band3: Color::Black, + band4: Color::Black, + band5: Color::Brown, }; model.color_codes_to_specs.selected_band = model.color_codes_to_specs.selected_band.min(4) @@ -189,12 +189,12 @@ pub fn update(model: &mut Model, msg: Msg) { msg: ColorCodesMsg::SixBands, } => { model.color_codes_to_specs.resistor = Resistor::SixBand { - band1: rusistor::Color::Brown, - band2: rusistor::Color::Black, - band3: rusistor::Color::Black, - band4: rusistor::Color::Black, - band5: rusistor::Color::Brown, - band6: rusistor::Color::Black, + band1: Color::Brown, + band2: Color::Black, + band3: Color::Black, + band4: Color::Black, + band5: Color::Brown, + band6: Color::Black, }; model.color_codes_to_specs.selected_band = model.color_codes_to_specs.selected_band.min(5) @@ -223,7 +223,7 @@ pub fn update(model: &mut Model, msg: Msg) { let mut resistor = Err("".to_string()); while resistor.is_err() { i += 1; - let next_color = index_to_color((current_idx + i) % 13); + let next_color = Color::from((current_idx + i) % 13); resistor = model .color_codes_to_specs .resistor @@ -240,7 +240,7 @@ pub fn update(model: &mut Model, msg: Msg) { let mut resistor = Err("".to_string()); while resistor.is_err() { i -= 1; - let next_color = index_to_color((current_idx + i) % 13); + let next_color = Color::from((current_idx + i) % 13); resistor = model .color_codes_to_specs .resistor