@@ -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,40 @@ 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+ let ( r, g, b) = tuple;
43+ if ( 0 ..=255 ) . contains ( & r) && ( 0 ..=255 ) . contains ( & g) && ( 0 ..255 ) . contains ( & b) {
44+ return Ok ( Color {
45+ red : r as u8 ,
46+ green : g as u8 ,
47+ blue : b as u8 ,
48+ } ) ;
49+ } else {
50+ return Err ( IntoColorError :: IntConversion ) ;
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+ if arr. len ( ) != 3 {
60+ return Err ( IntoColorError :: BadLen ) ;
61+ }
62+
63+ Color :: try_from ( ( arr[ 0 ] , arr[ 1 ] , arr[ 2 ] ) )
5164 }
5265}
5366
5467// Slice implementation
5568impl TryFrom < & [ i16 ] > for Color {
5669 type Error = IntoColorError ;
5770 fn try_from ( slice : & [ i16 ] ) -> Result < Self , Self :: Error > {
71+ if slice. len ( ) != 3 {
72+ return Err ( IntoColorError :: BadLen ) ;
73+ }
74+
75+ Color :: try_from ( ( slice[ 0 ] , slice[ 1 ] , slice[ 2 ] ) )
5876 }
5977}
6078
0 commit comments