Sorting Elements

This commit is contained in:
Jaby 2022-10-11 20:44:29 +02:00 committed by Jaby
parent 0cb939e06a
commit 9d1bb67cd0
4 changed files with 93 additions and 18 deletions

View File

@ -24,12 +24,14 @@ fn populate() -> Result<CDDesc, Error> {
desc.root.add_file(file); desc.root.add_file(file);
desc.root.add_file(file2); desc.root.add_file(file2);
desc.update_dir_records();
Ok(desc) Ok(desc)
} }
fn run_main() -> Result<(), Error> { fn run_main() -> Result<(), Error> {
let desc = populate()?; let desc = populate()?;
println!("\n<== Planschbecken ==>");
for element in desc.get_memory_layout().iter() { for element in desc.get_memory_layout().iter() {
match element { match element {
Layout::SystemArea(_) => println!("SystemArea:"), Layout::SystemArea(_) => println!("SystemArea:"),

View File

@ -0,0 +1,38 @@
use super::*;
pub enum DirectoryRecordMember {
Directory{name: String},
File{name: String},
}
impl DirectoryRecordMember {
pub fn get_name(&self) -> &String {
match self {
DirectoryRecordMember::Directory{name} => name,
DirectoryRecordMember::File{name} => name,
}
}
}
pub fn collect_directory_record_member(files: &Vec<File>, dirs: &Vec<Directory>) -> Vec<DirectoryRecordMember> {
let mut collection = Vec::new();
for file in files {
if !file.content.is_hidden {
if let Some(name) = file.name.as_string() {
collection.push(DirectoryRecordMember::File{name});
}
}
}
for dir in dirs {
if !dir.content.is_hidden {
if let Some(name) = dir.name.as_string() {
collection.push(DirectoryRecordMember::Directory{name});
}
}
}
collection.sort_by(|a, b| {a.get_name().cmp(b.get_name())});
collection
}

View File

@ -1,3 +1,4 @@
mod helper;
pub mod layout; pub mod layout;
use cdtypes::types::cdstring::DString; use cdtypes::types::cdstring::DString;
@ -24,6 +25,18 @@ impl CDDesc {
pub fn get_memory_layout_mut(&mut self) -> layout::MemoryLayoutMut { pub fn get_memory_layout_mut(&mut self) -> layout::MemoryLayoutMut {
layout::DefaultLayoutMut::new(self) layout::DefaultLayoutMut::new(self)
} }
pub fn update_dir_records(&mut self) {
fn update_dir_record_of(dir: &mut Directory) {
dir.update_content();
for sub_dir in &mut dir.dirs {
update_dir_record_of(sub_dir);
}
}
update_dir_record_of(&mut self.root);
}
} }
pub struct SystemArea { pub struct SystemArea {
@ -45,8 +58,8 @@ impl PrimaryVolumeDescriptor {
} }
pub struct Directory { pub struct Directory {
name: DirectoryName, pub name: DirectoryName,
content: Content, pub content: Content,
files: Vec<File>, files: Vec<File>,
dirs: Vec<Directory> dirs: Vec<Directory>
} }
@ -67,6 +80,16 @@ impl Directory {
pub fn get_lba(&self) -> usize { pub fn get_lba(&self) -> usize {
self.content.lba self.content.lba
} }
fn update_content(&mut self) {
let dir_member = helper::collect_directory_record_member(&self.files, &self.dirs);
self.content.data.clear();
println!("{} updating content", self.name.as_string().unwrap_or("<No name>".to_owned()));
for member in dir_member {
println!(">>> {}", member.get_name());
}
}
} }
impl std::fmt::Display for Directory { impl std::fmt::Display for Directory {
@ -76,8 +99,8 @@ impl std::fmt::Display for Directory {
} }
pub struct File { pub struct File {
name: FileName, pub name: FileName,
content: Content pub content: Content
} }
impl File { impl File {
@ -106,11 +129,19 @@ impl DirectoryName {
let dir_name = dir_name.to_uppercase(); let dir_name = dir_name.to_uppercase();
Ok(DirectoryName{name: DString::from_str(dir_name.as_ref())?, len: dir_name.len()}) Ok(DirectoryName{name: DString::from_str(dir_name.as_ref())?, len: dir_name.len()})
} }
pub fn as_string(&self) -> Option<String> {
Some(self.as_str()?.to_owned())
}
fn as_str(&self) -> Option<&str> {
dstring_as_str(&self.name, self.len)
}
} }
impl std::fmt::Display for DirectoryName { impl std::fmt::Display for DirectoryName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", dstring_as_str(&self.name, self.len)) write!(f, "{}", self.as_str().unwrap_or("<???>"))
} }
} }
@ -137,17 +168,21 @@ impl FileName {
Ok(FileName{name: DString::from_str(name)?, len: name.len(), ext, ext_len}) Ok(FileName{name: DString::from_str(name)?, len: name.len(), ext, ext_len})
} }
pub fn as_string(&self) -> Option<String> {
if let Some(ext) = &self.ext {
Some(format!("{}.{};1", dstring_as_str(&self.name, self.len)?, dstring_as_str(&ext, self.ext_len.unwrap_or(0))?))
}
else {
Some(format!("{}", dstring_as_str(&self.name, self.len)?))
}
}
} }
impl std::fmt::Display for FileName { impl std::fmt::Display for FileName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(ext) = &self.ext { write!(f, "{}", self.as_string().unwrap_or("<???>".to_owned()))
write!(f, "{}.{};1", dstring_as_str(&self.name, self.len), dstring_as_str(&ext, self.ext_len.unwrap_or(0)))
}
else {
write!(f, "{}", dstring_as_str(&self.name, self.len))
}
} }
} }
@ -164,9 +199,9 @@ impl Default for Content {
} }
} }
fn dstring_as_str<const SIZE: usize>(string: &DString<SIZE>, len: usize) -> &str { fn dstring_as_str<const SIZE: usize>(string: &DString<SIZE>, len: usize) -> Option<&str> {
match std::str::from_utf8(&string.as_raw()[0..len]) { match std::str::from_utf8(&string.as_raw()[0..len]) {
Ok(str) => str, Ok(str) => Some(str),
Err(_) => "???", Err(_) => None,
} }
} }