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)]
58pub enum DiffAlgorithm {
59    /// Myers diff algorithm (default)
60    Myers,
61    /// Patience diff algorithm (better for code reorganization)
62    Patience,
63}
64
65impl Default for DiffAlgorithm {
66    fn default() -> Self {
67        Self::Myers
68    }
69}
70
71/// Options
72#[derive(Debug, Clone, Copy, PartialEq, Eq)]
73pub struct BlameOptions {
74    /// Diff algorithm to use
75    pub algorithm: DiffAlgorithm,
76}
77
78impl Default for BlameOptions {
79    fn default() -> Self {
80        Self {
81            algorithm: DiffAlgorithm::default(),
82        }
83    }
84}
85
86/// Errors
87#[derive(Debug, thiserror::Error)]
88pub enum BlameError {
89    /// No revisions were provided
90    #[error("no revisions provided")]
91    EmptyRevisions,
92
93    /// Invalid input data
94    #[error("invalid input: {0}")]
95    InvalidInput(String),
96}