From c6f92eaedf197beafef461ee6c2bd067e7160c49 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Tue, 21 Jun 2022 16:06:50 +0200 Subject: Refactor to improve a bunch of states MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Improve passing stuff around * Add traits to enums for markers and such * Fix “life time” stuff I didn’t understand --- src/compiler.rs | 58 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 26 deletions(-) (limited to 'src/compiler.rs') diff --git a/src/compiler.rs b/src/compiler.rs index 5c7f6d8..9bc2488 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -9,13 +9,37 @@ use crate::util::{ }; /// To do. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub enum LineEnding { CarriageReturnLineFeed, CarriageReturn, LineFeed, } +impl LineEnding { + /// Turn the line ending into a [str]. + fn as_str(&self) -> &str { + match self { + LineEnding::CarriageReturnLineFeed => "\r\n", + LineEnding::CarriageReturn => "\r", + LineEnding::LineFeed => "\n", + } + } + /// Turn a [Code] into a line ending. + /// + /// ## Panics + /// + /// Panics if `code` is not `\r\n`, `\r`, or `\n`. + fn from_code(code: Code) -> LineEnding { + match code { + Code::CarriageReturnLineFeed => LineEnding::CarriageReturnLineFeed, + Code::Char('\r') => LineEnding::CarriageReturn, + Code::Char('\n') => LineEnding::LineFeed, + _ => unreachable!("invalid code"), + } + } +} + /// Configuration (optional). #[derive(Default, Debug)] pub struct Options { @@ -120,29 +144,20 @@ pub fn compile(events: &[Event], codes: &[Code], options: &Options) -> String { || event.token_type == TokenType::LineEnding) { let codes = codes_from_span(codes, &from_exit_event(events, index)); - let code = *codes.first().unwrap(); - line_ending_inferred = Some(if code == Code::CarriageReturnLineFeed { - LineEnding::CarriageReturnLineFeed - } else if code == Code::Char('\r') { - LineEnding::CarriageReturn - } else { - LineEnding::LineFeed - }); + line_ending_inferred = Some(LineEnding::from_code(*codes.first().unwrap())); break; } index += 1; } - let line_ending_default: LineEnding; - - if let Some(value) = line_ending_inferred { - line_ending_default = value; + let line_ending_default = if let Some(value) = line_ending_inferred { + value } else if let Some(value) = &options.default_line_ending { - line_ending_default = value.clone(); + value.clone() } else { - line_ending_default = LineEnding::LineFeed; - } + LineEnding::LineFeed + }; index = 0; @@ -557,17 +572,8 @@ fn buf_tail(buffers: &mut [Vec]) -> &Vec { /// Add a line ending. fn line_ending(buffers: &mut [Vec], default: &LineEnding) { let tail = buf_tail_mut(buffers); - - println!("xxx: {:?}", default); - - let line_ending = match default { - LineEnding::CarriageReturnLineFeed => "\r\n", - LineEnding::CarriageReturn => "\r", - LineEnding::LineFeed => "\n", - }; - // lastWasTag = false - tail.push(line_ending.to_string()); + tail.push(default.as_str().to_string()); } /// Add a line ending if needed (as in, there’s no eol/eof already). -- cgit