use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CrateSpec {
pub name: String,
pub path: Option<PathBuf>,
pub version: Option<String>,
pub git: Option<String>,
pub rev: Option<String>,
}
impl CrateSpec {
pub fn local(name: impl Into<String>, path: impl Into<PathBuf>) -> Self {
Self { name: name.into(), path: Some(path.into()), version: None, git: None, rev: None }
}
pub fn crates_io(name: impl Into<String>, version: impl Into<String>) -> Self {
Self { name: name.into(), path: None, version: Some(version.into()), git: None, rev: None }
}
pub fn git(name: impl Into<String>, url: impl Into<String>, rev: impl Into<String>) -> Self {
Self {
name: name.into(),
path: None,
version: None,
git: Some(url.into()),
rev: Some(rev.into()),
}
}
pub fn is_local(&self) -> bool {
self.path.is_some()
}
pub fn is_crates_io(&self) -> bool {
self.version.is_some() && self.git.is_none()
}
pub fn is_git(&self) -> bool {
self.git.is_some()
}
pub fn nix_source(&self) -> String {
if let Some(path) = &self.path {
format!("./{}", path.display())
} else if let Some(version) = &self.version {
format!("crates.io:{version}")
} else if let (Some(git), Some(rev)) = (&self.git, &self.rev) {
format!("git:{git}?rev={rev}")
} else {
"unknown".to_string()
}
}
}