Skip to main content

dependency_check_updates_core/
style.rs

1//! File formatting detection (indentation, line endings, trailing newline)
2//! used to preserve a manifest's original style when rewriting it.
3
4/// Detected indentation style.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum IndentStyle {
7    /// Indentation by the given number of spaces per level.
8    Spaces(u8),
9    /// Indentation by a single tab character per level.
10    Tab,
11}
12
13impl Default for IndentStyle {
14    fn default() -> Self {
15        Self::Spaces(2)
16    }
17}
18
19/// Detected line ending style.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
21pub enum LineEnding {
22    /// Unix line endings (`\n`).
23    #[default]
24    Lf,
25    /// Windows line endings (`\r\n`).
26    CrLf,
27}
28
29/// The detected formatting style of a file.
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub struct FileStyle {
32    /// Detected indentation style.
33    pub indent: IndentStyle,
34    /// Detected line-ending style.
35    pub line_ending: LineEnding,
36    /// Whether the file ends with a trailing newline.
37    pub trailing_newline: bool,
38}
39
40impl Default for FileStyle {
41    fn default() -> Self {
42        Self {
43            indent: IndentStyle::default(),
44            line_ending: LineEnding::default(),
45            trailing_newline: true,
46        }
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_file_style_default() {
56        let style = FileStyle::default();
57        assert_eq!(style.indent, IndentStyle::Spaces(2));
58        assert_eq!(style.line_ending, LineEnding::Lf);
59        assert!(style.trailing_newline);
60    }
61
62    #[test]
63    fn test_indent_style_default() {
64        let indent = IndentStyle::default();
65        assert_eq!(indent, IndentStyle::Spaces(2));
66    }
67
68    #[test]
69    fn test_line_ending_default() {
70        let ending = LineEnding::default();
71        assert_eq!(ending, LineEnding::Lf);
72    }
73}