feat: completed solutions
This commit is contained in:
@@ -28,14 +28,45 @@ enum IntoColorError {
|
||||
impl TryFrom<(i16, i16, i16)> for Color {
|
||||
type Error = IntoColorError;
|
||||
|
||||
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {}
|
||||
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
|
||||
match tuple {
|
||||
(red, green, blue)
|
||||
if (0..=255).contains(&red)
|
||||
&& (0..=255).contains(&green)
|
||||
&& (0..=255).contains(&blue) =>
|
||||
{
|
||||
Ok(Color {
|
||||
red: red as u8,
|
||||
green: green as u8,
|
||||
blue: blue as u8,
|
||||
})
|
||||
}
|
||||
_ => Err(IntoColorError::IntConversion),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Array implementation.
|
||||
impl TryFrom<[i16; 3]> for Color {
|
||||
type Error = IntoColorError;
|
||||
|
||||
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {}
|
||||
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
|
||||
match arr.as_slice() {
|
||||
[red, green, blue] => {
|
||||
if (0..=255).contains(red) && (0..=255).contains(green) && (0..=255).contains(blue)
|
||||
{
|
||||
Ok(Color {
|
||||
red: *red as u8,
|
||||
green: *green as u8,
|
||||
blue: *blue as u8,
|
||||
})
|
||||
} else {
|
||||
Err(IntoColorError::IntConversion)
|
||||
}
|
||||
}
|
||||
_ => Err(IntoColorError::BadLen),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Slice implementation.
|
||||
@@ -43,7 +74,23 @@ impl TryFrom<[i16; 3]> for Color {
|
||||
impl TryFrom<&[i16]> for Color {
|
||||
type Error = IntoColorError;
|
||||
|
||||
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {}
|
||||
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
|
||||
match slice {
|
||||
[red, green, blue] => {
|
||||
if (0..=255).contains(red) && (0..=255).contains(green) && (0..=255).contains(blue)
|
||||
{
|
||||
Ok(Color {
|
||||
red: *red as u8,
|
||||
green: *green as u8,
|
||||
blue: *blue as u8,
|
||||
})
|
||||
} else {
|
||||
Err(IntoColorError::IntConversion)
|
||||
}
|
||||
}
|
||||
_ => Err(IntoColorError::BadLen),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
Reference in New Issue
Block a user