Skip to main content

cargo_quality/differ/display/
types.rs

1// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
2// SPDX-License-Identifier: MIT
3
4/// Pre-rendered file diff block with calculated dimensions.
5///
6/// Stores all output lines for a single file along with the maximum visual
7/// width required for display. Used by grid layout system to calculate optimal
8/// column arrangements.
9///
10/// # Memory Layout
11///
12/// Lines are stored with ANSI color codes embedded. Width represents the
13/// visual character count excluding escape sequences.
14///
15/// # Examples
16///
17/// ```
18/// use cargo_quality::differ::display::types::RenderedFile;
19///
20/// let rendered = RenderedFile {
21///     lines: vec![
22///         "File: test.rs".to_string(),
23///         "────────────".to_string(),
24///     ],
25///     width: 40,
26/// };
27///
28/// assert_eq!(rendered.line_count(), 2);
29/// assert_eq!(rendered.width, 40);
30/// ```
31#[derive(Debug, Clone)]
32pub struct RenderedFile {
33    /// Output lines with embedded ANSI color codes
34    pub lines: Vec<String>,
35    /// Maximum visual width in characters (excluding ANSI codes)
36    pub width: usize
37}
38
39impl RenderedFile {
40    /// Returns the number of lines in the rendered output.
41    ///
42    /// # Returns
43    ///
44    /// Line count
45    ///
46    /// # Examples
47    ///
48    /// ```
49    /// use cargo_quality::differ::display::types::RenderedFile;
50    ///
51    /// let mut rendered = RenderedFile {
52    ///     lines: vec!["line1".to_string(), "line2".to_string()],
53    ///     width: 40
54    /// };
55    ///
56    /// assert_eq!(rendered.line_count(), 2);
57    /// ```
58    #[inline]
59    pub const fn line_count(&self) -> usize {
60        self.lines.len()
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn test_line_count() {
70        let mut rendered = RenderedFile {
71            lines: Vec::new(),
72            width: 40
73        };
74        assert_eq!(rendered.line_count(), 0);
75
76        rendered.lines.push("line1".to_string());
77        assert_eq!(rendered.line_count(), 1);
78
79        rendered.lines.push("line2".to_string());
80        assert_eq!(rendered.line_count(), 2);
81    }
82
83    #[test]
84    fn test_clone() {
85        let original = RenderedFile {
86            lines: vec!["test".to_string()],
87            width: 50
88        };
89
90        let cloned = original.clone();
91        assert_eq!(cloned.line_count(), 1);
92        assert_eq!(cloned.width, 50);
93    }
94}