@@ -27,8 +27,6 @@ enum IntoColorError {
2727 IntConversion ,
2828}
2929
30- // I AM NOT DONE
31-
3230// Your task is to complete this implementation and return an Ok result of inner
3331// type Color. You need to create an implementation for a tuple of three
3432// integers, an array of three integers, and a slice of integers.
@@ -41,20 +39,36 @@ enum IntoColorError {
4139impl TryFrom < ( i16 , i16 , i16 ) > for Color {
4240 type Error = IntoColorError ;
4341 fn try_from ( tuple : ( i16 , i16 , i16 ) ) -> Result < Self , Self :: Error > {
42+ for i in [ tuple. 0 , tuple. 1 , tuple. 2 ] {
43+ if i < 0 || i > 255 {
44+ return Err ( IntoColorError :: IntConversion ) ;
45+ }
46+ }
47+ return Ok ( Color {
48+ red : tuple. 0 as u8 ,
49+ green : tuple. 1 as u8 ,
50+ blue : tuple. 2 as u8 ,
51+ } ) ;
4452 }
4553}
4654
4755// Array implementation
4856impl TryFrom < [ i16 ; 3 ] > for Color {
4957 type Error = IntoColorError ;
5058 fn try_from ( arr : [ i16 ; 3 ] ) -> Result < Self , Self :: Error > {
59+ Color :: try_from ( ( arr[ 0 ] , arr[ 1 ] , arr[ 2 ] ) )
5160 }
5261}
5362
5463// Slice implementation
5564impl TryFrom < & [ i16 ] > for Color {
5665 type Error = IntoColorError ;
5766 fn try_from ( slice : & [ i16 ] ) -> Result < Self , Self :: Error > {
67+ if slice. len ( ) != 3 {
68+ return Err ( IntoColorError :: BadLen ) ;
69+ } else {
70+ return Color :: try_from ( ( slice[ 0 ] , slice[ 1 ] , slice[ 2 ] ) ) ;
71+ }
5872 }
5973}
6074
0 commit comments