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 >= 0Option<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{            BookItem::Book{pages,discount} => {                let discount_valid = match discount{                    Some(val) => (0..=50).contains(val),                    None => true                };                return *pages > 0 && discount_valid;            },            BookItem::EBook(title,(_,count)) => !title.is_empty() && *count > 0,            BookItem::Collection(book_list) =>{                return !book_list.is_empty() && book_list.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 {                    return false;                } else {                    match discount {                        Some(amount) => return *amount >= 0 && *amount <= 50,                        None => return true,                    }                }            }            BookItem::EBook(title, (_, b)) => !title.is_empty() && *b > 0,            BookItem::Collection(book_items) => {                !book_items.is_empty() && book_items.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 {    // TODO: Finish the implementation    pub fn check_validity(&self) -> bool {        match self {            BookItem::Book { pages, discount }            if *pages > 0 && {                if discount.is_some() && discount.unwrap() >= 0 && discount.unwrap() <= 50 {                    true                } else {                    false                }            } => true,            BookItem::Book { pages, discount }             if *pages > 0 && discount.is_none() => true,            BookItem::Book { pages, discount } => false,            BookItem::EBook(title, (_, b))            if !title.is_empty() && *b > 0 => true,            BookItem::EBook(_, (_, _)) => false,            BookItem::Collection(list)            if !list.is_empty() && {                let mut result = Vec::<bool>::new();                for item in list.iter() {                    let item_result = item.check_validity();                    result.push(item_result);                }                if result.contains(&false) {                    false                } else {                    true                }            } => true,            BookItem::Collection(list) => 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 {    pub fn check_validity(&self) -> bool {        match &self {            BookItem::Book { pages, discount } => {                if *pages <= 0 {                    return false;                }                if let Some(d) = discount {                    *d >= 0 && *d <= 50                } else {                    true                }            }            BookItem::EBook(title, (_, sec)) => !title.is_empty() && *sec > 0,            BookItem::Collection(items) => {                !items.is_empty() && !items.iter().any(|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: Some(discount),            } if *discount >= 0 && *discount <= 50 && *pages > 0 => true,            BookItem::Book {                pages,                discount: None,            } if *pages > 0 => true,            BookItem::EBook(a, b) => !a.is_empty() && b.1 > 0,            BookItem::Collection(book_items) => {                !book_items.is_empty() && book_items.iter().all(|x| x.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: Some(discount),            } if *discount >= 0 && *discount <= 50 && *pages > 0 => true,            BookItem::Book {                pages,                discount: None,            } if *pages > 0 => true,            BookItem::EBook(title, (_, b)) if !title.is_empty() && *b > 0 => true,            BookItem::Collection(c) if !c.is_empty() && c.iter().all(|x| x.check_validity()) => true,            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: Some(discount),            } if *pages > 0 && *discount >= 0 && *discount <= 50 => true,            BookItem::Book {                pages,                discount: None            } if *pages > 0 => true,            BookItem::EBook(a, (_, b)) if !a.is_empty() && *b > 0 => true,                        BookItem::Collection(v) => if v.is_empty() {false} else {                let res = v.iter().all(|item| item.check_validity());                if res {true} else {false}            }            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 {                    if let Some(d) = *discount {                        return d >= 0 && d <= 50;                    };                    return true                }                false            },            BookItem::EBook(s, (_, t2)) => {                !s.is_empty() && *t2 > 0            },            BookItem::Collection(books) => {                if !books.is_empty() {                    books.iter().all(|item| item.check_validity())                } else { 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 {            BookItem::Book{pages, discount} => {                if *pages > 0 {                    if let Some(dist) = *discount {                        return dist >= 0 && dist <= 50                    };                    return true                }                false            },            BookItem::EBook(s, (_, t2)) => {                !s.is_empty() && *t2 > 0            },            BookItem::Collection(books) => {                if !books.is_empty() {                    books.iter().all(|item| item.check_validity())                } else { 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 {    pub fn check_validity(&self) -> bool {        match self {            BookItem::Book { pages, discount } => {                if *pages > 0 && ((*discount).is_none() || *discount >= Some(0) && *discount <= Some(50)) {                    return true;                }            },            BookItem::EBook(name, info) => {                if !name.is_empty() && info.1 > 0 {                    return true;                }            },            BookItem::Collection(items) => {                if !items.is_empty() && items.iter().all(|item| item.check_validity()) {                    return true;                }            },            _ => return 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 && !discount.is_some_and(|x|x < 0 || x>50){return true}false},            BookItem::EBook(title, (a, b)) => {if !title.is_empty() && *a > 0 && *b >0 { return true}false},                       BookItem::Collection(v) => {if !v.is_empty() && v.iter().all(|item|item.check_validity()){return true}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 } => match *pages {                p if p > 0 => match discount {                    Some(d) => *d >= 0 && *d <= 50,                    _ => true,                },                _ => false,            },            BookItem::EBook(title, (_, second)) => !title.is_empty() && *second > 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 :Some(discount)} => {                if *pages >0 && *discount>=0 && *discount <=50 {                    return true;                }else{                    return false;                }            },            BookItem::Book{pages,discount : None} => {                if  *pages >0 {                    return true;                }else{                    return false;                }            },            BookItem::EBook(title,(n1,n2)) => {                if !title.is_empty() && *n1>0 && *n2>0 {                    return true;                }else{                    return false;                }            },            BookItem::Collection(list) => {                if !list.is_empty() && list.iter().all(|bookitem| bookitem.check_validity()) {                    return true;                }else{                    return false;                }            },            BookItem::OutOfPrint => {                return 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 => {                if let Some(d) = discount {                    if *d >= 0 && *d <= 50 {                        return true;                    }                } else {                    return true;                }                false             },            BookItem::EBook(t, (_, s)) if !t.is_empty() && *s > 0 => true,            BookItem::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 {    pub fn check_validity(&self)->bool{        match self{            BookItem::Book { pages, discount } => {                if *pages > 0 && discount.unwrap_or_default() >= 0 && discount.unwrap_or_default() <= 50{                    return true;                } else{                    return false;                }            },            BookItem::EBook(title, (b, c))=>{                if title.len() > 0 && *c > 0{                    return true;                }else{                    return false;                }            },            BookItem::Collection(v)=>{                if !v.is_empty() && v.iter().all(|item| item.check_validity()){                    return true;                }else{                    return 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: None,            } => *pages > 0,            BookItem::Book {                pages,                discount: Some(discount),            } => *pages > 0 && (0..50).contains(discount),            BookItem::EBook(str, (_, second)) => !str.is_empty() && *second > 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 {    pub fn check_validity(&self) -> bool {        match self {            BookItem::Book { pages, discount } => {                if pages > &0 && discount.unwrap_or_default() >= 0 && discount.unwrap_or_default() <= 50 {                    return true;                } else {                    return false;                }            },            BookItem::EBook( string, (int1, int2) ) => {                if !string.is_empty() && int2 > &0 && int1 > &0 {                    return true;                } else {                    return false;                }            },            BookItem::Collection( vector ) => {                if !vector.is_empty() && vector.iter().all(|item| item.check_validity()) {                    return true;                } else {                    return 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::Book { pages, discount } => {                if *pages <= 0 {                    return false;                }                if let Some(x) = *discount {                    return x >= 0 && x <= 50;                }                 return true;            },            Self::EBook (s, (_, i2)) => {                return !s.is_empty() && *i2 > 0;            },            Self::Collection(c) => {                if c.is_empty() {                    return false;                }                                if !c.iter().all(|item| item.check_validity()) {                    return false;                }                return true;            },            _ => {                return 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: Some(discount) } if *pages > 0 && *discount >= 0 && *discount <= 50 => true,            BookItem::Book { pages, discount: None } if *pages > 0 => true,            BookItem::Book { pages: _, discount: _ } => false,            BookItem::EBook(title, (_, y)) if !title.is_empty() && *y > 0 => true,            BookItem::EBook(_, (_, _)) => false,            BookItem::Collection(collection) => !collection.is_empty() && collection.into_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 && *discount == None) ||                (*pages > 0 && *discount >= Some(0 as i32) && *discount <= Some(50))            => true,            BookItem::EBook(x, (y, z)) if !x.is_empty() && *y > 0 && *z > 0 => true,            BookItem::Collection(x) if !x.is_empty() && x.iter().all(|item| item.check_validity()) =>                true,            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 {            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"    );}