Skip to main content

Parser

Struct Parser 

Source
pub struct Parser { /* private fields */ }
Expand description

The configurable parser.

§Examples

use parse_net::{sfv::Parser, ErrorKind, Limits};

let strict = Parser::new(Limits::DEFAULT.max_list_members(2));
assert_eq!(strict.list(b"a, b, c").unwrap_err().kind(), ErrorKind::TooManyMembers);
assert_eq!(Parser::default().list(b"a, b, c")?.len(), 3);

Implementations§

Source§

impl Parser

Source

pub const fn new(limits: Limits) -> Self

A parser with limits.

§Examples
use parse_net::{sfv::Parser, Limits};

let p = Parser::new(Limits::DEFAULT.max_dictionary_members(1));
assert!(p.dictionary(b"a").is_ok());
assert!(p.dictionary(b"a, b").is_err());
Source

pub fn item<'a>(&self, v: &'a [u8]) -> Result<Item<'a>, ParseError>

Parses an Item (section 4.2.3).

§Errors

See parse_item.

§Examples
use parse_net::sfv::{BareItem, Parser};

let item = Parser::default().item(b"?0;x")?;
assert_eq!(item.bare, BareItem::Boolean(false));
assert_eq!(item.params.len(), 1);
Source

pub fn list<'a>(&self, v: &'a [u8]) -> Result<List<'a>, ParseError>

Parses a List (section 4.2.1).

§Errors

See parse_list.

§Examples
use parse_net::sfv::Parser;

assert_eq!(Parser::default().list(b"a, (b c);q=1")?.len(), 2);
assert!(Parser::default().list(b"a,,b").is_err());
Source

pub fn dictionary<'a>(&self, v: &'a [u8]) -> Result<Dictionary<'a>, ParseError>

Parses a Dictionary (section 4.2.2).

§Errors

See parse_dictionary.

§Examples
use parse_net::sfv::{BareItem, Member, Parser};

let d = Parser::default().dictionary(b"a=1, b")?;
assert!(matches!(d.get("a"), Some(Member::Item(i)) if i.bare == BareItem::Integer(1)));
Source

pub fn list_lines<'a>( &self, lines: &'a [&'a [u8]], ) -> Result<ListLines<'a>, ParseError>

Parses several field lines as one List: each line must parse on its own, an empty line fails the field, and the member limit counts across lines.

§Errors

As list, with ParseError::line set; an empty line is ErrorKind::Empty.

§Examples
use parse_net::{sfv::Parser, ErrorKind};

let good: [&[u8]; 2] = [b"a", b"b, c"];
assert_eq!(Parser::default().list_lines(&good)?.len(), 3);
let bad: [&[u8]; 2] = [b"a", b""];
let e = Parser::default().list_lines(&bad).unwrap_err();
assert_eq!((e.kind(), e.line()), (ErrorKind::Empty, 1));
Source

pub fn dictionary_lines<'a>( &self, lines: &'a [&'a [u8]], ) -> Result<DictionaryLines<'a>, ParseError>

Parses several field lines as one Dictionary (the last value of a repeated key wins across lines).

§Errors

As list_lines.

§Examples
use parse_net::sfv::{BareItem, Member, Parser};

let lines: [&[u8]; 2] = [b"a=1", b"a=2"];
let d = Parser::default().dictionary_lines(&lines)?;
assert!(matches!(d.get("a"), Some(Member::Item(i)) if i.bare == BareItem::Integer(2)));
Source

pub fn visit_item<'a, V: Visitor<'a>>( &self, v: &'a [u8], visitor: &mut V, ) -> Result<(), ParseError>

Parses an Item, reporting it to visitor.

§Errors

As item, or the visitor’s error.

§Examples
use parse_net::{sfv::{BareItem, Parser, Visitor}, Limits, ParseError};

struct Count(usize);
impl<'a> Visitor<'a> for Count {
    fn item(&mut self, _: BareItem<'a>) -> Result<(), ParseError> { Ok(()) }
    fn param(&mut self, _: &'a str, _: BareItem<'a>) -> Result<(), ParseError> { self.0 += 1; Ok(()) }
}

let mut c = Count(0);
Parser::new(Limits::DEFAULT).visit_item(b"a;x=1;y", &mut c)?;
assert_eq!(c.0, 2);
Source

pub fn visit_list<'a, V: Visitor<'a>>( &self, v: &'a [u8], visitor: &mut V, ) -> Result<(), ParseError>

Parses a List, reporting it to visitor.

§Errors

As list, or the visitor’s error.

§Examples
use parse_net::{sfv::{BareItem, Parser, Visitor}, Limits, ParseError};

struct Count(usize);
impl<'a> Visitor<'a> for Count {
    fn item(&mut self, _: BareItem<'a>) -> Result<(), ParseError> { self.0 += 1; Ok(()) }
    fn param(&mut self, _: &'a str, _: BareItem<'a>) -> Result<(), ParseError> { Ok(()) }
}

let mut c = Count(0);
Parser::new(Limits::DEFAULT).visit_list(b"a, (b c);q", &mut c)?;
assert_eq!(c.0, 3);
Source

pub fn visit_dictionary<'a, V: Visitor<'a>>( &self, v: &'a [u8], visitor: &mut V, ) -> Result<(), ParseError>

Parses a Dictionary, reporting it to visitor.

§Errors

As dictionary, or the visitor’s error.

§Examples
use parse_net::{sfv::{BareItem, Parser, Visitor}, Limits, ParseError};

struct Count(usize);
impl<'a> Visitor<'a> for Count {
    fn item(&mut self, _: BareItem<'a>) -> Result<(), ParseError> { Ok(()) }
    fn param(&mut self, _: &'a str, _: BareItem<'a>) -> Result<(), ParseError> { Ok(()) }
    fn key(&mut self, _: &'a str) -> Result<(), ParseError> { self.0 += 1; Ok(()) }
}

let mut c = Count(0);
Parser::new(Limits::DEFAULT).visit_dictionary(b"a=1, b, c=(x y)", &mut c)?;
assert_eq!(c.0, 3);

Trait Implementations§

Source§

impl Clone for Parser

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Parser

Source§

impl Debug for Parser

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Parser

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for Parser

Source§

impl Hash for Parser

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Parser

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Parser

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.