Guest User

Untitled

a guest
Jan 22nd, 2023
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.25 KB | Source Code | 0 0
  1. #[derive(Clone, Debug)]
  2. pub enum FileTree {
  3.     File(String),
  4.     Directory(DirectoryData),
  5. }
  6.  
  7. impl Display for FileTree {
  8.     fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  9.        match self {
  10.            FileTree::File(file_name) => write!(f, "{}", file_name),
  11.            FileTree::Directory(directory_data) => write!(f, "{}", directory_data.name),
  12.        }
  13.    }
  14. }
  15.  
  16. #[derive(Clone, Debug)]
  17. pub struct DirectoryData {
  18.    depth: u16,
  19.    name: String,
  20.    path: String,
  21. }
  22.  
  23. pub struct TreeFolder {
  24.    current_depth: u16,
  25.    directory_queue: Vec<ItemToSearch>,
  26.    pub tree: Rc<RefCell<Tree<Box<FileTree>>>>,
  27. }
  28.  
  29. impl TreeFolder {
  30.    pub fn new() -> TreeFolder {
  31.        TreeFolder {
  32.            current_depth: 0,
  33.            directory_queue: Vec::new(),
  34.            tree: Rc::new(RefCell::new(Tree::new(Box::new(FileTree::Directory(DirectoryData {
  35.                depth: 0,
  36.                name: String::from("~"),
  37.                path: String::from("~"),
  38.            }))))),
  39.        }
  40.    }
  41.  
  42.    pub fn build_tree(&mut self, searcher:&impl Searcher, communicator: Box<dyn Communicate>, tcp_wrapper:&mut TcpWrapper) {
  43.        self.search_directory(Rc::clone(&self.tree), searcher, communicator, tcp_wrapper);
  44.    }
  45.  
  46.    fn search_directory(&mut self, parent: Rc<RefCell<Tree<Box<FileTree>>>>, searcher:&impl Searcher, communicator: Box<dyn Communicate>, tcp_wrapper:&mut TcpWrapper) {
  47.        let (path, depth) = {
  48.            let first = &parent.as_ref()
  49.                .borrow()
  50.                .root;
  51.            match &**first {
  52.                FileTree::Directory(directory_data) => (directory_data.path.clone(), directory_data.depth),
  53.                _ => panic!("First element is not a directory"),
  54.            }
  55.        };
  56.  
  57.        let _ = tcp_wrapper.send_and_receive(EventsToSend::Cwd(path.to_string()));
  58.        let lines = communicator.retrieve_lines(tcp_wrapper);
  59.  
  60.        for line in lines {
  61.            let file_data = FileData::new(line);
  62.            self.store_line_in_tree(Rc::clone(&parent), file_data, &path, depth);
  63.        }
  64.        if let Some(next) = searcher.get_next(&mut self.directory_queue) {
  65.            let next_parent = Rc::new(RefCell::new(Tree::new(next.item)));
  66.            self.search_directory(Rc::clone(&next_parent), searcher, communicator, tcp_wrapper);
  67.        }
  68.    }
  69.  
  70.    fn store_line_in_tree(&mut self, boxed_parent: Rc<RefCell<Tree<Box<FileTree>>>>, file_data: FileData, path:&str, depth: u16) {
  71.        if file_data.m_rights.starts_with('d') {
  72.            let new_directory = Box::new(FileTree::Directory(DirectoryData {
  73.                depth: depth + 1,
  74.                name: file_data.m_name.clone(),
  75.                path: format!("{}/{}", path, file_data.m_name),
  76.            }));
  77.            let as_tree = Tree::new(Box::clone(&new_directory));
  78.            (*boxed_parent).borrow_mut().push(as_tree);
  79.            let new_item = ItemToSearch {
  80.                parent: Rc::clone(&boxed_parent),
  81.                item: new_directory,
  82.            };
  83.            self.directory_queue.push(new_item);
  84.        } else {
  85.            let new_file = Box::new(FileTree::File(file_data.m_name));
  86.            (*boxed_parent).borrow_mut().push(Tree::new(new_file));
  87.        }
  88.    }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment