[][src]Struct mininip::parse::Parser

pub struct Parser { /* fields omitted */ }

A parser with a local state. Use it by passing it the text to parse line after line

Notes

This parser does not work with a file but with lines passed as below. It allows you to parse an INI data from an iterator, a channel, the network...

Examples

use mininip::parse::Parser;
use mininip::datas::{Identifier, Value};
 
let mut parser = Parser::new();
 
parser.parse_line("abc = 123").unwrap();
parser.parse_line("; comment. A comment may start at an end of line").unwrap();
parser.parse_line("").unwrap(); // empty line
parser.parse_line("[section]").unwrap();
parser.parse_line("def = '\\;) \\= \\x00263a' ; This is perfectly valid").unwrap();
 
let data = parser.data();
 
let section = None;
let name = String::from("abc");
let abc = Identifier::new(section, name);
 
let value = Value::Int(123);
assert_eq!(data[&abc], value);
 
let section = Some(String::from("section"));
let name = String::from("def");
let def = Identifier::new(section, name);
 
let value = Value::Str(String::from(";) = \u{263a}"));
assert_eq!(data[&def], value);

Methods

impl Parser[src]

pub fn new() -> Parser[src]

Creates a new Parser, which didn't parsed any line

pub fn data(self) -> HashMap<Identifier, Value>[src]

Consumes the parser and returns its data which is an HashMap<Identifier, Value> linking an identifier to its value

pub fn parse_line(&mut self, line: &str) -> Result<(), Error>[src]

Parses a line

Parameters

line the line to parse

Return value

Ok(()) in case of success

Err(error) in case of error with error as the error code (see Error)

Examples

use mininip::parse::Parser;
use mininip::errors::{Error, error_kinds};
use mininip::datas::{Identifier, Value};
 
let mut parser = Parser::new();
 
let good_line = "greeting = Hello \\x00263a";
parser.parse_line(good_line)
    .expect("This line is valid");
 
let bad_line = "how to greet? = Hello \\x00263a";
match parser.parse_line(bad_line) {
    Ok(())                             => panic!("This line is invalid and should not be accepted"),
    Err(Error::InvalidIdentifier(err)) => assert_eq!(format!("{}", err), "Invalid identifier how to greet? in how to greet? = Hello \\x00263a"),
    Err(err)                           => panic!("Wrong error returned (got {:?})", err),
}

Trait Implementations

impl Clone for Parser[src]

impl Debug for Parser[src]

Auto Trait Implementations

impl RefUnwindSafe for Parser

impl Send for Parser

impl Sync for Parser

impl Unpin for Parser

impl UnwindSafe for Parser

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.