Решение на CSS Цветове от Станислав Христов

Обратно към всички решения

Към профила на Станислав Христов

Резултати

  • 12 точки от тестове
  • 0 бонус точки
  • 12 точки общо
  • 3 успешни тест(а)
  • 2 неуспешни тест(а)

Код

pub const COLOR_MAX: u8 = 255;
pub const HUE_MAX: u16 = 360;
pub const PERCENT_MAX: u8 = 100;
pub enum Color {
RGB {
red: u8,
green: u8,
blue: u8
},
HSV {
hue: u16,
saturation: u8,
value: u8,
}
}
impl Color {
pub fn new_rgb(red: u8, green: u8, blue: u8) -> Color {
Color::RGB {
red,
green,
blue
}
}
pub fn new_hsv(hue: u16, saturation: u8, value: u8) -> Color {
if hue > 360 {
panic!("Hue must be below 360 degress. Current hue passed is {}", hue);
}
if saturation > 100 {
panic!("Saturation must be below a 100. Current saturation passed is {}", saturation);
}
Color::HSV {
hue,
saturation,
value
}
}
}
impl Color {
pub fn unwrap_rgb(&self) -> (u8, u8, u8) {
if let Color::RGB{red, green, blue} = self {
(*red, *green, *blue)
} else {
panic!("Self isn't RGB :(");
}
}
pub fn unwrap_hsv(&self) -> (u16, u8, u8) {
if let Color::HSV{hue, saturation, value} = self {
(*hue, *saturation, *value)
} else {
panic!("Self isn't HSV >:^(");
}
}
}
impl Color {
pub fn to_string(&self) -> String {
match self {
Color::RGB{ red, green, blue } => {
format!("#{:0>2x}{:0>2x}{:0>2x}", red, green, blue)
},
Color::HSV{ hue, saturation, value } => {
format!("hsv({}, {}%, {}%)", hue, saturation, value)
},
_ => { panic!("Invalid type"); }

Няма как тази паника да се случи -- Rust ще провери compile-time че си покрил всички възможни случаи. Ако видиш warning-ите, компилатора ти казва, че до този клон няма как да се стигне.

}
}
}
impl Color {
pub fn invert(&self) -> Self {
match self {
Color::RGB{ red, green, blue } => {
Color::RGB {
red: COLOR_MAX - red,
green: COLOR_MAX - green,
blue: COLOR_MAX - blue
}
},
Color::HSV {hue, saturation, value } => {
Color::HSV {
hue: HUE_MAX - hue,
saturation: PERCENT_MAX - saturation,
value: PERCENT_MAX - value
}
},
_ => { panic!("Invalid type"); }
}
}
}

Лог от изпълнението

Compiling solution v0.1.0 (/tmp/d20230111-3772066-tn0se8/solution)
warning: unreachable pattern
  --> src/lib.rs:71:10
   |
71 |          _ => { panic!("Invalid type"); }
   |          ^
   |
   = note: `#[warn(unreachable_patterns)]` on by default

warning: unreachable pattern
  --> src/lib.rs:93:10
   |
93 |          _ => { panic!("Invalid type"); }
   |          ^

warning: `solution` (lib) generated 2 warnings
    Finished test [unoptimized + debuginfo] target(s) in 0.71s
     Running tests/solution_test.rs (target/debug/deps/solution_test-0edbea2040daef01)

running 5 tests
test solution_test::test_invert_hsv ... ok
test solution_test::test_invert_rgb ... ok
test solution_test::test_hsv_display ... FAILED
test solution_test::test_rgb_display ... ok
test solution_test::test_new_hsv ... FAILED

failures:

---- solution_test::test_hsv_display stdout ----
thread 'solution_test::test_hsv_display' panicked at 'assertion failed: `(left == right)`
  left: `"hsv(0, 0%, 0%)"`,
 right: `"hsv(0,0%,0%)"`', tests/solution_test.rs:15:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- solution_test::test_new_hsv stdout ----
thread 'solution_test::test_new_hsv' panicked at 'Hue must be below 360 degress. Current hue passed is 361', src/lib.rs:29:13
thread 'solution_test::test_new_hsv' panicked at 'Saturation must be below a 100. Current saturation passed is 200', src/lib.rs:33:13
thread 'solution_test::test_new_hsv' panicked at 'assertion failed: catch_unwind(|| Color::new_hsv(200, 100, 255)).is_err()', tests/solution_test.rs:24:5


failures:
    solution_test::test_hsv_display
    solution_test::test_new_hsv

test result: FAILED. 3 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass `--test solution_test`

История (1 версия и 3 коментара)

Станислав качи първо решение на 24.10.2022 13:31 (преди почти 3 години)