maven_rs/
settings.rs

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use std::path::PathBuf;

use serde::{Deserialize, Serialize};
mod mirrors;
mod servers;
pub use mirrors::*;
pub use servers::*;

pub static MAVEN_FOLDER: &str = ".m2";
pub static SETTINGS_FILE: &str = "settings.xml";
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct Settings {
    pub local_repository: Option<PathBuf>,
    pub interactive_mode: Option<bool>,
    pub offline: Option<bool>,
    #[serde(default)]
    pub servers: Servers,
    #[serde(default)]
    pub mirrors: Mirrors,
}
impl Settings {
    pub fn get_local_repository(&self) -> Option<PathBuf> {
        self.local_repository.clone()
    }
}
#[cfg(feature = "local")]
pub mod directories {
    use super::{Settings, MAVEN_FOLDER, SETTINGS_FILE};
    use crate::Error;
    use std::io::BufReader;
    use std::path::PathBuf;

    /// Returns the path to the .m2 folder
    ///
    /// If the home directory is not found, None is returned.
    ///
    /// # Example
    ///
    /// ```
    /// use maven_rs::settings::directories::get_settings_directory;
    /// let path = get_settings_directory();
    /// println!("{:?}", path);
    /// ``````
    pub fn get_settings_directory() -> Option<PathBuf> {
        dirs::home_dir().map(|dirs| dirs.join(MAVEN_FOLDER))
    }

    /// Returns returns the path to the settings file.
    pub fn get_settings_path() -> Option<PathBuf> {
        get_settings_directory().map(|dir| dir.join(SETTINGS_FILE))
    }

    impl Settings {
        /// Attempts to read the local configuration file.
        pub fn read_local_config() -> Result<Settings, Error> {
            let result = get_settings_path().ok_or(Error::NoHomeDirectory)?;
            if !result.exists() {
                return Ok(Settings::default());
            }
            let file = std::fs::File::open(result)?;
            quick_xml::de::from_reader(BufReader::new(file)).map_err(Error::from)
        }
        /// Returns the local repository or the default repository.
        ///
        /// If None is Returned Home Directory is not found.
        pub fn get_local_repository_or_default(&self) -> Option<PathBuf> {
            if let Some(local_repository) = &self.local_repository {
                Some(local_repository.clone())
            } else {
                get_settings_directory().map(|dir| dir.join("repository"))
            }
        }
    }
}

#[cfg(all(test, feature = "local"))]
pub mod tests {
    use crate::settings::{Server, Servers, Settings};

    #[test]
    pub fn test_to_string() {
        let settings = Settings {
            local_repository: None,
            servers: Servers {
                servers: vec![Server {
                    id: "test".to_string(),
                    username: Some("test".to_string()),
                    password: Some("test".to_string()),
                    ..Default::default()
                }],
            },
            ..Default::default()
        };

        println!("{}", quick_xml::se::to_string(&settings).unwrap());
    }

    #[test]
    pub fn test_read_local_config() {
        let settings = Settings::read_local_config().unwrap();
        println!("{:?}", settings);
    }
}