Skip to content

Commit

Permalink
move index_to_color to rusistor crate
Browse files Browse the repository at this point in the history
  • Loading branch information
dawedawe committed Mar 2, 2025
1 parent 67cc0eb commit ebc440a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rusistor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rusistor"
version = "0.3.0"
version = "0.3.1"
authors.workspace = true
license.workspace = true
edition.workspace = true
Expand Down
21 changes: 21 additions & 0 deletions rusistor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,27 @@ impl From<i32> for Color {
}
}

impl From<usize> 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 {
Expand Down
22 changes: 0 additions & 22 deletions tusistor-core/src/update.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use rusistor::{self};

pub enum ColorCodesMsg {
ThreeBands,
FourBands,
Expand All @@ -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"),
}
}
2 changes: 1 addition & 1 deletion tusistor-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
8 changes: 4 additions & 4 deletions tusistor-web/src/update.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tusistor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
44 changes: 22 additions & 22 deletions tusistor/src/update.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ebc440a

Please sign in to comment.