pars_core/util/tree/
mod.rs

1mod convert;
2mod print;
3
4use std::path::Path;
5
6use bumpalo::collections::Vec as BumpVec;
7use colored::Color;
8use regex::Regex;
9
10use crate::config;
11
12#[derive(PartialEq, Eq, Clone, Copy)]
13pub enum FilterType {
14    Include,
15    Exclude,
16    Disable,
17}
18
19#[derive(Clone)]
20pub struct TreeConfig<'a> {
21    pub root: &'a Path,
22    pub target: &'a str,
23    pub filter_type: FilterType,
24    pub filters: Vec<Regex>,
25}
26
27pub struct TreePrintConfig {
28    pub dir_color: Option<Color>,
29    pub file_color: Option<Color>,
30    pub symbol_color: Option<Color>,
31    pub tree_color: Option<Color>,
32}
33
34impl<CFG: AsRef<config::cli::PrintConfig>> From<CFG> for TreePrintConfig {
35    fn from(config: CFG) -> Self {
36        Self {
37            dir_color: string_to_color_opt(&config.as_ref().dir_color),
38            file_color: string_to_color_opt(&config.as_ref().file_color),
39            symbol_color: string_to_color_opt(&config.as_ref().symbol_color),
40            tree_color: string_to_color_opt(&config.as_ref().tree_color),
41        }
42    }
43}
44
45#[derive(Debug)]
46pub enum NodeType {
47    File,
48    Dir,
49    Symlink,
50    Other,
51    Invalid,
52}
53
54#[derive(Debug)]
55pub struct TreeNode {
56    pub name: String,
57    pub parent: Option<usize>,
58    pub children: Vec<usize>,
59    pub node_type: NodeType,
60    pub symlink_target: Option<String>,
61    pub is_recursive: bool,
62    pub visible: bool,
63}
64
65pub struct DirTree<'a> {
66    pub map: BumpVec<'a, TreeNode>,
67    pub root: usize,
68}
69
70impl<T: AsRef<Path>> From<T> for NodeType {
71    fn from(value: T) -> Self {
72        let path = value.as_ref();
73        if !path.exists() {
74            NodeType::Invalid
75        } else if path.is_symlink() {
76            NodeType::Symlink
77        } else if path.is_file() {
78            NodeType::File
79        } else if path.is_dir() {
80            NodeType::Dir
81        } else {
82            NodeType::Other
83        }
84    }
85}
86
87pub fn string_to_color_opt(color_str: &str) -> Option<Color> {
88    color_str.parse::<Color>().ok()
89}