use crate::{
ObjectId, ObjectKind, ObjectReadError, ReadLimits, TreeCompareError, TreeCompareLimits,
};
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum Copies {
#[default]
Disabled,
Modified,
ModifiedPostimage,
}
#[derive(Clone, Copy, Debug)]
pub struct Options {
pub similarity: u8,
pub copies: Copies,
pub track_empty: bool,
pub candidate_limit: usize,
pub approximate_binary: bool,
}
impl Default for Options {
fn default() -> Self {
Self {
similarity: 50,
copies: Copies::Disabled,
track_empty: false,
candidate_limit: 1000,
approximate_binary: true,
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct Limits {
pub trees: TreeCompareLimits,
pub read: ReadLimits,
pub max_blob_bytes: usize,
pub max_spans: usize,
pub max_pairs: usize,
pub max_work: usize,
}
impl Default for Limits {
fn default() -> Self {
Self {
trees: TreeCompareLimits::default(),
read: ReadLimits::default(),
max_blob_bytes: 64 * 1024 * 1024,
max_spans: 1_000_000,
max_pairs: 1_000_000,
max_work: 100_000_000,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Kind {
Rename,
Copy,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Rewrite {
pub source: Vec<u8>,
pub target: Vec<u8>,
pub source_id: ObjectId,
pub target_id: ObjectId,
pub similarity: u8,
pub kind: Kind,
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("rewrite similarity must be in 1..=100, got {0}")]
Similarity(u8),
#[error("comparing rewrite trees")]
Trees(#[from] TreeCompareError),
#[error("reading rewrite blob {id}")]
Read {
id: ObjectId,
#[source]
source: ObjectReadError,
},
#[error("missing rewrite blob {0}")]
Missing(ObjectId),
#[error("rewrite object {id} is {actual:?}, not a blob")]
NotBlob {
id: ObjectId,
actual: ObjectKind,
},
#[error("rewrite candidates ({sources} sources, {targets} targets) exceed limit {limit}")]
Candidates {
sources: usize,
targets: usize,
limit: usize,
},
#[error("rewrite resource limit exhausted: {0}")]
Limit(&'static str),
#[error("rewrite detection cancelled")]
Cancelled,
}