1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use toml::from_str;

use xcell_types::TypeMetaInfo;

use crate::{config::table::TableLineMode, MergeRules};

use super::*;

#[derive(Clone, Debug)]
pub struct ProjectConfig {
    pub root: PathBuf,
    /// 当前版本号
    pub version: String,
    /// 包含的 excel 路径, 优先级最高
    pub include: String,
    /// 排除的 excel 模式, 优先级低于 include
    pub exclude: String,
    /// 行列排序模式
    pub line: TableLineMode,
    /// 类型解析模式
    pub typing: TypeMetaInfo,
    /// 合表模式
    pub merge: MergeRules,
    /// Unity 生成模式
    pub unity: UnityCodegen,
}

mod der;
mod ser;

impl ProjectConfig {
    pub fn new(workspace: &Path) -> Self {
        log::info!("工作目录: {}", workspace.display());
        let cfg = workspace.join("XCell.toml");
        let success;
        let mut config = match Self::load_toml(&cfg) {
            Ok(o) => {
                success = "成功";
                o
            }
            Err(e) => {
                success = "失败";
                log::error!("{}", e.with_path(&cfg));
                Default::default()
            }
        };
        config.root = workspace.to_path_buf();
        log::trace!("加载项目配置{success}, 当前配置\n{config:#?}");
        config
    }
    fn load_toml(file: &Path) -> XResult<Self> {
        if file.exists() {
            let text = read_to_string(file)?;
            Ok(from_str(&text)?)
        }
        else {
            Err(XError::table_error("XCell.toml 不存在"))
        }
    }
}

impl WorkspaceManager {
    pub fn get_relative(&self, file: &Path) -> XResult<PathBuf> {
        get_relative(file, &self.config.root)
    }
    pub fn disable_xml(&mut self) {
        self.config.unity.xml.enable = false;
    }
    pub fn disable_json(&self) {
        // self.config.unity.enable = false;
    }
    pub fn dry_run(&mut self) {
        self.disable_xml();
        self.disable_json();
    }
    pub fn clear(&self) -> XResult<()> {
        Ok(())
    }
}