Merge book entries in multiple languages

This commit is contained in:
Jarrod Doyle 2024-02-05 16:53:18 +00:00
parent 7ca5c76082
commit df5d0041d3
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 41 additions and 12 deletions

View File

@ -78,13 +78,16 @@ impl Application for BookManagerApp {
let mut books: Vec<Element<'_, Self::Message>> = vec![];
if let Some(project) = &self.project {
for book in project.books.iter() {
books.push(text(book.file_name.clone()).into());
for (file_name, _) in project.books.iter() {
books.push(text(file_name.clone()).into());
}
}
let left_panel = scrollable(Column::with_children(books).spacing(10).padding(10))
.width(Length::Fixed(256.0));
let left_panel = column![
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]
.spacing(10)
.padding(10);

View File

@ -1,22 +1,24 @@
use std::{ffi::OsStr, path::PathBuf};
use std::{collections::HashMap, ffi::OsStr, path::PathBuf};
use walkdir::WalkDir;
#[derive(Debug, Clone)]
pub struct Book {
pub path: PathBuf,
pub file_name: String,
pub languages: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct ProjectState {
pub path: PathBuf,
pub books: Vec<Book>,
pub books: HashMap<String, Book>,
}
impl ProjectState {
pub fn new(path: PathBuf) -> Self {
let mut books = vec![];
let book_walker = WalkDir::new(path.join("books")).into_iter();
let search_path = path.join("books");
let mut books = HashMap::new();
let book_walker = WalkDir::new(search_path.clone()).into_iter();
for entry in book_walker.flatten() {
if entry.metadata().is_ok_and(|md| md.is_dir()) {
continue;
@ -24,10 +26,34 @@ impl ProjectState {
let extension = entry.path().extension().and_then(OsStr::to_str);
if extension.is_some_and(|ext| ext.to_lowercase() == "str") {
books.push(Book {
let language = entry
.path()
.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: entry.file_name().to_str().unwrap().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(),
// });
}
}