Improve code

This commit is contained in:
jaby 2022-10-16 16:08:12 +02:00
parent 685af072bb
commit a90ffb3952
4 changed files with 34 additions and 22 deletions

View File

@ -20,9 +20,9 @@ fn populate() -> Result<CDDesc, Error> {
folder
};
desc.root.borrow_mut().add_dir(folder);
desc.root.borrow_mut().add_file(file);
desc.root.borrow_mut().add_file(file2);
desc.add_dir(folder);
desc.add_file(file);
desc.add_file(file2);
desc.calculate_lbas();
Ok(desc)

View File

@ -1,5 +1,4 @@
use super::*;
use std::{cell::RefCell, rc::Rc};
const CURRENT_DIR_NAME:&'static str = "/x00";
const PARENT_DIR_NAME:&'static str = "/x01";
@ -23,8 +22,8 @@ impl DirectoryRecordMember {
}
}
pub fn collect_path_table_member(root: Rc<RefCell<Directory>>) -> Vec<PathTableMember> {
fn collect_path_table_for(collection: &mut Vec<PathTableMember>, dirs: &Vec<Rc<RefCell<Directory>>>, parent_id: usize) {
pub fn collect_path_table_member(root: SharedPtr<Directory>) -> Vec<PathTableMember> {
fn collect_path_table_for(collection: &mut Vec<PathTableMember>, dirs: &Vec<SharedPtr<Directory>>, parent_id: usize) {
let mut cur_dirs = Vec::new();
for dir in dirs {
@ -49,7 +48,7 @@ pub fn collect_path_table_member(root: Rc<RefCell<Directory>>) -> Vec<PathTableM
collection
}
pub fn collect_directory_record_member(files: &Vec<Rc<RefCell<File>>>, dirs: &Vec<Rc<RefCell<Directory>>>) -> Vec<DirectoryRecordMember> {
pub fn collect_directory_record_member(files: &Vec<SharedPtr<File>>, dirs: &Vec<SharedPtr<Directory>>) -> Vec<DirectoryRecordMember> {
let mut collection = Vec::new();
collection.push(DirectoryRecordMember::Directory{name: CURRENT_DIR_NAME.to_owned()});

View File

@ -1,5 +1,4 @@
use super::*;
use std::cell::RefCell;
pub type MemoryLayout = Vec<Layout>;
pub type MemoryLayoutMut = Vec<LayoutMut>;
@ -29,14 +28,14 @@ macro_rules! declare_memory_layout {
}
pub enum [< Layout$($val:camel),* >] {
SystemArea(Rc<RefCell<SystemArea>>),
PVD(Rc<RefCell<PrimaryVolumeDescriptor>>),
SystemArea(SharedPtr<SystemArea>),
PVD(SharedPtr<PrimaryVolumeDescriptor>),
PathTables,
Directory(Rc<RefCell<Directory>>),
File(Rc<RefCell<File>>)
Directory(SharedPtr<Directory>),
File(SharedPtr<File>)
}
fn [< add_dir_and_subdir $(_$val),* >]<'a>(layout: &mut Vec<[< Layout$($val:camel),* >]>, dir: Rc<RefCell<Directory>>) {
fn [< add_dir_and_subdir $(_$val),* >]<'a>(layout: &mut Vec<[< Layout$($val:camel),* >]>, dir: SharedPtr<Directory>) {
layout.push([< Layout$($val:camel),* >]::Directory(dir.clone()));
let $($val),* dir = dir.[< borrow $(_$val),* >]();

View File

@ -6,10 +6,16 @@ use layout::LayoutMut;
use tool_helper::Error;
use std::{cell::RefCell, rc::Rc};
pub type SharedPtr<T> = Rc<RefCell<T>>;
pub fn new_shared_ptr<T>(value: T) -> SharedPtr<T> {
Rc::new(RefCell::new(value))
}
pub struct CDDesc {
system_area: Rc<RefCell<SystemArea>>,
pvd: Rc<RefCell<PrimaryVolumeDescriptor>>,
pub root: Rc<RefCell<Directory>>
system_area: SharedPtr<SystemArea>,
pvd: SharedPtr<PrimaryVolumeDescriptor>,
root: SharedPtr<Directory>
}
impl CDDesc {
@ -17,7 +23,7 @@ impl CDDesc {
pub fn new() -> CDDesc {
match Directory::new("root") {
Ok(root) => CDDesc{system_area: Rc::new(RefCell::new(SystemArea::new())), pvd: Rc::new(RefCell::new(PrimaryVolumeDescriptor::new())), root: Rc::new(RefCell::new(root))},
Ok(root) => CDDesc{system_area: new_shared_ptr(SystemArea::new()), pvd: new_shared_ptr(PrimaryVolumeDescriptor::new()), root: new_shared_ptr(root)},
Err(error) => panic!("Creating root directory failed with: {}", error)
}
}
@ -30,6 +36,14 @@ impl CDDesc {
layout::DefaultLayoutMut::new(self)
}
pub fn add_dir(&mut self, dir: Directory) {
self.root.borrow_mut().add_dir(dir);
}
pub fn add_file(&mut self, file: File) {
self.root.borrow_mut().add_file(file);
}
pub fn calculate_lbas(&mut self) {
let mut path_table_properties = {
let mut size_bytes = 0;
@ -79,7 +93,7 @@ impl CDDesc {
}
}
fn for_each_dir_mut<T: Fn(&mut Directory)>(dir: Rc<RefCell<Directory>>, function: &T) {
fn for_each_dir_mut<T: Fn(&mut Directory)>(dir: SharedPtr<Directory>, function: &T) {
let mut dir = dir.borrow_mut();
function(&mut dir);
@ -116,8 +130,8 @@ impl PrimaryVolumeDescriptor {
pub struct Directory {
pub name: DirectoryName,
pub properties: Properties,
files: Vec<Rc<RefCell<File>>>,
dirs: Vec<Rc<RefCell<Directory>>>
files: Vec<SharedPtr<File>>,
dirs: Vec<SharedPtr<Directory>>
}
impl Directory {
@ -126,11 +140,11 @@ impl Directory {
}
pub fn add_dir(&mut self, dir: Directory) {
self.dirs.push(Rc::new(RefCell::new(dir)));
self.dirs.push(new_shared_ptr(dir));
}
pub fn add_file(&mut self, file: File) {
self.files.push(Rc::new(RefCell::new(file)));
self.files.push(new_shared_ptr(file));
}
fn update_content_size(&mut self) {