feat: completed solutions

This commit is contained in:
2026-03-23 03:36:33 -04:00
parent 2279bea6f1
commit f568c094cb
65 changed files with 424 additions and 139 deletions
+50 -3
View File
@@ -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() {