#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SourceScope {
User,
Project,
Temporary,
}
impl Default for SourceScope {
fn default() -> Self {
Self::Temporary
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SourceOrigin {
Package,
TopLevel,
}
impl Default for SourceOrigin {
fn default() -> Self {
Self::TopLevel
}
}
#[derive(Debug, Clone)]
pub struct SourceInfo {
pub path: String,
pub source: String,
pub scope: SourceScope,
pub origin: SourceOrigin,
pub base_dir: Option<String>,
}
#[derive(Debug, Clone)]
pub struct PathMetadata {
pub source: String,
pub scope: SourceScope,
pub origin: SourceOrigin,
pub base_dir: Option<String>,
}
pub fn create_source_info(path: impl Into<String>, metadata: &PathMetadata) -> SourceInfo {
SourceInfo {
path: path.into(),
source: metadata.source.clone(),
scope: metadata.scope.clone(),
origin: metadata.origin.clone(),
base_dir: metadata.base_dir.clone(),
}
}
pub fn create_synthetic_source_info(
path: impl Into<String>,
source: impl Into<String>,
scope: Option<SourceScope>,
origin: Option<SourceOrigin>,
base_dir: Option<String>,
) -> SourceInfo {
SourceInfo {
path: path.into(),
source: source.into(),
scope: scope.unwrap_or_default(),
origin: origin.unwrap_or_default(),
base_dir,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn create_from_metadata() {
let meta = PathMetadata {
source: "my-ext".into(),
scope: SourceScope::Project,
origin: SourceOrigin::Package,
base_dir: Some("/base".into()),
};
let info = create_source_info("/path/to/file", &meta);
assert_eq!(info.path, "/path/to/file");
assert_eq!(info.source, "my-ext");
assert_eq!(info.scope, SourceScope::Project);
assert_eq!(info.origin, SourceOrigin::Package);
assert_eq!(info.base_dir.as_deref(), Some("/base"));
}
#[test]
fn synthetic_defaults() {
let info = create_synthetic_source_info("p", "s", None, None, None);
assert_eq!(info.scope, SourceScope::Temporary);
assert_eq!(info.origin, SourceOrigin::TopLevel);
assert!(info.base_dir.is_none());
}
}