Pattern matching in Rust is a powerful feature that allows you to destructure and inspect complex data types in a concise and readable manner. By using match statements, you can handle different variants of enums, tuples, and other data structures, applying specific logic based on the shape and content of the data.
This challenge will help you practice advanced pattern matching techniques in a real-world scenario.
You need to implement a method check_validity(&self)
for the BookItem
enum that returns a bool
indicating whether the item is valid based on the following rules:
Book:
EBook:
String
) must be non-empty.Collection:
OutOfPrint:
check_validity(&self)
using match guards to reflect these rules.true
or false
) based on the type of BookItem
.if
guards to combine multiple conditions: if *pages > 0 && *d >= 0
Option<i32>
using Some(d)
and None
!items.is_empty()
firstiter().all(|item| item.check_validity())
to ensure all items are valid*pages
, *second
_ => false
arm handles any remaining invalid cases#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { Self::Book{ pages, discount } => { *pages > 0 && match discount { Some(discount) => *discount >= 0 && *discount <= 50, None => true } }, Self::EBook(title, (_, second)) => { !title.is_empty() && *second > 0 }, Self::Collection(collection) => { !collection.is_empty() && collection.iter().all(|item| item.check_validity()) }, _ => false } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation // any comparison requires destruct pub fn check_validity(&self) -> bool { match self { BookItem::Book{pages, discount} => { *pages > 0 && match discount { Some(d) => *d > 0 && *d < 50, None => true } }, BookItem::EBook(title, (_, field2)) => {!title.is_empty() && *field2 > 0}, BookItem::Collection(vec) => { !vec.is_empty() && vec.iter().all(|a| a.check_validity())}, _ => false } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug,Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self{ BookItem::Book{pages,discount} if *pages>0 && discount.is_none() => true, BookItem::Book{pages: pages, discount: Some(disc)} if *pages>0 && *disc>=0 && *disc<=50 => true, BookItem::EBook(title, (_,v)) if !title.is_empty() && *v>0 => true, BookItem::Collection(vec) if !vec.is_empty() && vec.iter().map(|x| if x.check_validity() {1}else{0}).sum::<usize>() == vec.len() => true, _ => false } } }// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool{ match self { BookItem::Book { pages, discount } => match (pages, discount) { (1.., Some(0..=50) | None) => true, _ => false }, BookItem::EBook(title, (first, second) ) => match (title.len(), first, second) { (1.., _ , 1..) => true, _ => false }, BookItem::Collection(collection) => match (collection.len(), collection.iter().all(|x| x.check_validity()) ) { (1.., true) => true, _ => false }, BookItem::OutOfPrint => false, } } }// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { Self::OutOfPrint => false, Self::Collection(list) if list.is_empty() || !list.iter().all(|book| book.check_validity()) => { false } Self::EBook(title, tuple) if title.is_empty() || tuple.0 <= 0 || tuple.1 <= 0 => false, Self::Book { pages, discount } if *pages <= 0 || discount.is_some_and(|discount| discount <= 0 || discount > 50) => { false } _ => true, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { *pages > 0 && if let Some(discount) = discount { *discount >= 0 && *discount <= 50 } else { true } }, BookItem::EBook(title, (a, b)) => { !title.is_empty() && *a > 0 && *b > 0 }, BookItem::Collection(books) => { !books.is_empty() && books.iter().all(|book| book.check_validity()) }, BookItem::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match &self { BookItem::Book{ pages: p, discount: _} if *p < 0 => false, BookItem::Book{ pages: _, discount: Some(d)} if *d >= 0 && *d <= 50 => true, BookItem::Book{ pages: _, discount: None} => true, BookItem::EBook(s, (_, p)) if s.len() > 0 && *p > 0 => true, BookItem::Collection(v) if v.is_empty() => false, BookItem::Collection(v) if v.iter().all(|b|b.check_validity()) => true, _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book {pages, discount: None } if (*pages > 0) => true, BookItem::Book {pages, discount: Some(discount)} if (*pages > 0) & (*discount >= 0) & (*discount <=50) => true, BookItem::EBook (s, (_, b)) if !s.is_empty() & (*b > 0) => true, BookItem::Collection(book_items) if !(book_items.is_empty()) => book_items.iter().all(|b| b.check_validity()), BookItem::OutOfPrint => false, _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match &self { BookItem::Book { pages, discount } => { *pages > 0 && discount.map_or(true, |discount| discount >= 0 && discount <= 50) } BookItem::EBook(title, (_, t)) => !title.is_empty() && *t > 0, BookItem::Collection(book_items) => { !(book_items.is_empty() || book_items.iter().any(|item| !item.check_validity())) } BookItem::OutOfPrint => false, } } // TODO: Finish the implementation}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book{ pages, discount } => { *pages > 0 && discount.map_or(true, |d| d >= 0 && d <= 50) }, BookItem::EBook(title, (_, page_count)) => { !title.is_empty() && *page_count > 0 }, BookItem::Collection(books) => { !(books.is_empty() || books.iter().any(|book| !book.check_validity())) }, BookItem::OutOfPrint => false, } }}pub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { let discount = match discount { Some(d) => *d >= 0 && *d <= 50, None => true, }; *pages > 0 && discount } BookItem::EBook(title, (num1, num2)) => !title.is_empty() && *num1 > 0 && *num2 > 0, BookItem::Collection(book_items) => { !book_items.is_empty() && book_items.iter().all(|book| book.check_validity()) } BookItem::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { let mut validity = true; match &self { BookItem::Book { pages, discount } => { if *pages < 0 { validity = false; } if let Some(count) = *discount { if count > 50 || count < 0 { validity = false; } } } BookItem::EBook(title, (m, n)) => { if title.is_empty() || *m <= 0 || *n <= 0 { validity = false; } } BookItem::Collection(list) => { if list.is_empty() { validity = false; } list.iter().for_each(|item| { if !item.check_validity() { validity = false; } }); } BookItem::OutOfPrint => { validity = false; } } validity }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { if *pages <= 0 { return false; } // check discount validity if present match discount { Some(discount_value) => { *discount_value >= 0 && *discount_value <= 50 } None => true, // no discount is valid } } BookItem::EBook(title, (_, second_field)) => { !title.is_empty() && *second_field > 0 } BookItem::Collection(items) => { !items.is_empty() && items.iter().all(|item| item.check_validity()) } BookItem::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { if *pages <= 0 { return false; } if let Some(discount) = discount { if *discount < 0 || *discount > 50 { return false; } } true } BookItem::EBook(title, (a, b)) => { if title.is_empty() { return false; } if *a <= 0 || *b <= 0 { return false; } true } BookItem::Collection(books) => { if books.is_empty() { return false; } books.iter().all(|book| book.check_validity()) } _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { Self::Book{pages, discount} if *pages > 0 && if discount.is_some() { discount.unwrap() >= 0 && discount.unwrap() <= 50 } else { discount.is_none() } => true, Self::EBook(name, (a, b)) if !name.is_empty() && *b > 0 => true, Self::Collection(items) if !items.is_empty() && items.iter().all(|x| x.check_validity()) => true, _ => false } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { *pages > 0 && match discount { Some(discount) => *discount >= 0 && *discount <= 50, None => true, } } BookItem::EBook(title, (_, second)) => !title.is_empty() && *second > 0, BookItem::Collection(book_items) => { !book_items.is_empty() && book_items.iter().all(|x| x.check_validity()) } BookItem::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool{ match self { BookItem::Book { pages, discount } => *pages > 0 && discount.map_or(true, |d| d <= 50 && d >= 0), BookItem::EBook(title, (_x, y)) => !title.is_empty() && *y > 0, BookItem::Collection(items) => !items.is_empty() && items.iter().all(|item| item.check_validity()), BookItem::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool{ match self { BookItem::Book { pages, discount } => *pages > 0 && discount.map_or(true, |d| d <= 50 && d >= 0), BookItem::EBook(title, (_x, y)) => !title.is_empty() && *y > 0, BookItem::Collection(items) => !items.is_empty() && items.iter().all(|item| item.check_validity()), BookItem::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Clone, Debug)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount} => *pages > 0 && ((*discount >= Some(0) && *discount <= Some(50)) || *discount == None), BookItem::EBook(title, (_b, c)) => title != "" && *c > 0, BookItem::Collection(books) => !books.is_empty() && books.iter().all(|b| b.check_validity()), BookItem::OutOfPrint => false } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } if *pages > 0 => match discount { Some(d) => 0 <= *d && *d <= 50, None => true, }, BookItem::EBook(title, (_, f)) => !title.is_empty() && *f > 0, BookItem::Collection(c) => !c.is_empty() && c.iter().all(|b| b.check_validity()), _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn clone(&self) -> Self { match self { BookItem::Book { pages, discount } => BookItem::Book { pages: *pages, discount: discount.clone(), }, BookItem::EBook(title, tuple) => BookItem::EBook(title.clone(), *tuple), BookItem::Collection(items) => BookItem::Collection(items.iter().map(|item| item.clone()).collect()), BookItem::OutOfPrint => BookItem::OutOfPrint, } } pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { if let Some(discount) = discount { (*discount <= 50) & (*pages >= 0) & (*discount >= 0) } else { *pages >= 0 } }, BookItem::EBook(title, tuple) => { !title.is_empty() & (tuple.1 > 0) }, BookItem::Collection(items) => { if items.is_empty() { return false; } items.iter().all(|item| item.check_validity()) }, BookItem::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
pub const MIN_PAGES: i32 = 1;pub const MAX_DISCOUNT: i32 = 50;#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { Self::Book { pages, discount } => Self::is_valid_book(*pages, *discount), Self::EBook(title, fields) => Self::is_valid_ebook(title, fields), Self::Collection(items) => !items.is_empty() && items.iter().all(Self::check_validity), Self::OutOfPrint => false, } } fn is_valid_book(pages: i32, discount: Option<i32>) -> bool { pages >= MIN_PAGES && discount.map_or(true, |d| (0..=MAX_DISCOUNT).contains(&d)) } fn is_valid_ebook(title: &str, version: &(i32, i32)) -> bool { !title.is_empty() && version.1 > 0 }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug,Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount} if *pages > 0 => if let Some(discount) = discount { *discount >= 0 && *discount <= 50 } else { true }, BookItem::EBook(title, (_, b)) if title.len() > 0 && *b > 0 => true, BookItem::Collection(collection) if !collection.is_empty() => collection.iter().all(|item| item.check_validity()), _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount} if *pages > 0 => if let Some(d) = discount { *d <= 50 && *d >= 0 } else { true }, BookItem::EBook(title, (_, y)) if !title.is_empty() && *y > 0 => true, BookItem::Collection(items) => !items.is_empty() && items.iter().all(|item| item.check_validity()), BookItem::OutOfPrint => false, BookItem::Book { .. } => false, BookItem::EBook(_, _) => false } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { Self::Book { pages, discount: None } if *pages > 0 => true, Self::Book { pages, discount: Some(discount) } if (*pages > 0) && (*discount >= 0) && (*discount <= 50) => true, Self::EBook (title, (_, value1)) if !title.is_empty() && (*value1 > 0) => true, Self::Collection (items) if !items.is_empty() && items.iter().all(|item| item.check_validity()) => true, _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { Self::Book { pages, discount } if *pages > 0 && (discount.is_none() || discount.unwrap() >= 0 && discount.unwrap() <= 50) => true, Self::EBook(a, (_, c)) if !a.is_empty() && *c > 0 => true, Self::Collection(v) if !v.is_empty() && v.iter().all(|item| item.check_validity()) => true, _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { fn is_valid_book(pages: i32, discount: Option<i32>) -> bool { pages > 0 && discount.map_or(true, |d| (0..=50).contains(&d)) } fn is_valid_ebook(msg: &String, b: &i32) -> bool { !msg.is_empty() && *b > 0 } fn is_valid_collection(items: &[BookItem]) -> bool { !items.is_empty() && items.iter().all(|x| x.check_validity()) } pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => Self::is_valid_book(*pages, *discount), BookItem::EBook(msg, (_, b)) => Self::is_valid_ebook(msg, b), BookItem::Collection(items) => Self::is_valid_collection(items), _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book {pages , discount } if *pages > 0 && (*discount == None || ( discount.unwrap() >=0 && discount.unwrap() <=50 )) => true, BookItem::EBook(msg, (_a, b)) if !msg.is_empty() && *b > 0 => true, BookItem::Collection(items) if !items.is_empty() && items.iter().all(|x| x.check_validity())=> true, _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book{pages, discount} if *pages > 0 && (*discount == None || (discount.unwrap() >= 0 && discount.unwrap() <= 50) ) => true, BookItem::EBook(title, (first, second)) if !title.is_empty() && *second > 0 => true, BookItem::Collection(vec) if !vec.is_empty() => { for item in vec { if !item.check_validity() { return false } } true }, _ => false } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug,Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { return match self { Self::Book{pages : p, discount : None} if *p > 0 => true, Self::Book{pages : p, discount : Some(_d @ 0..=50 )} if *p > 0 => true, Self::EBook(n,(_,r)) if !n.is_empty() && *r > 0 => true, Self::Collection(t) if !t.is_empty() && t.iter().all(|e| e.check_validity()) => true, _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug,Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { return match self { Self::Book{pages : p, discount : None} if *p > 0 => true, Self::Book{pages : p, discount : Some(d)} if *p > 0 && *d >=0 && *d <= 50 => true, Self::EBook(n,(_,r)) if !n.is_empty() && *r > 0 => true, Self::Collection(t) if !t.is_empty() && t.iter().all(|e| e.check_validity()) => true, _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book{pages, discount} => { *pages > 0 && discount.map_or(true, |d| d >= 0 && d <= 50) }, BookItem::EBook(title, (_, second)) => !title.is_empty() && *second > 0, BookItem::Collection(items) => !items.is_empty() && items.iter().all(|item| item.check_validity()), _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book{pages, discount} => { *pages > 0 && discount.map_or(true, |d| d >= 0 && d <= 50) }, BookItem::EBook(title, (_, second)) => !title.is_empty() && *second > 0, BookItem::Collection(items) => !items.is_empty() && items.iter().all(|item| item.check_validity()), _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book {pages, discount} => { return *pages >= 0 && discount.map_or(true, |d| d >= 0 && d <= 50); }, BookItem::EBook(title, (l, r)) => !title.is_empty() && *r > 0, BookItem::Collection(items) => items.len() > 0 && items.iter().all(|i| i.check_validity()), BookItem::OutOfPrint => false, _ => panic!("unknown book item"), } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { *pages > 0 && discount.map_or(true, |d| d <= 50 && d >= 0) } BookItem::EBook(title, tuple) => !title.is_empty() && tuple.0 > 0 && tuple.1 > 0, BookItem::Collection(items) => { items.len() > 0 && items.iter().all(|item| item.check_validity()) } BookItem::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { Self::Book { pages: 1.., discount: Some(0..=50) | None } | Self::EBook(_, (_, 1..)) | Self::Collection(_) if self.non_pattern_validity_check() => true, Self::OutOfPrint | _ => false } } /// internal function used for if-guards fn non_pattern_validity_check(&self) -> bool { // fricken expressions not being pattern-able lol match self { Self::EBook(string, (_, _)) => !string.is_empty(), Self::Collection(vec) => { if vec.is_empty() { return false } vec.iter().all(Self::check_validity) }, Self::Book {..} | Self::OutOfPrint => true } } }// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { Self::Book{pages: 1.., discount: None | Some(0..=50)} => true, Self::EBook(title, (_, 1..)) if !title.is_empty() => true, Self::Collection(items) if !items.is_empty() && items.iter().all(|book| book.check_validity()) => true, _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match &self { BookItem::Book{pages,discount} => { if *pages < 1 { return false } match discount { None => return true, _ => { if discount.unwrap() > 50 || discount.unwrap() < 0 { return false } } } return true }, BookItem::EBook(n, (_x, y)) => { if n.len() == 0 { return false } if *y < 1 { return false } return true }, BookItem::Collection(n) => { if n.len() == 0 { return false } for item in n { if !item.check_validity() { return false } } return true }, BookItem::OutOfPrint => { false } } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool{ match self { BookItem::Book { pages, discount } => { if *pages <= 0 { return false; } match discount { Some(d) => { if *d < 0 || *d > 50 { return false; } return true; }, None => return true, } }, BookItem::EBook(title, tuple) => { if title.is_empty() { return false; } if tuple.1 <= 0 { return false; } return true; }, BookItem::Collection(book_items) => { if book_items.is_empty() { return false; } for book in book_items.iter() { if !book.check_validity() { return false; } } return true; }, BookItem::OutOfPrint => return false, } } // TODO: Finish the implementation}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount: None } => *pages > 0, BookItem::Book { pages, discount: Some(discount) } => { *pages > 0 && *discount >= 0 && *discount <= 50 } BookItem::EBook(string, (_, second_field)) => !string.is_empty() && *second_field > 0, BookItem::Collection(book_items) => { !book_items.is_empty() && book_items.iter().all(|book_item| book_item.check_validity()) }, BookItem::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { Self::Book { pages: 1.., discount: None | Some(0..=50) } => true, Self::EBook(title, (_, 1..)) if !title.is_empty() => true, Self::Collection(i) if !i.is_empty() && i.iter().all(|x| x.check_validity()) => true, _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages: 1.., discount: None | Some(0..=50) } => true, // BookItem::Book { pages, discount } => { // if *pages <= 0 { // return false; // } // if let Some(discount) = discount { // return *discount >= 0 && *discount <= 50; // } // return true // }, BookItem::EBook(title, (_, b)) => title.len() > 0 && *b > 0, BookItem::Collection(books) => books.len() > 0 && books.iter().all(|book| book.check_validity()), BookItem::OutOfPrint => false, _ => false } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { if *pages <= 0 { return false; } if let Some(discount) = discount { return *discount >= 0 && *discount <= 50; } return true }, BookItem::EBook(title, (_, b)) => title.len() > 0 && *b > 0, BookItem::Collection(books) => books.len() > 0 && books.iter().all(|book| book.check_validity()), BookItem::OutOfPrint => false } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages: 1.., discount: None | Some(0..=50) } => true, BookItem::EBook(title, (_, 1..)) if !title.is_empty() => true, BookItem::Collection(vec) if !vec.is_empty() && vec.iter().all(BookItem::check_validity) => true, _ => false } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { if let Some(d) = discount { return (*d >= 0) && (*d <= 50) && (*pages > 0); } else { return *pages > 0 } } BookItem::EBook(title, (_, i)) => { !title.is_empty() && (*i > 0) } BookItem::Collection(vec) => { !vec.is_empty() && vec.iter().map(|bi| bi.check_validity()).all(|x| x) } BookItem::OutOfPrint => false } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { Self::Book { pages: 1.., discount: None | Some(0..=50) } => true, Self::EBook(title, (_, 1..)) if !title.is_empty() => true, Self::Collection(c) if !c.is_empty() && c.iter().all(BookItem::check_validity) => true, _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { Self::Book { pages, discount: None } if *pages > 0 => true, Self::Book { pages, discount: Some(d) } if *pages > 0 && (0..=50).contains(d) => true, Self::EBook(title, (_, i)) if !title.is_empty() && *i > 0 => true, Self::Collection(c) if !c.is_empty() && c.iter().all(BookItem::check_validity) => true, _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => *pages > 0 && discount.map_or(true, |d| d >= 0 && d <= 50), BookItem::EBook(title, (width, height)) => { !title.is_empty() && *width > 0 && *height > 0 } BookItem::Collection(items) => { !items.is_empty() && items.iter().all(|item| item.check_validity()) } BookItem::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { Self::Book { pages, discount } => { if let Some(d) = discount { *pages > 0 && *d >= 0 && *d <= 50 } else { *pages > 0 } } Self::EBook(title, (width, height)) => { !title.is_empty() && *width > 0 && *height > 0 } Self::Collection(items) => { if items.is_empty() { false } else { items.iter().all(|item| item.check_validity()) } } Self::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { Self::Book { pages, discount } => { if let Some(d) = discount { *pages > 0 && *d >= 0 && *d <= 50 } else { *pages > 0 } } Self::EBook(title, (width, height)) => { !title.is_empty() && *width > 0 && *height > 0 } Self::Collection(items) => { if items.is_empty() { false } else { items.iter().all(|item| item.check_validity()) } } Self::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}