use crate::Result;
use crate::pack::{LocalPackRef, PackRef};
use std::str::FromStr;
#[derive(Debug, Clone)]
pub enum PartialAgentRef {
LocalPath(String),
PackRef(PackRef),
}
impl PartialAgentRef {
pub fn new(input: &str) -> Result<Self> {
if input.contains('@') {
let partial_pack_ref = PackRef::from_str(input)?;
Ok(PartialAgentRef::PackRef(partial_pack_ref))
} else {
Ok(PartialAgentRef::LocalPath(input.to_string()))
}
}
}
impl std::fmt::Display for PartialAgentRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PartialAgentRef::LocalPath(path) => write!(f, "{}", path),
PartialAgentRef::PackRef(pack_ref) => {
write!(f, "{}", pack_ref)?;
Ok(())
}
}
}
}
#[allow(unused)]
#[derive(Debug, Clone)]
pub enum AgentRef {
LocalPath(String),
PackRef(LocalPackRef),
}
#[cfg(test)]
mod tests {
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
use super::*;
use crate::_test_support::assert_contains;
#[test]
fn test_agent_ref_new_localpath() -> Result<()> {
let input = "path/to/local/file.rs";
let agent_ref = PartialAgentRef::new(input)?;
match agent_ref {
PartialAgentRef::LocalPath(ref path) => {
assert_eq!(path, input, "The local path should match the input string.");
}
_ => panic!("Expected AgentRef::LocalPath but got a different variant."),
}
Ok(())
}
#[test]
fn test_agent_ref_new_packref_without_subpath() -> Result<()> {
let input = "pro@coder";
let agent_ref = PartialAgentRef::new(input)?;
match agent_ref {
PartialAgentRef::PackRef(ref pack_ref) => {
assert_eq!(pack_ref.namespace, "pro", "Namespace should be 'pro'.");
assert_eq!(pack_ref.name, "coder", "Pack name should be 'coder'.");
assert!(pack_ref.sub_path.is_none(), "Sub path should be None.");
}
_ => panic!("Expected AgentRef::PackRef but got a different variant."),
}
Ok(())
}
#[test]
fn test_agent_ref_invalid() -> Result<()> {
let input = " jc @ coder/example/path ";
let err = PartialAgentRef::new(input).err().ok_or("Should return error")?;
assert_contains(
&err.to_string(),
"Pack namespace can only contain alphanumeric characters,",
);
Ok(())
}
}