From cd49153d479313ea18f4e5df2b38bd95ce9b82ff Mon Sep 17 00:00:00 2001 From: MatyTan <1396568121@qq.com> Date: Mon, 28 Nov 2022 22:11:20 +0800 Subject: [PATCH] day13 --- exercises/conversions/from_str.rs | 28 ++++++++++- exercises/conversions/try_from_into.rs | 65 +++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 4 deletions(-) diff --git a/exercises/conversions/from_str.rs b/exercises/conversions/from_str.rs index fe168159..e286ceae 100644 --- a/exercises/conversions/from_str.rs +++ b/exercises/conversions/from_str.rs @@ -28,8 +28,6 @@ enum ParsePersonError { ParseInt(ParseIntError), } -// I AM NOT DONE - // Steps: // 1. If the length of the provided string is 0, an error should be returned // 2. Split the given string on the commas present in it @@ -46,6 +44,32 @@ enum ParsePersonError { impl FromStr for Person { type Err = ParsePersonError; fn from_str(s: &str) -> Result { + let mut p = Person { + name: "".to_string(), + age: 0, + }; + if s.is_empty() { + return Err(ParsePersonError::Empty); + } + + let mut person: Vec<&str> = s.split(',').collect(); + + if person.len() != 2 { + return Err(ParsePersonError::BadLen); + } + + if person[0].is_empty() { + return Err(ParsePersonError::NoName); + } + p.name = person[0].to_string(); + + match person[1].parse() { + Ok(a) => p.age = a, + Err(err) => { + return Err(ParsePersonError::ParseInt(err)); + } + } + Ok(p) } } diff --git a/exercises/conversions/try_from_into.rs b/exercises/conversions/try_from_into.rs index fa98bc90..21f56fbc 100644 --- a/exercises/conversions/try_from_into.rs +++ b/exercises/conversions/try_from_into.rs @@ -23,8 +23,6 @@ enum IntoColorError { IntConversion, } -// I AM NOT DONE - // Your task is to complete this implementation // and return an Ok result of inner type Color. // You need to create an implementation for a tuple of three integers, @@ -38,6 +36,24 @@ enum IntoColorError { impl TryFrom<(i16, i16, i16)> for Color { type Error = IntoColorError; fn try_from(tuple: (i16, i16, i16)) -> Result { + let x: u8 = tuple + .0 + .try_into() + .map_err(|_| IntoColorError::IntConversion)?; + let y: u8 = tuple + .1 + .try_into() + .map_err(|_| IntoColorError::IntConversion)?; + let z: u8 = tuple + .2 + .try_into() + .map_err(|_| IntoColorError::IntConversion)?; + + Ok(Color { + red: x, + green: y, + blue: z, + }) } } @@ -45,6 +61,30 @@ impl TryFrom<(i16, i16, i16)> for Color { impl TryFrom<[i16; 3]> for Color { type Error = IntoColorError; fn try_from(arr: [i16; 3]) -> Result { + let mut a: [u8; 3] = [0, 0, 0]; + // arr.into_iter().enumerate().map(|(i, x)| { + // let t: u8 = match x.try_into() { + // Ok(i) => i, + // Err(err) => { + // return Err(IntoColorError::IntConversion); + // } + // }; + // }); + for (i, x) in arr.into_iter().enumerate() { + let tmp: u8 = match x.to_owned().try_into() { + Ok(i) => i, + Err(err) => { + return Err(IntoColorError::IntConversion); + } + }; + // let tmp: u8 = x.try_into():IntoColorError::IntConversion; + a[i] = tmp + } + Ok(Color { + red: a[0], + green: a[1], + blue: a[2], + }) } } @@ -52,6 +92,27 @@ impl TryFrom<[i16; 3]> for Color { impl TryFrom<&[i16]> for Color { type Error = IntoColorError; fn try_from(slice: &[i16]) -> Result { + if slice.len() != 3 { + return Err(IntoColorError::BadLen); + } + let mut a: [u8; 3] = [0, 0, 0]; + // arr.iter().enumerate().map(|(i, x)| { + // x = x.try_into() + // }) + for (i, x) in slice.into_iter().enumerate() { + let tmp: u8 = match x.to_owned().try_into() { + Ok(i) => i, + Err(err) => { + return Err(IntoColorError::IntConversion); + } + }; + a[i] = tmp + } + Ok(Color { + red: a[0], + green: a[1], + blue: a[2], + }) } }