pub struct Parser<'a> { /* private fields */ }Implementations§
Source§impl Parser<'_>
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.
Sourcepub fn parse_item(&mut self) -> Result<Option<Value>, Error>
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).
Sourcepub fn parse_param(&mut self) -> Result<Option<(&'_ str, Value)>, Error>
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}"),
}
}
}
}
}Sourcepub fn parse_dict(&mut self) -> Result<Option<(&'_ str, Value)>, Error>
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.
Sourcepub fn parse_list(&mut self) -> Result<Option<Value>, Error>
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}"),
}
}
}
}
}Sourcepub fn parse_inner_list(&mut self) -> Result<Option<Value>, Error>
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}"),
}
}