add from_bytes variants for parser

This commit is contained in:
Edward Shen 2021-02-24 16:30:04 -05:00
parent df6937ade1
commit f99da79ad9
Signed by: edward
GPG key ID: 19182661E818369F

View file

@ -391,9 +391,9 @@ pub struct Parser<'a> {
impl<'a> Parser<'a> { impl<'a> Parser<'a> {
/// Attempt to zero-copy parse the provided `&str`. On success, returns a /// Attempt to zero-copy parse the provided `&str`. On success, returns a
/// [`Parser`] that provides methods to accessing leading comments and sections /// [`Parser`] that provides methods to accessing leading comments and
/// of a `git-config` file and can be converted into an iterator of [`Event`] /// sections of a `git-config` file and can be converted into an iterator of
/// for higher level processing. /// [`Event`] for higher level processing.
/// ///
/// This function is identical to [`parse_from_str`]. /// This function is identical to [`parse_from_str`].
/// ///
@ -406,6 +406,20 @@ impl<'a> Parser<'a> {
parse_from_str(s) parse_from_str(s)
} }
/// Attempt to zero-copy parse the provided bytes. On success, returns a
/// [`Parser`] that provides methods to accessing leading comments and
/// sections of a `git-config` file and can be converted into an iterator of
/// [`Event`] for higher level processing.
///
/// # Errors
///
/// Returns an error if the string provided is not a valid `git-config`.
/// This generally is due to either invalid names or if there's extraneous
/// data succeeding valid `git-config` data.
pub fn from_bytes(s: impl Into<&'a BStr>) -> Result<Self, ParserError<'a>> {
parse_from_bytes(s.into())
}
/// Returns the leading events (any comments, whitespace, or newlines before /// Returns the leading events (any comments, whitespace, or newlines before
/// a section) from the parser. Consider [`Parser::take_frontmatter`] if /// a section) from the parser. Consider [`Parser::take_frontmatter`] if
/// you need an owned copy only once. If that function was called, then this /// you need an owned copy only once. If that function was called, then this
@ -471,6 +485,20 @@ impl<'a> Parser<'a> {
/// This generally is due to either invalid names or if there's extraneous /// This generally is due to either invalid names or if there's extraneous
/// data succeeding valid `git-config` data. /// data succeeding valid `git-config` data.
pub fn parse_from_str(input: &str) -> Result<Parser<'_>, ParserError> { pub fn parse_from_str(input: &str) -> Result<Parser<'_>, ParserError> {
parse_from_bytes(input.as_bytes())
}
/// Attempt to zero-copy parse the provided bytes. On success, returns a
/// [`Parser`] that provides methods to accessing leading comments and sections
/// of a `git-config` file and can be converted into an iterator of [`Event`]
/// for higher level processing.
///
/// # Errors
///
/// Returns an error if the string provided is not a valid `git-config`.
/// This generally is due to either invalid names or if there's extraneous
/// data succeeding valid `git-config` data.
pub fn parse_from_bytes(input: &[u8]) -> Result<Parser<'_>, ParserError> {
let (i, frontmatter) = many0(alt(( let (i, frontmatter) = many0(alt((
map(comment, Event::Comment), map(comment, Event::Comment),
map(take_spaces, |whitespace| { map(take_spaces, |whitespace| {