sfparse

Struct Parser

Source
pub struct Parser<'a> { /* private fields */ }

Implementations§

Source§

impl Parser<'_>

Parser is RFC 9651 Structured Field Values parser. It is designed not to do any extra allocation, like allocating maps, lists, and Strings, and do the minimal stuff to parse the input data.

Source

pub fn new(data: &[u8]) -> Parser<'_>

Creates new Parser with the given data.

Source

pub fn parse_item(&mut self) -> Result<Option<Value>, Error>

Returns the next item. If there is no parameter left, this function returns Ok(None).

use sfparse::Parser;

let mut p = Parser::new("1235".as_bytes());

match p.parse_item().unwrap() {
    None => (),
    Some(v) => println!("{v}"),
}

To get the parameters attached to the item, call Parser::parse_param().

use sfparse::Parser;

let mut p = Parser::new("foo;f;g".as_bytes());

match p.parse_item().unwrap() {
    None => (),
    Some(v) => {
        println!("{v}");

        loop {
            match p.parse_param().unwrap() {
                None => break,
                Some((k, v)) => println!("{k}: {v}"),
            }
        }
    }
}

To ensure that the data given to Parser just contains a single item, call this method again and verify that it returns Ok(None).

Source

pub fn parse_param(&mut self) -> Result<Option<(&'_ str, Value)>, Error>

Returns the next key and item in a parameter list. If there is no parameter left, this function returns Ok(None).

Application should keep calling this method until it returns either Ok(None) or Err(Error).

use sfparse::Parser;

let mut p = Parser::new("a,b;q=0.5".as_bytes());

loop {
    match p.parse_list().unwrap() {
         None => break,
         Some(v) => {
             println!("{v}");

             loop {
                 match p.parse_param().unwrap() {
                     None => break,
                     Some((k, v)) => println!("{k}: {v}"),
                 }
             }
         }
    }
}
Source

pub fn parse_dict(&mut self) -> Result<Option<(&'_ str, Value)>, Error>

Parses the data as dictionary and returns the next key and item. If there is no pair left, this function returns Ok(None).

Application should keep calling this method until it returns either Ok(None) or Err(Error).

use sfparse::Parser;

let mut p = Parser::new(r#"en="Applepie", da=:w4ZibGV0w6ZydGU=:"#.as_bytes());

loop {
    match p.parse_dict().unwrap() {
         None => break,
         Some((k, v)) => println!("{k}: {v}"),
    }
}

If Value::InnerList is returned, and a caller is interested in its members, call Parser::parse_inner_list(). Otherwise, calling Parser::parse_dict returns the next key/item pair.

use sfparse::{Parser, Value};

let mut p = Parser::new("a=(1 2 3)".as_bytes());

loop {
    match p.parse_dict().unwrap() {
         None => break,
         Some((k, Value::InnerList)) => {
             println!("InnerList");

             loop {
                 match p.parse_inner_list().unwrap() {
                     None => break,
                     Some(v) => println!("{v}"),
                 }
             }
         }
         Some((k, v)) => println!("{k}: {v}"),
    }
}

To get the parameters attached to the key/item pair, call Parser::parse_param().

use sfparse::Parser;

let mut p = Parser::new("a,b;q=0.5".as_bytes());

loop {
    match p.parse_dict().unwrap() {
         None => break,
         Some(("b", _)) => {
             loop {
                 match p.parse_param().unwrap() {
                     None => break,
                     Some((k, v)) => println!("{k}: {v}"),
                 }
             }
         }
         _ => (),
    }
}

This method does no effect to verify the uniqueness of the key.

Source

pub fn parse_list(&mut self) -> Result<Option<Value>, Error>

Parses the data as list and returns the next item. If there is no item left, this function returns Ok(None).

Application should keep calling this method until it returns either Ok(None) or Err(Error).

use sfparse::Parser;

let mut p = Parser::new(r#"abc;a=1;b=2; cde_456, (ghi;jk=4 l);q="9";r=w"#.as_bytes());

loop {
    match p.parse_list().unwrap() {
         None => break,
         Some(v) => println!("{v}"),
    }
}

If Value::InnerList is returned, and a caller is interested in its members, call Parser::parse_inner_list(). Otherwise, calling Parser::parse_list returns the next item.

use sfparse::{Parser, Value};

let mut p = Parser::new("(1 2 3);f, foo".as_bytes());

loop {
    match p.parse_list().unwrap() {
         None => break,
         Some(Value::InnerList) => {
             println!("InnerList");

             loop {
                 match p.parse_inner_list().unwrap() {
                     None => break,
                     Some(v) => println!("{v}"),
                 }
             }
         }
         Some(v) => println!("{v}"),
    }
}

To get the parameters attached to the item, call Parser::parse_param().

use sfparse::Parser;

let mut p = Parser::new("a,b;q=0.5".as_bytes());

loop {
    match p.parse_list().unwrap() {
         None => break,
         Some(v) => {
             println!("{v}");

             loop {
                 match p.parse_param().unwrap() {
                     None => break,
                     Some((k, v)) => println!("{k}: {v}"),
                 }
             }
         }
    }
}
Source

pub fn parse_inner_list(&mut self) -> Result<Option<Value>, Error>

Returns the next item in an inner list. If there is no item left, this function returns Ok(None).

Application should keep calling this method until it returns either Ok(None) or Err(Error).

use sfparse::{Parser, Value};

let mut p = Parser::new("(1 2 3);f, foo".as_bytes());

loop {
    match p.parse_list().unwrap() {
         None => break,
         Some(Value::InnerList) => {
             println!("InnerList");

             loop {
                 match p.parse_inner_list().unwrap() {
                     None => break,
                     Some(v) => println!("{v}"),
                 }
             }
         }
         Some(v) => println!("{v}"),
    }
}

To get the parameters attached to the item, call Parser::parse_param().

use sfparse::{Parser, Value};

let mut p = Parser::new("(1 2 3);f, foo".as_bytes());

loop {
    match p.parse_list().unwrap() {
         None => break,
         Some(Value::InnerList) => {
             println!("InnerList");

             loop {
                 match p.parse_inner_list().unwrap() {
                     None => break,
                     Some(v) => {
                         println!("{v}");

                         loop {
                             match p.parse_param().unwrap() {
                                 None => break,
                                 Some((k, v)) => println!("{k}: {v}"),
                             }
                         }
                     }
                 }
             }
         }
         Some(v) => println!("{v}"),
    }
}

Auto Trait Implementations§

§

impl<'a> Freeze for Parser<'a>

§

impl<'a> RefUnwindSafe for Parser<'a>

§

impl<'a> Send for Parser<'a>

§

impl<'a> Sync for Parser<'a>

§

impl<'a> Unpin for Parser<'a>

§

impl<'a> UnwindSafe for Parser<'a>

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.