string_auto_indent/
line_ending.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3#[allow(clippy::upper_case_acronyms)]
4pub enum LineEnding {
5 LF, CRLF, CR, }
9
10impl LineEnding {
11 pub fn detect(s: &str) -> Self {
13 if s.contains("\r\n") {
14 Self::CRLF
15 } else if s.contains("\r") {
16 Self::CR
17 } else {
18 Self::LF
19 }
20 }
21
22 pub fn as_str(&self) -> &'static str {
24 match self {
25 Self::LF => "\n",
26 Self::CRLF => "\r\n",
27 Self::CR => "\r",
28 }
29 }
30
31 pub fn normalize(s: &str) -> String {
33 s.replace("\r\n", "\n").replace("\r", "\n")
34 }
35
36 #[allow(dead_code)]
38 pub fn restore(&self, s: &str) -> String {
39 s.replace("\n", self.as_str())
40 }
41
42 pub fn restore_from_lines(&self, lines: Vec<String>) -> String {
44 lines.join(self.as_str())
45 }
46}