loop_lib/
config.rs

1use serde::{Deserialize, Serialize};
2use serde_json;
3use std::fs;
4
5/// Represents the configuration for the loop command.
6///
7/// This struct holds the configuration options that can be set in the .looprc file,
8/// such as directories to ignore.
9#[derive(Serialize, Deserialize, Debug)]
10pub struct LoopConfig {
11    pub ignore: Vec<String>,
12}
13
14/// Creates a .looprc file in the current directory with the default configuration.
15///
16/// This function creates a .looprc file in the current directory with the default configuration.
17/// The default configuration is a list of directories to ignore, such as .git.
18pub fn create_looprc() {
19    use std::env;
20
21    let config = LoopConfig {
22        ignore: vec![".git".to_string()],
23    };
24    let json = serde_json::to_string_pretty(&config).unwrap();
25    let file_path = ".looprc";
26    fs::write(file_path, &json).unwrap();
27    let full_path = env::current_dir().unwrap().join(file_path);
28    println!(
29        "Created .looprc file at {} with content: {}",
30        full_path.display(),
31        json
32    );
33}
34
35/// Reads the .looprc configuration file from the current directory.
36///
37/// This function attempts to read and parse the .looprc file, returning
38/// a LoopConfig struct with the parsed configuration. If the file doesn't
39/// exist or can't be parsed, it returns a default configuration.
40pub fn read_looprc() -> LoopConfig {
41    match fs::read_to_string(".looprc") {
42        Ok(contents) => serde_json::from_str(&contents).unwrap_or_else(|err| {
43            eprintln!("Failed to parse .looprc: {}", err);
44            LoopConfig { ignore: vec![] }
45        }),
46        Err(_err) => {
47            // Fail silently and continue
48            LoopConfig { ignore: vec![] }
49        }
50    }
51}