vouch_lib/extension/
common.rs

1use anyhow::Result;
2
3#[derive(Debug, Clone, Hash, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
4pub struct VersionError(String);
5
6impl VersionError {
7    pub fn from_missing_version() -> Self {
8        Self("Missing version number".to_string())
9    }
10
11    pub fn from_parse_error(raw_version_number: &str) -> Self {
12        Self(format!("Version parse error: {}", raw_version_number))
13    }
14
15    pub fn message(&self) -> String {
16        self.0.clone()
17    }
18}
19
20pub type VersionParseResult = std::result::Result<String, VersionError>;
21
22/// A dependency as specified within a dependencies definition file.
23#[derive(Clone, Debug, Hash, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
24pub struct Dependency {
25    pub name: String,
26    pub version: VersionParseResult,
27}
28
29/// A dependencies specification file found from inspecting the local filesystem.
30#[derive(Clone, Debug, Hash, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
31pub struct DependenciesSpec {
32    /// Absolute file path for dependencies specification file.
33    pub path: std::path::PathBuf,
34
35    /// Dependencies registry host name.
36    pub registry_host_name: String,
37
38    /// Dependencies specified within the dependencies specification file.
39    pub dependencies: Vec<Dependency>,
40}
41
42#[derive(Debug, Clone, Hash, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
43pub struct RegistryPackageMetadata {
44    pub registry_host_name: String,
45    pub human_url: String,
46    pub artifact_url: String,
47    // True if this registry is the primary registry, otherwise false.
48    pub is_primary: bool,
49    // Included here incase package version was not given but found.
50    pub package_version: String,
51}
52
53pub trait FromLib: Extension + Send + Sync {
54    /// Initialize extension from a library.
55    fn new() -> Self
56    where
57        Self: Sized;
58}
59
60pub trait FromProcess: Extension + Send + Sync {
61    /// Initialize extension from a process.
62    fn from_process(
63        process_path: &std::path::PathBuf,
64        extension_config_path: &std::path::PathBuf,
65    ) -> Result<Self>
66    where
67        Self: Sized;
68}
69
70pub trait Extension: Send + Sync {
71    // Returns extension short name.
72    fn name(&self) -> String;
73
74    // Returns supported registries host names.
75    fn registries(&self) -> Vec<String>;
76
77    /// Identify local package dependencies.
78    fn identify_local_dependencies(
79        &self,
80        working_directory: &std::path::PathBuf,
81    ) -> Result<Vec<DependenciesSpec>>;
82
83    /// Query package registries for package metadata.
84    fn registries_package_metadata(
85        &self,
86        package_name: &str,
87        package_version: &Option<&str>,
88    ) -> Result<Vec<RegistryPackageMetadata>>;
89}