#![deny(missing_docs)]
pub mod ed;
pub mod edit;
pub mod quilt;
pub mod timestamp;
pub mod unified;
pub fn strip_prefix(path: &std::path::Path, prefix: usize) -> &std::path::Path {
let mut components = path.components();
for _ in 0..prefix {
components.next();
}
std::path::Path::new(components.as_path())
}
#[derive(Debug)]
pub enum ApplyError {
Conflict(String),
Unapplyable,
}
impl std::fmt::Display for ApplyError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::Conflict(reason) => write!(f, "Conflict: {}", reason),
Self::Unapplyable => write!(f, "Patch unapplyable"),
}
}
}
impl std::error::Error for ApplyError {}
pub trait SingleFilePatch: ContentPatch {
fn oldname(&self) -> &[u8];
fn newname(&self) -> &[u8];
}
pub trait ContentPatch {
fn apply_exact(&self, orig: &[u8]) -> Result<Vec<u8>, ApplyError>;
}
#[test]
fn test_strip_prefix() {
assert_eq!(
std::path::PathBuf::from("b"),
strip_prefix(std::path::Path::new("a/b"), 1)
);
assert_eq!(
std::path::PathBuf::from("a/b"),
strip_prefix(std::path::Path::new("a/b"), 0)
);
assert_eq!(
std::path::PathBuf::from(""),
strip_prefix(std::path::Path::new("a/b"), 2)
);
}