maven_rs/
settings.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4mod mirrors;
5mod servers;
6use crate::Error;
7pub use mirrors::*;
8pub use servers::*;
9use std::env;
10use std::io::BufReader;
11
12pub static MAVEN_FOLDER: &str = ".m2";
13pub static SETTINGS_FILE: &str = "settings.xml";
14#[derive(Debug, Clone, Serialize, Deserialize, Default)]
15#[serde(rename_all = "camelCase")]
16pub struct Settings {
17    pub local_repository: Option<PathBuf>,
18    pub interactive_mode: Option<bool>,
19    pub offline: Option<bool>,
20    #[serde(default)]
21    pub servers: Servers,
22    #[serde(default)]
23    pub mirrors: Mirrors,
24}
25impl Settings {
26    pub fn get_local_repository(&self) -> Option<PathBuf> {
27        self.local_repository.clone()
28    }
29    /// Attempts to read the local configuration file.
30    pub fn read_local_config() -> Result<Settings, Error> {
31        let result = get_settings_path().ok_or(Error::NoHomeDirectory)?;
32        if !result.exists() {
33            return Ok(Settings::default());
34        }
35        let file = std::fs::File::open(result)?;
36        quick_xml::de::from_reader(BufReader::new(file)).map_err(Error::from)
37    }
38    /// Returns the local repository or the default repository.
39    ///
40    /// If None is Returned Home Directory is not found.
41    pub fn get_local_repository_or_default(&self) -> Option<PathBuf> {
42        if let Some(local_repository) = &self.local_repository {
43            Some(local_repository.clone())
44        } else {
45            get_settings_directory().map(|dir| dir.join("repository"))
46        }
47    }
48}
49/// Returns the path to the .m2 folder
50///
51/// If the home directory is not found, None is returned.
52///
53/// # Example
54///
55/// ```
56/// use maven_rs::settings::get_settings_directory;
57/// let path = get_settings_directory();
58/// println!("{:?}", path);
59/// ``````
60pub fn get_settings_directory() -> Option<PathBuf> {
61    env::home_dir().map(|dirs| dirs.join(MAVEN_FOLDER))
62}
63
64/// Returns returns the path to the settings file.
65pub fn get_settings_path() -> Option<PathBuf> {
66    get_settings_directory().map(|dir| dir.join(SETTINGS_FILE))
67}
68
69#[cfg(test)]
70pub mod tests {
71
72    use crate::settings::{Server, Servers, Settings};
73
74    #[test]
75    pub fn test_to_string() {
76        let settings = Settings {
77            local_repository: None,
78            servers: Servers {
79                servers: vec![Server {
80                    id: "test".to_string(),
81                    username: Some("test".to_string()),
82                    password: Some("test".to_string()),
83                    ..Default::default()
84                }],
85            },
86            ..Default::default()
87        };
88
89        println!("{}", quick_xml::se::to_string(&settings).unwrap());
90    }
91
92    #[test]
93    pub fn test_read_local_config() {
94        let settings = Settings::read_local_config().unwrap();
95        println!("{:?}", settings);
96    }
97}