Santa found it a good opportunity to have a look to see what Blitzen has been doing all this time when he was tech lead.
Rolling up his sleeves, he ran a git diff
command and began scrolling through the commits. His candy-cane-themed monitor reflected the code as his eyes narrowed like a debugger catching a null pointer. Then he froze, staring at the parse_row
function.
"Blitzen," he barked, "what is this disaster? return &'static str
for errors? Seriously?!"
Blitzen shrugged, sipping his oat milk latte. "MVP, Santa. Move fast, y'know?"
Santa slammed his mug of hot coffee down, marshmallows flying. "MVP? Last time you were in charge, you tried to rewrite grep and cat in Rust because you were 'bored'! And now you're telling me you can't handle proper error types? It takes five minutes! Do you want runtime panics for Christmas?!"
Bernard leaned in. "What if we use an enum
? Variants for different errors, match on them later. Clean, ergonomic."
Santa's face lit up like Rudolph's nose. "Finally, a solution that doesn't make me want to refactor the North Pole. Bernard, you get an extra cookie ration this year. Blitzen? No oat lattes for a week."
Blitzen sighed. "Enum it is."
Create an enum
called ParseError
with a few variants:
NoName
NoGoodDeeds
NoBadDeeds
InvalidGoodDeeds
InvalidBadDeeds
When errors displayed, they should be human-readable:
ParseError::NoName
should display as "Name field is missing"
ParseError::NoGoodDeeds
should display as "Good deeds field is missing"
ParseError::NoBadDeeds
should display as "Bad deeds field is missing"
ParseError::InvalidGoodDeeds
should display as "Good deeds value is invalid"
ParseError::InvalidBadDeeds
should display as "Bad deeds value is invalid"
Implement the std::error::Error
trait for ParseError
.
Update the parse_row
function to return meaningful errors using the ParseError
enum.
Make sure you return the correct error variant for each error condition.
Handle the cases where an item in a row is missing. e.g. Alice,,3
in this case, you should return an error with the NoGoodDeeds
variant.
If you're stuck or need a starting point, here are some hints to help you along the way!
The Error
trait requires you to implement the Display
and Debug
traits for the custom error type.
Implement Debug
by using the derive
macro. e.g. #[derive(Debug)]
For Display
you must implement manually. e.g.
Now that both traits are implemented, you can implement the Error
trait.
franlopezm
// Import the necessary modulesuse std::error::Error;use std::fmt;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds}impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next().ok_or(ParseError::NoName)?.to_string(); if name.is_empty() { return Err(ParseError::NoName); } let good_deeds = fields .next() .ok_or(ParseError::NoGoodDeeds)?; if good_deeds.is_empty() { return Err(ParseError::NoGoodDeeds); } let good_deeds = good_deeds .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .ok_or(ParseError::NoBadDeeds)?; if bad_deeds.is_empty() { return Err(ParseError::NoBadDeeds); } let bad_deeds = bad_deeds .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
mei28
use std::error::Error;use std::fmt;use std::fmt::{Debug, Display, Formatter};use crate::ParseError::{InvalidBadDeeds, InvalidGoodDeeds, NoBadDeeds, NoGoodDeeds, NoName};// Import the necessary modulespub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl Debug for ParseError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}", match self { ParseError::NoName => {"Name field is missing"} ParseError::NoGoodDeeds => {"Good deeds field is missing"} ParseError::NoBadDeeds => {"Bad deeds field is missing"} ParseError::InvalidGoodDeeds => {"Good deeds value is invalid"} ParseError::InvalidBadDeeds => {"Bad deeds value is invalid"} }) }}impl Display for ParseError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fmt::Debug::fmt(self, f) }}impl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next().ok_or(NoName)?.to_string(); if name == "" { return Err(NoName); } let good_deeds = fields.next().ok_or(NoGoodDeeds)?; if good_deeds == "" { return Err(NoGoodDeeds); } let good_deeds = good_deeds.parse::<u32>().map_err(|_| InvalidGoodDeeds)?; let bad_deeds = fields.next().ok_or(NoBadDeeds)?; if bad_deeds == "" { return Err(NoBadDeeds); } let bad_deeds = bad_deeds.parse::<u32>().map_err(|_| InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
FlorianGD
// Import the necessary modules#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl std::fmt::Display for ParseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let message = match self { ParseError::NoName => "Name field is missing", ParseError::NoGoodDeeds => "Good deeds field is missing", ParseError::NoBadDeeds => "Bad deeds field is missing", ParseError::InvalidGoodDeeds => "Good deeds value is invalid", ParseError::InvalidBadDeeds => "Bad deeds value is invalid", }; write!(f, "{}", message) }}// 2. Implement the Error trait for ParseErrorimpl std::error::Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next().ok_or(ParseError::NoName)?.to_string(); if name.is_empty() { return Err(ParseError::NoName) } let good_deeds = fields .next() .ok_or(ParseError::NoGoodDeeds)?; if good_deeds.is_empty() { return Err(ParseError::NoGoodDeeds) } let good_deeds = good_deeds .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .ok_or(ParseError::NoBadDeeds)?; if bad_deeds.is_empty() { return Err(ParseError::NoBadDeeds) } let bad_deeds = bad_deeds .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
josephcopenhaver
use std::fmt;use std::error::Error;// Import the necessary modulespub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl fmt::Debug for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(f, "{}", match self { ParseError::NoName => "Name field is missing", ParseError::NoGoodDeeds => "Good deeds field is missing", ParseError::NoBadDeeds => "Bad deeds field is missing", ParseError::InvalidGoodDeeds => "Good deeds value is invalid", ParseError::InvalidBadDeeds => "Bad deeds value is invalid", }) }}impl Error for ParseError {}impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { fmt::Debug::fmt(self, f) }}// 2. Implement the Error trait for ParseErrorpub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next().ok_or(ParseError::NoName)?.to_string(); if name == "" { return Err(ParseError::NoName); } let good_deeds = fields .next() .ok_or(ParseError::NoGoodDeeds)?; if good_deeds == "" { return Err(ParseError::NoGoodDeeds) } let good_deeds = good_deeds.parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .ok_or(ParseError::NoBadDeeds)?; if bad_deeds == "" { return Err(ParseError::NoBadDeeds) } let bad_deeds = bad_deeds.parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
kapaseker
use std::error::Error;use std::fmt;use std::fmt::{Debug, Display, Formatter};use crate::ParseError::{InvalidBadDeeds, InvalidGoodDeeds, NoBadDeeds, NoGoodDeeds, NoName};// Import the necessary modulespub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl Debug for ParseError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}", match self { ParseError::NoName => {"Name field is missing"} ParseError::NoGoodDeeds => {"Good deeds field is missing"} ParseError::NoBadDeeds => {"Bad deeds field is missing"} ParseError::InvalidGoodDeeds => {"Good deeds value is invalid"} ParseError::InvalidBadDeeds => {"Bad deeds value is invalid"} }) }}impl Display for ParseError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fmt::Debug::fmt(self, f) }}impl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next().ok_or(NoName)?.to_string(); if name == "" { return Err(NoName); } let good_deeds = fields.next().ok_or(NoGoodDeeds)?; if good_deeds == "" { return Err(NoGoodDeeds); } let good_deeds = good_deeds.parse::<u32>().map_err(|_| InvalidGoodDeeds)?; let bad_deeds = fields.next().ok_or(NoBadDeeds)?; if bad_deeds == "" { return Err(NoBadDeeds); } let bad_deeds = bad_deeds.parse::<u32>().map_err(|_| InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
Sympatron
// Import the necessary modules#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl std::error::Error for ParseError {}impl std::fmt::Display for ParseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", match self { ParseError::NoName => "Name field is missing", ParseError::NoGoodDeeds => "Good deeds field is missing", ParseError::NoBadDeeds => "Bad deeds field is missing", ParseError::InvalidGoodDeeds => "Good deeds value is invalid", ParseError::InvalidBadDeeds => "Bad deeds value is invalid", } ) }}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next().ok_or(ParseError::NoName)?.to_string(); if name == "" { return Err(ParseError::NoName); } let good_deeds = fields .next() .ok_or(ParseError::NoGoodDeeds)?; if good_deeds == "" { return Err(ParseError::NoGoodDeeds); } let good_deeds = good_deeds .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .ok_or(ParseError::NoBadDeeds)?; if bad_deeds == "" { return Err(ParseError::NoBadDeeds); } let bad_deeds = bad_deeds .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
MGehrmann
use std::error::Error;use std::fmt;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), // other variants... ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|f| !f.trim().is_empty()) .ok_or(ParseError::NoName)? .to_string(); let good_deeds = fields .next() .filter(|f| !f.trim().is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|f| !f.trim().is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
Ferdinanddb
use std::error::Error;use std::fmt;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), // other variants... ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|f| !f.trim().is_empty()) .ok_or(ParseError::NoName)? .to_string(); let good_deeds = fields .next() .filter(|f| !f.trim().is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|f| !f.trim().is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
sarub0b0
// Import the necessary modules#[derive(Debug)]pub enum ParseError { NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl std::fmt::Display for ParseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}// 2. Implement the Error trait for ParseErrorimpl std::error::Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|f| !f.trim().is_empty()) .ok_or(ParseError::NoName)? .to_string(); let good_deeds = fields .next() .filter(|f| !f.trim().is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|f| !f.trim().is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
sarub0b0
// Import the necessary modules#[derive(Debug)]pub enum ParseError { NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl std::fmt::Display for ParseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}// 2. Implement the Error trait for ParseErrorimpl std::error::Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors dbg!(csv_row); let mut fields = csv_row.split(','); let name = fields .next() .filter(|f| !f.trim().is_empty()) .ok_or(ParseError::NoName)? .to_string(); let good_deeds = fields .next() .filter(|f| !f.trim().is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|f| !f.trim().is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
n3m6
// Import the necessary modulesuse std::fmt;use std::error::Error;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}impl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next().filter(|x| !x.trim().is_empty()).ok_or(ParseError::NoName)?.to_string(); let good_deeds = fields .next() .filter(|x| !x.trim().is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|x| !x.trim().is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
habu1010
// Import the necessary modulesuse std::fmt;use std::error::Error;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}impl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next().filter(|x| !x.trim().is_empty()).ok_or(ParseError::NoName)?.to_string(); let good_deeds = fields .next() .filter(|x| !x.trim().is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|x| !x.trim().is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
doroshtapgh
use std::fmt;// Import the necessary modules#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds}// 2. Implement the Error trait for ParseErrorimpl std::error::Error for ParseError {}impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid") } }}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next().ok_or(ParseError::NoName)?.trim().to_string(); if name.is_empty() { return Err(ParseError::NoName); } let good_deeds = fields .next() .ok_or(ParseError::NoGoodDeeds)? .trim(); if good_deeds.is_empty() { return Err(ParseError::NoGoodDeeds); } let good_deeds = good_deeds .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .ok_or(ParseError::NoBadDeeds)? .trim(); if bad_deeds.is_empty() { return Err(ParseError::NoBadDeeds); } let bad_deeds = bad_deeds .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
tamanishi
// Import the necessary modulesuse std::fmt;use std::error::Error;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|s| {!s.trim().is_empty()}) .ok_or(ParseError::NoName)?.to_string(); let good_deeds = fields .next() .filter(|s| {!s.trim().is_empty()}) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|s| {!s.trim().is_empty()}) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
Ankit8848
// Import the necessary modulesuse std::error::Error;use std::fmt;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|s| {!s.trim().is_empty()}) .ok_or(ParseError::NoName)?.to_string(); let good_deeds = fields .next() .filter(|s| {!s.trim().is_empty()}) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|s| {!s.trim().is_empty()}) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
eguefif
// Import the necessary modulesuse std::error::Error;use std::fmt;#[derive(Debug)]pub enum ParseError { NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let error_message = match self { ParseError::NoName => "Name field is missing", ParseError::NoGoodDeeds => "Good deeds field is missing", ParseError::NoBadDeeds => "Bad deeds field is missing", ParseError::InvalidGoodDeeds => "Good deeds value is invalid", ParseError::InvalidBadDeeds => "Bad deeds value is invalid", }; write!(f, "{}", error_message) }}impl Error for ParseError {}// 2. Implement the Error trait for ParseErrorpub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next().ok_or(ParseError::NoName)?.to_string(); if name.trim().len() == 0 { return Err(ParseError::NoName); } let mut good_deeds = fields .next() .ok_or(ParseError::NoGoodDeeds)?; if good_deeds.len() == 0 { return Err(ParseError::NoGoodDeeds); } let good_deeds = good_deeds.parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .ok_or(ParseError::NoBadDeeds)?; if bad_deeds.len() == 0 { return Err(ParseError::NoBadDeeds); } let bad_deeds = bad_deeds.parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
KushnerykPavel
// Import the necessary modulesuse std::error::Error;use std::fmt;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|s| {!s.trim().is_empty()}) .ok_or(ParseError::NoName)?.to_string(); let good_deeds = fields .next() .filter(|s| {!s.trim().is_empty()}) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|s| {!s.trim().is_empty()}) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
rjensen
// Import the necessary modulesuse std::error::Error;use std::fmt::{Display, Formatter};#[derive(Debug)]pub enum ParseError { NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl Display for ParseError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { Self::NoName => write!(f, "Name field is missing"), Self::NoGoodDeeds => write!(f, "Good deeds field is missing"), Self::NoBadDeeds => write!(f, "Bad deeds field is missing"), Self::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), Self::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or(ParseError::NoName)? .to_string(); let good_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
rony0000013
// Import the necessary modulesuse std::fmt;use std::error::Error;use::std::fmt::{Display, Formatter};#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}impl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next() .filter(|s| !s.trim().is_empty()) .ok_or(ParseError::NoName)?.to_string(); let good_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
pagyeman
// Import the necessary modulesuse::std::{error::Error, fmt::{Display, Formatter}};#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl Display for ParseError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { use ParseError::*; match self { NoName => write!(f, "Name field is missing"), NoGoodDeeds => write!(f, "Good deeds field is missing"), NoBadDeeds => write!(f, "Bad deeds field is missing"), InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}impl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or(ParseError::NoName)?.to_string(); let good_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
arm01846
// Import the necessary modulesuse std::fmt;use std::error::Error;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}impl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next().ok_or(ParseError::NoName)?.to_string(); if name.is_empty() { return Err(ParseError::NoName); } let good_deeds = fields.next(); if let Some("") = good_deeds { return Err(ParseError::NoGoodDeeds) } let good_deeds = good_deeds .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields.next(); if let Some("") = bad_deeds { return Err(ParseError::NoBadDeeds) } let bad_deeds = bad_deeds .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
gmvar
// Import the necessary modulesuse std::fmt;use std::error;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}// 2. Implement the Error trait for ParseErrorimpl error::Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or(ParseError::NoName)? .to_string(); let good_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>().map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>().map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
Nismirno
// Import the necessary modulesuse std::error::Error;use std::fmt;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}impl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = match fields.next().ok_or(ParseError::NoName) { Ok(r) => { if r.trim().len() == 0 { return Err(ParseError::NoName); } r.to_string() } Err(e) => return Err(e), }; let good_deeds = match fields .next() .ok_or(ParseError::NoGoodDeeds) { Ok(r) => { if r.trim().len() == 0 { return Err(ParseError::NoGoodDeeds); } r.parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)? } Err(e) => return Err(e), }; let bad_deeds = match fields .next() .ok_or(ParseError::NoBadDeeds) { Ok(r) => { if r.trim().len() == 0 { return Err(ParseError::NoBadDeeds); } r.parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)? } Err(e) => return Err(e), }; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
gsspdev
// Import the necessary modulesuse std::fmt;use std::error::Error;// creates ParseError enum for error handling#[derive(Debug)]pub enum ParseError { NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds}// implements Display trait for ParseErrorimpl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid") } }}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } // .filter(|a| !a.trim().is_empty()) pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|a| !a.trim().is_empty()) .ok_or(ParseError::NoName)? .to_string(); let good_deeds = fields .next() .filter(|a| !a.trim().is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>().map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|a| !a.trim().is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>().map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
hafihaf123
// Import the necessary modulesuse std::fmt;use std::fmt::{Display, Formatter};use std::error::Error;use ParseError::*;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl ParseError { pub fn as_str(&self) -> &'static str { // tidy-alphabetical-start match *self { NoName => "Name field is missing", NoGoodDeeds => "Good deeds field is missing", NoBadDeeds => "Bad deeds field is missing", InvalidGoodDeeds => "Good deeds value is invalid", InvalidBadDeeds => "Bad deeds value is invalid", } }}impl Display for ParseError { fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { fmt.write_str(self.as_str()) }}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = parse_field(fields.next(), NoName)?.to_string(); let good_deeds = parse_field(fields.next(), NoGoodDeeds)?; let good_deeds = good_deeds .parse::<u32>() .map_err(|_| InvalidGoodDeeds)?; let bad_deeds = parse_field(fields.next(), NoBadDeeds)?; let bad_deeds = bad_deeds .parse::<u32>() .map_err(|_| InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}fn parse_field<'a>(field: Option<&'a str>, error: ParseError) -> Result<&'a str, ParseError> { field .map(|f| f.trim()) .filter(|f| !f.is_empty()) .ok_or(error)}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
hafihaf123
// Import the necessary modulesuse std::fmt;use std::fmt::{Display, Formatter};use std::error::Error;use ParseError::*;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl ParseError { pub fn as_str(&self) -> &'static str { // tidy-alphabetical-start match *self { NoName => "Name field is missing", NoGoodDeeds => "Good deeds field is missing", NoBadDeeds => "Bad deeds field is missing", InvalidGoodDeeds => "Good deeds value is invalid", InvalidBadDeeds => "Bad deeds value is invalid", } }}impl Display for ParseError { fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { fmt.write_str(self.as_str()) }}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = parse_field(fields.next(), NoName)?; let good_deeds = parse_field(fields.next(), NoGoodDeeds)?; let good_deeds = good_deeds .parse::<u32>() .map_err(|_| InvalidGoodDeeds)?; let bad_deeds = parse_field(fields.next(), NoBadDeeds)?; let bad_deeds = bad_deeds .parse::<u32>() .map_err(|_| InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}fn parse_field(field: Option<&str>, error: ParseError) -> Result<String, ParseError> { field .map(|f| f.to_string()) .filter(|f| !f.is_empty()) .ok_or(error)}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
stanisgo
// Import the necessary modulesuse std::fmt;use std::error::Error;#[derive(Debug)]pub enum ParseError { NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}impl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { let mut fields = csv_row.split(','); let name = fields .next() .map(|filed| filed.trim()) .filter(|filed| !filed.is_empty()) .ok_or(ParseError::NoName)? .to_string(); let good_deeds = fields .next() .map(|filed| filed.trim()) .filter(|filed| !filed.is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .map(|filed| filed.trim()) .filter(|filed| !filed.is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
Burnus
use std::error::Error;use std::fmt::{Display, Formatter};#[derive(Debug)]pub enum ParseError { NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl Display for ParseError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { Self::NoName => write!(f, "Name field is missing"), Self::NoGoodDeeds => write!(f, "Good deeds field is missing"), Self::NoBadDeeds => write!(f, "Bad deeds field is missing"), Self::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), Self::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}impl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { let mut fields = csv_row.split(','); let name = fields.next().ok_or(ParseError::NoName)?.to_string(); if name.is_empty() { return Err(ParseError::NoName); } let good_deeds = fields .next() .ok_or(ParseError::NoGoodDeeds)?; if good_deeds.is_empty() { return Err(ParseError::NoGoodDeeds); } let good_deeds = good_deeds .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .ok_or(ParseError::NoBadDeeds)?; if bad_deeds.is_empty() { return Err(ParseError::NoBadDeeds); } let bad_deeds = bad_deeds .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
vaclav0411
use std::fmt::Display;use std::error::Error;#[derive(Debug)]pub enum ParseError { NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl Error for ParseError {}impl Display for ParseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next().filter(|s| !s.trim().is_empty()).ok_or(ParseError::NoName)?.to_string(); let good_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
joanne-cmd
// Import the necessary modulesuse std::error::Error;use std::fmt::Display;// Import the necessary modules#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}impl Display for ParseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|a| !a.trim().is_empty()) .ok_or(ParseError::NoName)? .to_string(); let good_deeds = fields .next() .filter(|a| !a.trim().is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|a| !a.trim().is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
felipesaruhashi
use std::error::Error;use std::fmt::Display;// Import the necessary modules#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}impl Display for ParseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|a| !a.trim().is_empty()) .ok_or(ParseError::NoName)? .to_string(); let good_deeds = fields .next() .filter(|a| !a.trim().is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|a| !a.trim().is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
KLcpb
// Import the necessary modules// Import the necessary modulesuse std::error::Error;use std::fmt;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let error_msg = match self { Self::NoName => "Name field is missing", Self::NoGoodDeeds => "Good deeds field is missing", Self::NoBadDeeds => "Bad deeds field is missing", Self::InvalidGoodDeeds => "Good deeds value is invalid", Self::InvalidBadDeeds => "Bad deeds value is invalid", }; write!(f, "{}", error_msg) }}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or_else(|| ParseError::NoName)? .to_string(); let good_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or_else(|| ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or_else(|| ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
joshvon44
use std::error::Error;use std::fmt::Display;// Import the necessary modules#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}impl Display for ParseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|a| !a.trim().is_empty()) .ok_or(ParseError::NoName)? .to_string(); let good_deeds = fields .next() .filter(|a| !a.trim().is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|a| !a.trim().is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
wishkus
use std::error::Error;use std::fmt;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let error_msg = match self { Self::NoName => "Name field is missing", Self::NoGoodDeeds => "Good deeds field is missing", Self::NoBadDeeds => "Bad deeds field is missing", Self::InvalidGoodDeeds => "Good deeds value is invalid", Self::InvalidBadDeeds => "Bad deeds value is invalid", }; write!(f, "{}", error_msg) }}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or_else(|| ParseError::NoName)? .to_string(); let good_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or_else(|| ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or_else(|| ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
kanakshilledar
// Import the necessary modulesuse std::error::Error;use std::fmt;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let error_msg = match self { Self::NoName => "Name field is missing", Self::NoGoodDeeds => "Good deeds field is missing", Self::NoBadDeeds => "Bad deeds field is missing", Self::InvalidGoodDeeds => "Good deeds value is invalid", Self::InvalidBadDeeds => "Bad deeds value is invalid", }; write!(f, "{}", error_msg) }}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or_else(|| ParseError::NoName)? .to_string(); let good_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or_else(|| ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or_else(|| ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
lulingar
// Import the necessary modulesuse std::{ error::Error, fmt,};#[derive(Debug)]pub enum ParseError { NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::NoName => write!(f, "Name field is missing"), Self::NoGoodDeeds => write!(f, "Good deeds field is missing"), Self::NoBadDeeds => write!(f, "Bad deeds field is missing"), Self::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), Self::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { use ParseError::*; // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next().ok_or(NoName)?.to_string(); if name.trim().len() == 0 { return Err(ParseError::NoName); } let good_deeds = fields .next() .ok_or(NoGoodDeeds)? .trim() .parse::<u32>() .map_err(|err| { use std::num::IntErrorKind::*; match &err.kind() { Empty => ParseError::NoGoodDeeds, _ => ParseError::InvalidGoodDeeds, } })?; let bad_deeds = fields .next() .ok_or(NoBadDeeds)? .trim() .parse::<u32>() .map_err(|err| { use std::num::IntErrorKind::*; match &err.kind() { Empty => ParseError::NoBadDeeds, _ => ParseError::InvalidBadDeeds, } })?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
chriswmann
// Import the necessary modulesuse std::error::Error;use std::fmt;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let error_msg = match self { Self::NoName => "Name field is missing", Self::NoGoodDeeds => "Good deeds field is missing", Self::NoBadDeeds => "Bad deeds field is missing", Self::InvalidGoodDeeds => "Good deeds value is invalid", Self::InvalidBadDeeds => "Bad deeds value is invalid", }; write!(f, "{}", error_msg) }}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or_else(|| ParseError::NoName)? .to_string(); let good_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or_else(|| ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or_else(|| ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
chriswmann
// Import the necessary modulesuse std::error::Error;use std::fmt;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let error_msg = match self { Self::NoName => "Name field is missing", Self::NoGoodDeeds => "Good deeds field is missing", Self::NoBadDeeds => "Bad deeds field is missing", Self::InvalidGoodDeeds => "Good deeds value is invalid", Self::InvalidBadDeeds => "Bad deeds value is invalid", }; write!(f, "{}", error_msg) }}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or_else(|| ParseError::NoName)? .to_string(); let good_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or_else(|| ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|s| !s.trim().is_empty()) .ok_or_else(|| ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
martynovs
// Import the necessary modules#[derive(Debug)]pub enum ParseError { NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl std::fmt::Display for ParseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}impl std::error::Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .filter(|s| s.trim().len() > 0) .ok_or(ParseError::NoName)?.to_string(); let good_deeds = fields .next() .filter(|s| s.trim().len() > 0) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|s| s.trim().len() > 0) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
titoeb
// Import the necessary modulesuse std::error::Error;#[derive(Debug, std::cmp::PartialEq)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl std::fmt::Display for ParseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::NoName => write!(f, "Name field is missing"), Self::NoGoodDeeds => write!(f, "Good deeds field is missing"), Self::NoBadDeeds => write!(f, "Bad deeds field is missing"), Self::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), Self::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), }?; Ok(()) }}impl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = match fields.next().ok_or(ParseError::NoName)? { "" => Err(ParseError::NoName), name => Ok(name), }? .to_string(); let good_deeds = match fields.next().ok_or(ParseError::NoGoodDeeds)? { "" => Err(ParseError::NoGoodDeeds), good_deed => good_deed .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds), }?; let bad_deeds = match fields.next().ok_or(ParseError::NoBadDeeds)? { "" => Err(ParseError::NoBadDeeds), bad_deed => bad_deed .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds), }?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}#[cfg(test)]mod test { use super::*; #[test] fn no_good_deed() { assert_eq!( Kid::parse_row("Alice,,1").err().unwrap(), ParseError::NoGoodDeeds ) } #[test] fn no_name() { assert_eq!(Kid::parse_row(",1,1").err().unwrap(), ParseError::NoName) } #[test] fn no_bad_deed() { assert_eq!( Kid::parse_row("Alice,1,").err().unwrap(), ParseError::NoBadDeeds ) }}
wlabranche
// Import the necessary modulesuse std::error::Error;use std::fmt;use std::fmt::{ Display, Formatter };#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl Display for ParseError { fn fmt(&self, f: &mut Formatter) -> fmt::Result { let error_message = match self { ParseError::NoName => "Name field is missing", ParseError::NoGoodDeeds => "Good deeds field is missing", ParseError::NoBadDeeds => "Bad deeds field is missing", ParseError::InvalidGoodDeeds => "Good deeds value is invalid", ParseError::InvalidBadDeeds => "Bad deeds value is invalid", }; write!(f, "{}", error_message) }}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next() .filter(|field| !field.is_empty()) .ok_or(ParseError::NoName)?.to_string(); let good_deeds = fields .next() .filter(|field| !field.is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|field| !field.is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
RedNapPanda
// Import the necessary modulesuse std::error::Error;use std::fmt::{Display, Formatter};use std::fmt;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl Display for ParseError { fn fmt(&self, f: &mut Formatter) -> fmt::Result { let s = match self { ParseError::NoName => "Name field is missing", ParseError::NoGoodDeeds => "Good deeds field is missing", ParseError::NoBadDeeds => "Bad deeds field is missing", ParseError::InvalidGoodDeeds => "Good deeds value is invalid", ParseError::InvalidBadDeeds => "Bad deeds value is invalid", }; write!(f, "{}", s) }}// 2. Implement the Error trait for ParseErrorimpl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next().filter(|s| !s.is_empty()).ok_or(ParseError::NoName)?.to_string(); let good_deeds = fields .next() .filter(|s| !s.is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|s| !s.is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
strachan
// Import the necessary modulesuse std::fmt;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}// 2. Implement the Error trait for ParseErrorimpl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid") } }}impl std::error::Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next() .filter(|name| !name.is_empty()).ok_or(ParseError::NoName)?.to_string(); let good_deeds = fields .next().filter(|deeds| !deeds.is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next().filter(|deeds| !deeds.is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
sweet2honey
use std::fmt; // Import the necessary modules #[derive(Debug, PartialEq)] pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds, } // 2. Implement the Error trait for ParseError impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } } } impl std::error::Error for ParseError { } #[derive(Debug, PartialEq)] pub struct Kid { pub name: String, pub niceness: Niceness, } impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next() .and_then(|x| if x.trim().is_empty() { None } else {Some(x.trim())}) .ok_or(ParseError::NoName)? .to_string(); let good_deeds = fields .next() .and_then(|x| if x.trim().is_empty() { None } else {Some(x.trim())}) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .and_then(|x| if x.trim().is_empty() { None } else {Some(x.trim())}) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 } } pub const GOOD_WEIGHT: f32 = 1.0; pub const BAD_WEIGHT: f32 = 2.0; #[derive(Debug, PartialEq)] pub enum Niceness { Nice(u32), Naughty, }
wendko
use std::fmt; // Import the necessary modules #[derive(Debug, PartialEq)] pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds, } // 2. Implement the Error trait for ParseError impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } } } impl std::error::Error for ParseError { } #[derive(Debug, PartialEq)] pub struct Kid { pub name: String, pub niceness: Niceness, } impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next() .and_then(|x| if x.trim().is_empty() { None } else {Some(x.trim())}) .ok_or(ParseError::NoName)? .to_string(); let good_deeds = fields .next() .and_then(|x| if x.trim().is_empty() { None } else {Some(x.trim())}) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .and_then(|x| if x.trim().is_empty() { None } else {Some(x.trim())}) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 } } pub const GOOD_WEIGHT: f32 = 1.0; pub const BAD_WEIGHT: f32 = 2.0; #[derive(Debug, PartialEq)] pub enum Niceness { Nice(u32), Naughty, }
Stephan-Lindner
use std::error::Error;use std::fmt::{Display, Formatter};#[derive(Debug)]pub enum ParseError { NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl Error for ParseError {}impl Display for ParseError { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { use ParseError::*; write!(f, "{}", match self { NoName => "Name field is missing", NoGoodDeeds => "Good deeds field is missing", NoBadDeeds => "Bad deeds field is missing", InvalidGoodDeeds => "Good deeds value is invalid", InvalidBadDeeds => "Bad deeds value is invalid", }) }}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { let mut fields = csv_row.split(','); let name = fields .next() .filter(|field| !field.is_empty()) .ok_or(ParseError::NoName)? .to_string(); let good_deeds = fields .next() .filter(|field| !field.is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|field| !field.is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
Stephan-Lindner
use std::error::Error;use std::fmt::{Display, Formatter};#[derive(Debug)]pub enum ParseError { NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl Error for ParseError {}impl Display for ParseError { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { use ParseError::*; write!(f, "{}", match self { NoName => "Name field is missing", NoGoodDeeds => "Good deeds field is missing", NoBadDeeds => "Bad deeds field is missing", InvalidGoodDeeds => "Good deeds value is invalid", InvalidBadDeeds => "Bad deeds value is invalid", }) }}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { let mut fields = csv_row.split(','); let name = fields .next() .filter(|field| !field.is_empty()) .ok_or(ParseError::NoName)? .to_string(); if name.is_empty() { return Err(ParseError::NoName) } let good_deeds = fields .next() .filter(|field| !field.is_empty()) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .filter(|field| !field.is_empty()) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
DominicD
use std::error::Error;use std::fmt::Display;use std::fmt::Formatter;// Import the necessary modules#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl ParseError { pub fn as_str(&self) -> &'static str { use ParseError::*; match *self { NoName => "Name field is missing", NoGoodDeeds => "Good deeds field is missing", NoBadDeeds => "Bad deeds field is missing", InvalidGoodDeeds => "Good deeds value is invalid", InvalidBadDeeds => "Bad deeds value is invalid" } }}impl Display for ParseError{ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.write_str(self.as_str()) }}impl Error for ParseError{}// 2. Implement the Error trait for ParseErrorpub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next().ok_or(ParseError::NoName)?.to_string(); if name.trim().is_empty() { return Err(ParseError::NoName); } let good_deeds = fields .next() .and_then(|good_deeds| { let trimmed = good_deeds.trim(); if trimmed.is_empty() { None } else { Some(trimmed) } }) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .and_then(|bad_deeds| { let trimmed = bad_deeds.trim(); if trimmed.is_empty() { None } else { Some(trimmed) } }) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
jsdm13
use std::fmt;use std::error::Error;#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds}// 2. Implement the Error trait for ParseErrorimpl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid") } }}impl Error for ParseError {}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields .next() .and_then(|name| { let trimmed = name.trim(); if trimmed.is_empty() { None } else { Some(trimmed) } }) .ok_or(ParseError::NoName)? .to_string(); let good_deeds = fields .next() .and_then(|good_deeds| { let trimmed = good_deeds.trim(); if trimmed.is_empty() { None } else { Some(trimmed) } }) .ok_or(ParseError::NoGoodDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds = fields .next() .and_then(|bad_deeds| { let trimmed = bad_deeds.trim(); if trimmed.is_empty() { None } else { Some(trimmed) } }) .ok_or(ParseError::NoBadDeeds)? .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}
seilis
// Import the necessary modulesuse std::fmt::{Display, Formatter};#[derive(Debug)]pub enum ParseError { // 1. Add variants here (read description) NoName, NoGoodDeeds, NoBadDeeds, InvalidGoodDeeds, InvalidBadDeeds,}impl Display for ParseError { fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> { match self { ParseError::NoName => write!(f, "Name field is missing"), ParseError::NoGoodDeeds => write!(f, "Good deeds field is missing"), ParseError::NoBadDeeds => write!(f, "Bad deeds field is missing"), ParseError::InvalidGoodDeeds => write!(f, "Good deeds value is invalid"), ParseError::InvalidBadDeeds => write!(f, "Bad deeds value is invalid"), } }}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Kid { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Kid { name, niceness } } pub fn parse_row(csv_row: &str) -> Result<Kid, ParseError> { // 3. Update the code to return meaningful errors let mut fields = csv_row.split(','); let name = fields.next().ok_or(ParseError::NoName)?.to_string(); if name.len() == 0 { return Err(ParseError::NoName); } let good_deeds_raw = fields .next() .ok_or(ParseError::NoGoodDeeds)?; if good_deeds_raw.len() == 0 { return Err(ParseError::NoGoodDeeds); } let good_deeds = good_deeds_raw .parse::<u32>() .map_err(|_| ParseError::InvalidGoodDeeds)?; let bad_deeds_raw = fields .next() .ok_or(ParseError::NoBadDeeds)?; if bad_deeds_raw.len() == 0 { return Err(ParseError::NoBadDeeds); } let bad_deeds = bad_deeds_raw .parse::<u32>() .map_err(|_| ParseError::InvalidBadDeeds)?; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}