Files
raven/crates/core/src/dependency.rs
2026-02-15 17:39:48 -05:00

51 lines
1.5 KiB
Rust

use semver::Version;
use serde::{Deserialize, Serialize};
use crate::package::PackageHandler;
/// Data struct
#[derive(Debug, Clone, Deserialize, Serialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Dependency {
name: String,
#[serde(skip_serializing_if = "<Option<_>>::is_none")]
version: Option<Version>,
pub checksum: String,
#[serde(skip_serializing_if = "<Option<_>>::is_none")]
source: Option<String>, // Path / URL
}
impl Dependency {
/// Returns a path to the dependency in local storage
/// if there is one.
pub fn local_path(&self) -> String {
if self.source.as_ref().is_some_and(|path| !is_url(path)) {
return self.source.clone().unwrap();
}
// TODO: Convert from reverse domain name to path.
return self.name.clone();
}
pub fn is_updated(&self) -> crate::Result<bool> {
// If the path is local and that file has not been updated.
Ok(self.source.as_ref().is_some_and(|path| is_url(path))
&& self.checksum == sha256::try_digest(self.source.as_ref().unwrap())?)
}
}
impl From<PackageHandler> for Dependency {
fn from(value: PackageHandler) -> Self {
Dependency {
name: value.name(),
version: Some(value.version()),
checksum: String::new(),
source: None,
}
}
}
/// TODO: This is just a placeholder at present.
fn is_url<S: ToString>(_path: S) -> bool {
return false;
}