Skip to main content

blame_rs/
types.rs

1use std::rc::Rc;
2
3#[derive(Debug, Clone)]
4pub struct BlameRevision<'a, T> {
5    pub content: &'a str,
6    pub metadata: Rc<T>,
7}
8
9#[derive(Debug, Clone)]
10pub struct BlameLine<'a, T> {
11    pub line_number: usize,
12    pub content: &'a str,
13    pub revision_metadata: Rc<T>,
14}
15
16/// The result of a blame operation, containing all lines with their origin information
17#[derive(Debug, Clone)]
18pub struct BlameResult<'a, T> {
19    lines: Vec<BlameLine<'a, T>>,
20}
21
22impl<'a, T> BlameResult<'a, T> {
23    pub fn new(lines: Vec<BlameLine<'a, T>>) -> Self {
24        Self { lines }
25    }
26
27    pub fn lines(&self) -> &[BlameLine<'a, T>] {
28        &self.lines
29    }
30
31    pub fn get_line(&self, index: usize) -> Option<&BlameLine<'a, T>> {
32        self.lines.get(index)
33    }
34
35    pub fn len(&self) -> usize {
36        self.lines.len()
37    }
38
39    pub fn is_empty(&self) -> bool {
40        self.lines.is_empty()
41    }
42
43    pub fn iter(&self) -> impl Iterator<Item = &BlameLine<'a, T>> {
44        self.lines.iter()
45    }
46}
47
48impl<'a, T> IntoIterator for BlameResult<'a, T> {
49    type Item = BlameLine<'a, T>;
50    type IntoIter = std::vec::IntoIter<BlameLine<'a, T>>;
51
52    fn into_iter(self) -> Self::IntoIter {
53        self.lines.into_iter()
54    }
55}
56
57#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
58pub enum DiffAlgorithm {
59    /// Myers diff algorithm (default)
60    #[default]
61    Myers,
62    /// Patience diff algorithm (better for code reorganization)
63    Patience,
64}
65
66/// Options
67#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
68pub struct BlameOptions {
69    /// Diff algorithm to use
70    pub algorithm: DiffAlgorithm,
71}
72
73/// Errors
74#[derive(Debug, thiserror::Error)]
75pub enum BlameError {
76    /// No revisions were provided
77    #[error("no revisions provided")]
78    EmptyRevisions,
79
80    /// Invalid input data
81    #[error("invalid input: {0}")]
82    InvalidInput(String),
83}