maven_rs/
utils.rs

1pub(crate) mod parse;
2pub(crate) mod serde_utils;
3pub mod time;
4
5pub fn group_id_to_path(group_id: &str) -> String {
6    group_id.replace(".", "/")
7}
8
9pub fn group_id_and_artifact_id_to_path(group_id: &str, artifact_id: &str) -> String {
10    format!("{}/{}", group_id_to_path(group_id), artifact_id)
11}
12/// Concatenates the group_id, artifact_id, and version to create a path
13pub fn group_id_and_artifact_id_and_version_to_path(
14    group_id: &str,
15    artifact_id: &str,
16    version: &str,
17) -> String {
18    format!("{}/{}/{}", group_id_to_path(group_id), artifact_id, version)
19}
20
21/// Bug Files. Used for testing
22///
23/// When a bug is found a bug file is created with the section of the pom file that caused the bug.
24///
25/// Then the bug is added a test to ensure the bug is fixed. and can be used for future regression testing.
26///
27/// Files are stored in toml as they support multi line strings without escaping.
28#[cfg(any(test, feature = "bug-files"))]
29pub mod bug_testing {
30    use std::fmt::Display;
31
32    use serde::{Deserialize, Serialize};
33
34    use crate::pom::Dependency;
35
36    #[derive(Debug, Serialize, Deserialize, Clone, Default)]
37    pub struct BugFile {
38        pub source: Source,
39        /// The error message that was found
40        pub error: String,
41        #[serde(default)]
42        pub depends: Vec<DependBug>,
43    }
44    /// A dependency related to a bug
45    #[derive(Debug, Serialize, Deserialize, Clone, Default)]
46    pub struct DependBug {
47        pub xml: String,
48        pub expected: FoundBugDependency,
49    }
50    /// The source of the bug. Includes the group_id, artifact_id, version, and the file name.
51    #[derive(Debug, Serialize, Deserialize, Clone, Default)]
52    pub struct Source {
53        pub group_id: String,
54        pub artifact_id: String,
55        pub version: String,
56        pub file_name: String,
57    }
58    impl Display for Source {
59        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60            write!(
61                f,
62                "Group ID {}\n Artifact ID {}\n Version {}",
63                self.group_id, self.artifact_id, self.version
64            )
65        }
66    }
67    #[derive(Debug, Serialize, Deserialize, Clone, Default)]
68    pub struct FoundBugDependency {
69        pub group_id: String,
70        pub artifact_id: String,
71        pub version: String,
72        pub depend_type: Option<String>,
73        pub scope: Option<String>,
74        pub classifier: Option<String>,
75    }
76    impl From<FoundBugDependency> for Dependency {
77        fn from(val: FoundBugDependency) -> Self {
78            Dependency {
79                group_id: val.group_id,
80                artifact_id: val.artifact_id,
81                version: Some(val.version.parse().expect("Failed to parse version")),
82                depend_type: val.depend_type,
83                scope: val.scope,
84                classifier: val.classifier,
85            }
86        }
87    }
88
89    #[cfg(test)]
90    pub fn get_bugs_path() -> std::path::PathBuf {
91        let cargo_home = env!("CARGO_MANIFEST_DIR");
92        std::path::PathBuf::from(cargo_home)
93            .join("tests")
94            .join("data")
95            .join("bugs")
96    }
97}