diff --git a/src/maven/settings.rs b/src/maven/settings.rs index 9e140df..6d86d51 100644 --- a/src/maven/settings.rs +++ b/src/maven/settings.rs @@ -6,6 +6,16 @@ use crate::{ }; pub fn get_settings() -> Result { + let settings_path = get_settings_path().map_err(|e| e.to_string())?; + get_settings_from_path(settings_path) +} + +pub fn get_settings_from_path(settings_path: PathBuf) -> Result { + let settings = fs::read_to_string(settings_path).map_err(|e| e.to_string())?; + get_settings_from_string(settings) +} + +pub fn get_settings_from_string(settings: String) -> Result { let mut local_repository = None; let mut interactive_mode = true; let mut use_plugin_registry = false; @@ -17,14 +27,8 @@ pub fn get_settings() -> Result { let mut active_profiles = vec![]; let mut plugin_groups = vec![]; - let settings_path = get_settings_path(); - let settings_file = fs::read_to_string(settings_path?).map_err(|e| e.to_string())?; - - for child in get_document(settings_file) - .map_err(|err| err.to_string())? - .root - .children - { + let root = get_document(settings).map_err(|err| err.to_string())?.root; + for child in root.children { match child.name.as_str() { "localRepository" => local_repository = child.text, "interactiveMode" => interactive_mode = child.text.map(|b| b == "true").unwrap_or(true), @@ -405,119 +409,107 @@ fn get_settings_path() -> Result { #[derive(Debug)] pub struct Settings { - local_repository: Option, - interactive_mode: bool, - use_plugin_registry: bool, - offline: bool, - proxies: Vec, - servers: Vec, - mirrors: Vec, - profiles: Vec, - active_profiles: Vec, - plugin_groups: Vec, + pub local_repository: Option, + pub interactive_mode: bool, + pub use_plugin_registry: bool, + pub offline: bool, + pub proxies: Vec, + pub servers: Vec, + pub mirrors: Vec, + pub profiles: Vec, + pub active_profiles: Vec, + pub plugin_groups: Vec, } #[derive(Debug)] -struct Server { - id: Option, - username: Option, - password: Option, - private_key: Option, - passphrase: Option, - file_permissions: Option, - directory_permissions: Option, - configuration: Option, //xsd:any +pub struct Server { + pub id: Option, + pub username: Option, + pub password: Option, + pub private_key: Option, + pub passphrase: Option, + pub file_permissions: Option, + pub directory_permissions: Option, + pub configuration: Option, //xsd:any } #[derive(Debug)] -struct Mirror { - id: Option, - mirror_of: Option, - name: Option, - url: Option, +pub struct Mirror { + pub id: Option, + pub mirror_of: Option, + pub name: Option, + pub url: Option, } #[derive(Debug)] -struct Proxy { - active: bool, - protocol: String, - username: Option, - password: Option, - port: usize, - host: Option, - non_proxy_hosts: Option, - id: Option, +pub struct Proxy { + pub active: bool, + pub protocol: String, + pub username: Option, + pub password: Option, + pub port: usize, + pub host: Option, + pub non_proxy_hosts: Option, + pub id: Option, } #[derive(Debug)] -struct Profile { - id: Option, - activation: Option, - properties: Vec, - repositories: Vec, - plugin_repositories: Vec, +pub struct Profile { + pub id: Option, + pub activation: Option, + pub properties: Vec, + pub repositories: Vec, + pub plugin_repositories: Vec, } #[derive(Debug)] -struct Activation { - active_by_default: bool, - jdk: Option, - os: Option, - property: Option, - file: Option, +pub struct Activation { + pub active_by_default: bool, + pub jdk: Option, + pub os: Option, + pub property: Option, + pub file: Option, } #[derive(Debug)] -struct ActivationOs { - name: Option, - family: Option, - arch: Option, - version: Option, +pub struct ActivationOs { + pub name: Option, + pub family: Option, + pub arch: Option, + pub version: Option, } #[derive(Debug)] -struct ActivationProperty { - name: Option, - value: Option, +pub struct ActivationProperty { + pub name: Option, + pub value: Option, } #[derive(Debug)] -struct ActivationFile { - missing: Option, - exists: Option, +pub struct ActivationFile { + pub missing: Option, + pub exists: Option, } #[derive(Debug)] -struct Property { - name: String, - value: Option, +pub struct Property { + pub name: String, + pub value: Option, } #[derive(Debug)] -struct Repository { - releases: Option, - snapshots: Option, - id: Option, - name: Option, - url: Option, - layout: String, +pub struct Repository { + pub releases: Option, + pub snapshots: Option, + pub id: Option, + pub name: Option, + pub url: Option, + pub layout: String, } #[derive(Debug)] -struct RepositoryPolicy { - enabled: bool, - update_policy: Option, - checksum_policy: Option, -} - -#[cfg(test)] -mod test { - - use super::*; - - #[test] - fn test() { - let settings = get_settings().expect("no fail"); - println!("{:?}", settings); - } +pub struct RepositoryPolicy { + pub enabled: bool, + pub update_policy: Option, + pub checksum_policy: Option, } diff --git a/src/xml/sax_parser.rs b/src/xml/sax_parser.rs index ab4bcf4..7afbfbc 100644 --- a/src/xml/sax_parser.rs +++ b/src/xml/sax_parser.rs @@ -46,10 +46,10 @@ impl<'a> SAXParser<'a> { fn parse(&mut self) -> Result<(), SaxError> { self.advance()?; - self.expect( - "", - "Content is not allowed in prolog.", - )?; + // self.expect( + // "", + // "Content is not allowed in prolog.", + // ).unwrap_or_default(); // not fatal TODO self.skip_whitespace()?; self.handler.start_document(); self.parse_elements() @@ -63,6 +63,12 @@ impl<'a> SAXParser<'a> { self.char_buffer.clear(); } self.advance()?; + if self.current == '?' { + self.expect( + "?xml version=\"1.0\" encoding=\"UTF-8\"?>", + "Content is not allowed in prolog.", + )?; + } if self.current == '!' { self.skip_comment()?; } else if self.current != '/' { diff --git a/tests/maven/mod.rs b/tests/maven/mod.rs index 9258f28..6ea4001 100644 --- a/tests/maven/mod.rs +++ b/tests/maven/mod.rs @@ -1,2 +1,3 @@ mod pom_parser_test; -mod project_parser_test; \ No newline at end of file +mod project_parser_test; +mod settings_test; \ No newline at end of file diff --git a/tests/maven/resources/settings.xml b/tests/maven/resources/settings.xml new file mode 100644 index 0000000..8f335b8 --- /dev/null +++ b/tests/maven/resources/settings.xml @@ -0,0 +1,47 @@ + + + + github + + + central + https://repo1.maven.org/maven2 + + + github + https://maven.pkg.github.com/shautvast/JsonToy + + true + + + + + + reflective + + + central + https://repo1.maven.org/maven2 + + + github + https://maven.pkg.github.com/shautvast/reflective + + true + + + + + + + + + github + shautvast + foobar + + + diff --git a/tests/maven/settings_test.rs b/tests/maven/settings_test.rs new file mode 100644 index 0000000..a248115 --- /dev/null +++ b/tests/maven/settings_test.rs @@ -0,0 +1,8 @@ +use undeepend::maven::settings::get_settings_from_string; + +#[test] +fn test() { + let settings = include_str!("../maven/resources/settings.xml").to_string(); + let settings = get_settings_from_string(settings).expect("no fail"); + assert!(!settings.profiles.is_empty()); +} \ No newline at end of file