/rust/registry/src/index.crates.io-1949cf8c6b5b557f/base64ct-1.8.0/src/line_ending.rs
Line | Count | Source |
1 | | //! Line endings. |
2 | | |
3 | | /// Carriage return |
4 | | pub(crate) const CHAR_CR: u8 = 0x0d; |
5 | | |
6 | | /// Line feed |
7 | | pub(crate) const CHAR_LF: u8 = 0x0a; |
8 | | |
9 | | /// Line endings: variants of newline characters that can be used with Base64. |
10 | | /// |
11 | | /// Use [`LineEnding::default`] to get an appropriate line ending for the |
12 | | /// current operating system. |
13 | | #[allow(clippy::upper_case_acronyms)] |
14 | | #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
15 | | pub enum LineEnding { |
16 | | /// Carriage return: `\r` (Pre-OS X Macintosh) |
17 | | CR, |
18 | | |
19 | | /// Line feed: `\n` (Unix OSes) |
20 | | LF, |
21 | | |
22 | | /// Carriage return + line feed: `\r\n` (Windows) |
23 | | CRLF, |
24 | | } |
25 | | |
26 | | impl Default for LineEnding { |
27 | | // Default line ending matches conventions for target OS |
28 | | #[cfg(windows)] |
29 | | fn default() -> LineEnding { |
30 | | LineEnding::CRLF |
31 | | } |
32 | | #[cfg(not(windows))] |
33 | 0 | fn default() -> LineEnding { |
34 | 0 | LineEnding::LF |
35 | 0 | } |
36 | | } |
37 | | |
38 | | #[allow(clippy::len_without_is_empty)] |
39 | | impl LineEnding { |
40 | | /// Get the byte serialization of this [`LineEnding`]. |
41 | 0 | pub fn as_bytes(self) -> &'static [u8] { |
42 | 0 | match self { |
43 | 0 | LineEnding::CR => &[CHAR_CR], |
44 | 0 | LineEnding::LF => &[CHAR_LF], |
45 | 0 | LineEnding::CRLF => &[CHAR_CR, CHAR_LF], |
46 | | } |
47 | 0 | } |
48 | | |
49 | | /// Get the encoded length of this [`LineEnding`]. |
50 | 0 | pub fn len(self) -> usize { |
51 | 0 | self.as_bytes().len() |
52 | 0 | } |
53 | | } |