use std::path::Path;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub(crate) enum PackageSource {
Installed,
Path { path: String },
Git { url: String, rev: Option<String> },
Bundled { collection: Option<String> },
}
pub(crate) fn infer_from_legacy_source_string(s: &str) -> PackageSource {
if s == "bundled" {
return PackageSource::Bundled { collection: None };
}
let p = Path::new(s);
if p.is_absolute() {
return PackageSource::Installed;
}
PackageSource::Git {
url: s.to_string(),
rev: None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn infer_git_url() {
let result =
infer_from_legacy_source_string("https://github.com/ynishi/algocline-bundled-packages");
assert_eq!(
result,
PackageSource::Git {
url: "https://github.com/ynishi/algocline-bundled-packages".to_string(),
rev: None,
}
);
}
#[test]
fn infer_local_copy() {
let tmp = tempfile::tempdir().unwrap();
let path = tmp.path().to_str().unwrap().to_string();
let result = infer_from_legacy_source_string(&path);
assert_eq!(result, PackageSource::Installed);
}
#[test]
fn infer_bundled() {
let result = infer_from_legacy_source_string("bundled");
assert_eq!(result, PackageSource::Bundled { collection: None });
}
#[test]
fn infer_non_existent_absolute_path_is_installed() {
let result =
infer_from_legacy_source_string("/nonexistent/path/that/should/never/exist_xyz");
assert_eq!(result, PackageSource::Installed);
}
#[test]
fn infer_relative_path_is_git() {
let result = infer_from_legacy_source_string("relative/path/pkg");
assert!(matches!(result, PackageSource::Git { .. }));
}
}