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
80
81
82
83
84
85
86
87
88
89
//! This module provides configuration management for the `snipdoc`. It
//! includes functionality to load and manage configurations from a default YAML
//! file.
use std::path::Path;
use regex::Regex;
use serde::{Deserialize, Serialize};
use crate::errors::ConfigResult;
pub const DEFAULT_CONFIG_NAME: &str = "snipdoc-config.yml";
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct Config {
pub walk: WalkConfig,
#[serde(default)]
pub inject: InjectConfig,
}
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
#[allow(clippy::module_name_repetitions)]
pub struct InjectConfig {}
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
#[allow(clippy::module_name_repetitions)]
pub struct WalkConfig {
/// Patterns to include files.
#[serde(with = "serde_regex", default)]
pub includes: Vec<Regex>,
/// Patterns to exclude files.
#[serde(with = "serde_regex", default)]
pub excludes: Vec<Regex>,
}
impl Config {
/// Attempts to load the configuration from a default file under the given
/// path.
///
/// This function first checks if the default configuration file exists at
/// the given path. If it exists, it tries to load the configuration
/// from this file. If the file does not exist or contains invalid
/// content, it falls back to loading the default configuration.
///
/// # Returns
///
/// A [`Config`] instance loaded from the file if it exists and is valid, or
/// a default `Config` instance otherwise.
pub fn try_from_default_file(path: &Path) -> Self {
let maybe_config_exists = path.join(DEFAULT_CONFIG_NAME);
if maybe_config_exists.exists() {
match Self::from_file(maybe_config_exists.as_path()) {
Ok(config) => {
tracing::debug!(
path = %maybe_config_exists.display(),
"config file loaded"
);
return config;
}
Err(err) => {
tracing::error!(
path = %maybe_config_exists.display(),
err = %err,
"invalid config file content"
);
}
}
} else {
tracing::debug!(
path = %maybe_config_exists.display(),
"config not exists"
);
}
Self::default()
}
/// Loads the configuration from the specified file.
///
/// This function reads the file at the given path and attempts to
/// deserialize its content into a [`Config`] instance.
///
/// # Errors
///
/// Returns an error if the file cannot be read or if the content is
/// invalid.
pub fn from_file(path: &Path) -> ConfigResult<'_, Self> {
Ok(serde_yaml::from_reader(std::fs::File::open(path)?)?)
}
}