use crate::domain::author::Author; use crate::domain::book::Book; use std::path::PathBuf; /// Parses a given file path into a vector containing a `Book` object. /// /// # Arguments /// /// * `path` - A reference to a `PathBuf` that represents the file path to be parsed. /// /// # Returns /// /// * `Result, String>` - /// - On success, returns a `Vec` with a single `Book` object populated based on the input path. /// - On failure, returns an error `String` describing the issue. /// /// The function performs the following steps: /// /// 1. Creates a new instance of `Book`. /// 2. Sets the `title` of the `Book` to the string representation of the input path. /// 3. Creates a new instance of `Author`. /// 4. Sets the `first_name` of the `Author` to the string representation of the file extension of `path`. /// 5. Pushes the `Author` into the `author` vector of the `Book`. /// 6. Returns a `Vec` containing the newly created `Book`. /// /// # Panics /// /// The function will panic if the input path does not contain a file extension /// (i.e., when `path.extension()` returns `None`). /// /// # Example /// /// ```ignore /// use std::path::PathBuf; /// /// let path = PathBuf::from("example.txt"); /// let books = parse(&path).unwrap(); /// /// assert_eq!(books.len(), 1); /// assert_eq!(books[0].title, "example.txt"); /// assert_eq!(books[0].author[0].first_name, "txt"); /// ``` pub fn parse(path: &PathBuf) -> Result, String> { let mut book = Book::new(); book.title = path.to_string_lossy().to_string(); let mut author = Author::new(); author.first_name = path.extension().unwrap().to_string_lossy().to_string(); book.author.push(author); return Ok(vec![ book]); }