use crate::types::name::BinaryName;
use crate::types::op::{ItemId, OpError, OpPath, Origin, SkipReason};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Operation {
Write {
path: OpPath,
content: Vec<u8>,
covers: Vec<ItemId>,
},
Copy {
path: OpPath,
file: ItemId,
},
Skip {
item: ItemId,
reason: SkipReason,
},
RequireBinary {
name: BinaryName,
},
Delete {
path: OpPath,
},
}
impl Operation {
pub fn require_binary(text: &str) -> Result<Self, OpError> {
BinaryName::new(text)
.map(|name| Self::RequireBinary { name })
.map_err(OpError::InvalidBinaryName)
}
#[must_use]
pub const fn path(&self) -> Option<&OpPath> {
match self {
Self::Write { path, .. } | Self::Copy { path, .. } | Self::Delete { path } => {
Some(path)
}
Self::Skip { .. } | Self::RequireBinary { .. } => None,
}
}
#[must_use]
pub fn covered(&self) -> &[ItemId] {
match self {
Self::Write { covers, .. } => covers,
Self::Copy { file, .. } => std::slice::from_ref(file),
Self::Skip { item, .. } => std::slice::from_ref(item),
Self::RequireBinary { .. } | Self::Delete { .. } => &[],
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Proposal {
pub op: Operation,
pub origin: Origin,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::name::{ItemName, PackName};
use crate::types::tree::TreePath;
type TestResult = Result<(), Box<dyn std::error::Error>>;
fn agent(pack: &str, name: &str) -> Result<ItemId, Box<dyn std::error::Error>> {
Ok(ItemId::agent(&PackName::new(pack)?, &ItemName::new(name)?))
}
fn file() -> Result<ItemId, Box<dyn std::error::Error>> {
Ok(ItemId::skill_file(
&PackName::new("core")?,
&ItemName::new("status")?,
&TreePath::from_parts(&["SKILL.md"]),
))
}
fn write(covers: Vec<ItemId>) -> Result<Operation, Box<dyn std::error::Error>> {
Ok(Operation::Write {
path: OpPath::new(".claude/x")?,
content: b"x".to_vec(),
covers,
})
}
fn shown(ids: &[ItemId]) -> Vec<String> {
ids.iter().map(ToString::to_string).collect()
}
#[test]
fn binary_name_refusal_maps_to_the_op_refusal() -> TestResult {
let refused = Operation::require_binary("Git")
.err()
.ok_or("Git is not a binary name")?;
assert_eq!(
refused.to_string(),
"require_binary: name: \"Git\" is not a valid name"
);
let accepted = Operation::require_binary("git")?;
assert!(matches!(accepted, Operation::RequireBinary { .. }));
Ok(())
}
#[test]
fn operation_lists_what_it_covers() -> TestResult {
let copy = Operation::Copy {
path: OpPath::new(".claude/x")?,
file: file()?,
};
assert_eq!(shown(copy.covered()), ["core/skill/status/SKILL.md"]);
let both = write(vec![agent("core", "coder")?, agent("core", "other")?])?;
assert_eq!(
shown(both.covered()),
["core/agent/coder", "core/agent/other"]
);
let skip = Operation::Skip {
item: agent("core", "coder")?,
reason: SkipReason::new("no")?,
};
assert_eq!(shown(skip.covered()), ["core/agent/coder"]);
assert!(Operation::require_binary("git")?.covered().is_empty());
assert!(write(Vec::new())?.covered().is_empty());
Ok(())
}
#[test]
fn operation_path_of_each_kind() -> TestResult {
let path = OpPath::new(".claude/x")?;
let write = write(Vec::new())?;
let copy = Operation::Copy {
path: path.clone(),
file: file()?,
};
let delete = Operation::Delete { path: path.clone() };
for op in [&write, ©, &delete] {
assert_eq!(op.path(), Some(&path));
}
let skip = Operation::Skip {
item: agent("core", "coder")?,
reason: SkipReason::new("no")?,
};
assert_eq!(skip.path(), None);
assert_eq!(Operation::require_binary("git")?.path(), None);
Ok(())
}
}