Skip to content

Commit

Permalink
move line_ending to chars helper mod
Browse files Browse the repository at this point in the history
  • Loading branch information
jsdw committed Nov 14, 2023
1 parent 0a28590 commit 4379261
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
27 changes: 27 additions & 0 deletions src/chars.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! Helper functions for working with `Tokens<Item=char>`.
use crate::Tokens;

/// Parses a line ending of either `\n` (like on linux) or `\r\n` (like on windows).
/// Returns a static string equal to the line ending parsed, or `None` if no line
/// ending is seen at this location.
///
/// # Example
///
/// ```
/// use yap::{Tokens, IntoTokens, chars};
///
/// let mut toks = "\r\n abc".into_tokens();
///
/// assert_eq!(chars::line_ending(&mut toks), Some("\r\n"));
/// assert_eq!(toks.remaining(), " abc");
///
/// let mut toks = "\n abc".into_tokens();
///
/// assert_eq!(chars::line_ending(&mut toks), Some("\n"));
/// assert_eq!(toks.remaining(), " abc");
/// ```
pub fn line_ending<T: Tokens<Item = char>>(t: &mut T) -> Option<&'static str> {
t.optional(|t| t.token('\n').then_some("\n"))
.or_else(|| t.optional(|t| t.tokens("\r\n".chars()).then_some("\r\n")))
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,6 @@ assert_eq!(remaining, ",foobar");
mod one_of;
mod tokens;

pub mod chars;
pub mod types;
pub use tokens::{IntoTokens, TokenLocation, Tokens};
11 changes: 0 additions & 11 deletions src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,17 +1013,6 @@ pub trait Tokens: Sized {
}
}

/// Parses a line ending of either `\n` (like on linux) or `\r\n` (like on windows).
/// Returns a static string equal to the line ending parsed, or `None` if no line
/// ending is seen at this location.
fn line_ending(&mut self) -> Option<&'static str>
where
Self: Tokens<Item = char>,
{
self.optional(|t| t.token('\n').then_some("\n"))
.or_else(|| self.optional(|t| t.tokens("\r\n".chars()).then_some("\r\n")))
}

/// Checks next input is [`None`] and, if true, consumes the `None`.
///
/// # Example
Expand Down

0 comments on commit 4379261

Please sign in to comment.