Решение на CSS Цветове от Клементина Картевска
Към профила на Клементина Картевска
Резултати
- 12 точки от тестове
- 0 бонус точки
- 12 точки общо
- 3 успешни тест(а)
- 2 неуспешни тест(а)
Код
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 {
return Color::RGB{ red:red, green: green, blue: blue};
}
pub fn new_hsv(hue: u16, saturation: u8, value: u8) -> Color {
return Color::HSV{hue: hue, saturation: saturation, value: value};
}
pub fn unwrap_rgb(&self) -> (u8, u8, u8) {
if let Color::RGB{red: red, green: green, blue: blue} = &self {
return (red.clone(), green.clone(), blue.clone());
}
else {
panic!("It is not in RGB format!");
}
}
pub fn unwrap_hsv(&self) -> (u16, u8, u8) {
if let Color::HSV{hue: hue, saturation: saturation, value: value} = &self {
return (hue.clone(), saturation.clone(), value.clone());
}
else {
panic!("It is not in HSV format!");
}
}
pub fn to_string(&self) -> String {
let result: String = "".to_string();
if let Color::HSV{hue: hue, saturation: saturation, value: value} = &self {
return format!("{}, {}%, {}%", hue, saturation, value);
}
if let Color::RGB{red: red, green: green, blue: blue} = &self {
return format!("#{:02x}{:02x}{:02x}", red, green, blue);
}
result
}
pub fn invert(&self) -> Self {
match &self {
Color::HSV{hue: hue, saturation: saturation, value: value} => {
return Color::HSV{hue: 360 - hue, saturation: 100 - saturation, value: 100 - value};
},
Color::RGB{red: red, green: green, blue: blue} => {
return Color::RGB{ red: 255 - red, green: 255 - green, blue: 255 - blue};
}
};
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20230111-3772066-1rmpeui/solution)
warning: the `red:` in this pattern is redundant
--> src/lib.rs:24:27
|
24 | if let Color::RGB{red: red, green: green, blue: blue} = &self {
| ^^^^^^^^ help: use shorthand field pattern: `red`
|
= note: `#[warn(non_shorthand_field_patterns)]` on by default
warning: the `green:` in this pattern is redundant
--> src/lib.rs:24:37
|
24 | if let Color::RGB{red: red, green: green, blue: blue} = &self {
| ^^^^^^^^^^^^ help: use shorthand field pattern: `green`
warning: the `blue:` in this pattern is redundant
--> src/lib.rs:24:51
|
24 | if let Color::RGB{red: red, green: green, blue: blue} = &self {
| ^^^^^^^^^^ help: use shorthand field pattern: `blue`
warning: the `hue:` in this pattern is redundant
--> src/lib.rs:33:27
|
33 | if let Color::HSV{hue: hue, saturation: saturation, value: value} = &self {
| ^^^^^^^^ help: use shorthand field pattern: `hue`
warning: the `saturation:` in this pattern is redundant
--> src/lib.rs:33:37
|
33 | if let Color::HSV{hue: hue, saturation: saturation, value: value} = &self {
| ^^^^^^^^^^^^^^^^^^^^^^ help: use shorthand field pattern: `saturation`
warning: the `value:` in this pattern is redundant
--> src/lib.rs:33:61
|
33 | if let Color::HSV{hue: hue, saturation: saturation, value: value} = &self {
| ^^^^^^^^^^^^ help: use shorthand field pattern: `value`
warning: the `hue:` in this pattern is redundant
--> src/lib.rs:43:27
|
43 | if let Color::HSV{hue: hue, saturation: saturation, value: value} = &self {
| ^^^^^^^^ help: use shorthand field pattern: `hue`
warning: the `saturation:` in this pattern is redundant
--> src/lib.rs:43:37
|
43 | if let Color::HSV{hue: hue, saturation: saturation, value: value} = &self {
| ^^^^^^^^^^^^^^^^^^^^^^ help: use shorthand field pattern: `saturation`
warning: the `value:` in this pattern is redundant
--> src/lib.rs:43:61
|
43 | if let Color::HSV{hue: hue, saturation: saturation, value: value} = &self {
| ^^^^^^^^^^^^ help: use shorthand field pattern: `value`
warning: the `red:` in this pattern is redundant
--> src/lib.rs:46:27
|
46 | if let Color::RGB{red: red, green: green, blue: blue} = &self {
| ^^^^^^^^ help: use shorthand field pattern: `red`
warning: the `green:` in this pattern is redundant
--> src/lib.rs:46:37
|
46 | if let Color::RGB{red: red, green: green, blue: blue} = &self {
| ^^^^^^^^^^^^ help: use shorthand field pattern: `green`
warning: the `blue:` in this pattern is redundant
--> src/lib.rs:46:51
|
46 | if let Color::RGB{red: red, green: green, blue: blue} = &self {
| ^^^^^^^^^^ help: use shorthand field pattern: `blue`
warning: the `hue:` in this pattern is redundant
--> src/lib.rs:54:24
|
54 | Color::HSV{hue: hue, saturation: saturation, value: value} => {
| ^^^^^^^^ help: use shorthand field pattern: `hue`
warning: the `saturation:` in this pattern is redundant
--> src/lib.rs:54:34
|
54 | Color::HSV{hue: hue, saturation: saturation, value: value} => {
| ^^^^^^^^^^^^^^^^^^^^^^ help: use shorthand field pattern: `saturation`
warning: the `value:` in this pattern is redundant
--> src/lib.rs:54:58
|
54 | Color::HSV{hue: hue, saturation: saturation, value: value} => {
| ^^^^^^^^^^^^ help: use shorthand field pattern: `value`
warning: the `red:` in this pattern is redundant
--> src/lib.rs:57:24
|
57 | Color::RGB{red: red, green: green, blue: blue} => {
| ^^^^^^^^ help: use shorthand field pattern: `red`
warning: the `green:` in this pattern is redundant
--> src/lib.rs:57:34
|
57 | Color::RGB{red: red, green: green, blue: blue} => {
| ^^^^^^^^^^^^ help: use shorthand field pattern: `green`
warning: the `blue:` in this pattern is redundant
--> src/lib.rs:57:48
|
57 | Color::RGB{red: red, green: green, blue: blue} => {
| ^^^^^^^^^^ help: use shorthand field pattern: `blue`
warning: `solution` (lib) generated 18 warnings
Finished test [unoptimized + debuginfo] target(s) in 0.68s
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_hsv_display ... FAILED
test solution_test::test_invert_rgb ... ok
test solution_test::test_new_hsv ... FAILED
test solution_test::test_rgb_display ... ok
failures:
---- solution_test::test_hsv_display stdout ----
thread 'solution_test::test_hsv_display' panicked at 'assertion failed: `(left == right)`
left: `"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 'assertion failed: catch_unwind(|| Color::new_hsv(361, 0, 0)).is_err()', tests/solution_test.rs:22: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`
