1use serde::{Deserialize, Serialize};
2
3const CONFIG_FILE: &str = "fus.toml";
4
5pub fn read() -> crate::Result<Config> {
6 let toml = std::fs::read(CONFIG_FILE)?;
7 Ok(toml::from_slice(&toml)?)
8}
9pub fn init() -> crate::Result<()> {
10 let mut modules = Vec::new();
11 for result in ignore::WalkBuilder::new("./")
12 .max_depth(Some(1))
13 .build()
14 .filter_map(|entry| {
15 entry
16 .map(|entry| {
17 if matches!(entry.file_type(), Some(x) if x.is_dir()) {
18 Some(entry)
19 } else {
20 None
21 }
22 })
23 .transpose()
24 })
25 {
26 let entry = result?;
27 let path = entry.path();
28 if let (Some(module_name), Some(path)) = (
29 path.file_name().and_then(|file_name| file_name.to_str()),
30 path.to_str(),
31 ) {
32 modules.push(Module {
33 name: module_name.to_string(),
34 includes: vec![Include {
35 glob: path.to_string(),
36 prefix_strip: None,
37 }],
38 destination: format!("$CONFIG_DIR/{}", module_name),
39 })
40 }
41 }
42 let toml = toml::to_string_pretty(&Config {
43 module: modules,
44 vars: toml::Value::Table(toml::map::Map::new()),
45 })?;
46 Ok(std::fs::write(CONFIG_FILE, toml)?)
47}
48
49#[derive(Serialize, Deserialize, Debug)]
50pub struct Config {
51 pub module: Vec<Module>,
52 pub vars: toml::Value,
53}
54#[derive(Serialize, Deserialize, Debug)]
55pub struct Module {
56 pub name: String,
57 pub destination: String,
58 pub includes: Vec<Include>,
59}
60
61#[derive(Serialize, Deserialize, Debug)]
62pub struct Include {
63 pub glob: String,
64 pub prefix_strip: Option<usize>,
65}