Merge book entries in multiple languages
This commit is contained in:
		
							parent
							
								
									7ca5c76082
								
							
						
					
					
						commit
						df5d0041d3
					
				|  | @ -78,13 +78,16 @@ impl Application for BookManagerApp { | ||||||
| 
 | 
 | ||||||
|         let mut books: Vec<Element<'_, Self::Message>> = vec![]; |         let mut books: Vec<Element<'_, Self::Message>> = vec![]; | ||||||
|         if let Some(project) = &self.project { |         if let Some(project) = &self.project { | ||||||
|             for book in project.books.iter() { |             for (file_name, _) in project.books.iter() { | ||||||
|                 books.push(text(book.file_name.clone()).into()); |                 books.push(text(file_name.clone()).into()); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         let left_panel = scrollable(Column::with_children(books).spacing(10).padding(10)) |         let left_panel = column![ | ||||||
|             .width(Length::Fixed(256.0)); |             text(books.len()), | ||||||
|  |             scrollable(Column::with_children(books).spacing(10).padding(10)) | ||||||
|  |                 .width(Length::Fixed(256.0)) | ||||||
|  |         ]; | ||||||
|         let right_panel = column![hello_world, controls, active_editor] |         let right_panel = column![hello_world, controls, active_editor] | ||||||
|             .spacing(10) |             .spacing(10) | ||||||
|             .padding(10); |             .padding(10); | ||||||
|  |  | ||||||
|  | @ -1,22 +1,24 @@ | ||||||
| use std::{ffi::OsStr, path::PathBuf}; | use std::{collections::HashMap, ffi::OsStr, path::PathBuf}; | ||||||
| use walkdir::WalkDir; | use walkdir::WalkDir; | ||||||
| 
 | 
 | ||||||
| #[derive(Debug, Clone)] | #[derive(Debug, Clone)] | ||||||
| pub struct Book { | pub struct Book { | ||||||
|     pub path: PathBuf, |     pub path: PathBuf, | ||||||
|     pub file_name: String, |     pub file_name: String, | ||||||
|  |     pub languages: Vec<String>, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[derive(Debug, Clone)] | #[derive(Debug, Clone)] | ||||||
| pub struct ProjectState { | pub struct ProjectState { | ||||||
|     pub path: PathBuf, |     pub path: PathBuf, | ||||||
|     pub books: Vec<Book>, |     pub books: HashMap<String, Book>, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl ProjectState { | impl ProjectState { | ||||||
|     pub fn new(path: PathBuf) -> Self { |     pub fn new(path: PathBuf) -> Self { | ||||||
|         let mut books = vec![]; |         let search_path = path.join("books"); | ||||||
|         let book_walker = WalkDir::new(path.join("books")).into_iter(); |         let mut books = HashMap::new(); | ||||||
|  |         let book_walker = WalkDir::new(search_path.clone()).into_iter(); | ||||||
|         for entry in book_walker.flatten() { |         for entry in book_walker.flatten() { | ||||||
|             if entry.metadata().is_ok_and(|md| md.is_dir()) { |             if entry.metadata().is_ok_and(|md| md.is_dir()) { | ||||||
|                 continue; |                 continue; | ||||||
|  | @ -24,10 +26,34 @@ impl ProjectState { | ||||||
| 
 | 
 | ||||||
|             let extension = entry.path().extension().and_then(OsStr::to_str); |             let extension = entry.path().extension().and_then(OsStr::to_str); | ||||||
|             if extension.is_some_and(|ext| ext.to_lowercase() == "str") { |             if extension.is_some_and(|ext| ext.to_lowercase() == "str") { | ||||||
|                 books.push(Book { |                 let language = entry | ||||||
|                     path: entry.path().to_owned(), |                     .path() | ||||||
|                     file_name: entry.file_name().to_str().unwrap().to_owned(), |                     .parent() | ||||||
|                 }); |                     .unwrap() | ||||||
|  |                     .strip_prefix(search_path.clone()) | ||||||
|  |                     .unwrap() | ||||||
|  |                     .to_str() | ||||||
|  |                     .unwrap() | ||||||
|  |                     .to_owned() | ||||||
|  |                     .to_lowercase(); | ||||||
|  |                 let file_name = entry | ||||||
|  |                     .file_name() | ||||||
|  |                     .to_str() | ||||||
|  |                     .unwrap() | ||||||
|  |                     .to_owned() | ||||||
|  |                     .to_lowercase(); | ||||||
|  |                 books | ||||||
|  |                     .entry(file_name.clone()) | ||||||
|  |                     .and_modify(|b: &mut Book| b.languages.push(language.clone())) | ||||||
|  |                     .or_insert(Book { | ||||||
|  |                         path: entry.path().to_owned(), | ||||||
|  |                         file_name, | ||||||
|  |                         languages: vec![language], | ||||||
|  |                     }); | ||||||
|  |                 // books.push(Book {
 | ||||||
|  |                 //     path: entry.path().to_owned(),
 | ||||||
|  |                 //     file_name: entry.file_name().to_str().unwrap().to_owned(),
 | ||||||
|  |                 // });
 | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue