normalized_line_endings/
line_ending.rs

1//! # Line endings
2
3use crate::common::{CR, LF};
4
5/// Line endings.
6#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
7pub enum LineEnding {
8  /// Line ending is a line feed character (U+000A).
9  Lf,
10  /// Line ending is a carriage return character (U+000D).
11  Cr,
12  /// Line ending is a sequence of carriage return and line feed characters (U+000D U+000A).
13  CrLf,
14}
15
16impl AsRef<str> for LineEnding {
17  fn as_ref(&self) -> &str {
18    match self {
19      LineEnding::Lf => "\n",
20      LineEnding::Cr => "\r",
21      LineEnding::CrLf => "\r\n",
22    }
23  }
24}
25
26impl LineEnding {
27  /// Returns the first character of the line ending.
28  ///
29  /// # Examples
30  ///
31  /// ```
32  /// use normalized_line_endings::LineEnding;
33  ///
34  /// assert_eq!('\n', LineEnding::Lf.first());
35  /// assert_eq!('\r', LineEnding::Cr.first());
36  /// assert_eq!('\r', LineEnding::CrLf.first());
37  /// ```
38  pub fn first(&self) -> char {
39    match self {
40      LineEnding::Lf => LF,
41      LineEnding::Cr => CR,
42      LineEnding::CrLf => CR,
43    }
44  }
45
46  /// Returns the last character of the line ending.
47  ///
48  /// # Examples
49  ///
50  /// ```
51  /// use normalized_line_endings::LineEnding;
52  ///
53  /// assert_eq!('\n', LineEnding::Lf.last());
54  /// assert_eq!('\r', LineEnding::Cr.last());
55  /// assert_eq!('\n', LineEnding::CrLf.last());
56  /// ```
57  pub fn last(&self) -> char {
58    match self {
59      LineEnding::Lf => LF,
60      LineEnding::Cr => CR,
61      LineEnding::CrLf => LF,
62    }
63  }
64}