Expand description
sfv
is an implementation of Structured Field Values for HTTP, as specified in RFC 9651 for parsing and serializing HTTP field values.
It also exposes a set of types that might be useful for defining new structured fields.
§Data Structures
There are three types of structured fields:
Item
– anInteger
,Decimal
,String
,Token
,Byte Sequence
,Boolean
,Date
, orDisplay String
. It can have associatedParameters
.List
– an array of zero or more members, each of which can be anItem
or anInnerList
, both of which can haveParameters
.Dictionary
– an ordered map of name-value pairs, where the names are short textual strings and the values areItem
s or arrays ofItems
(represented withInnerList
), both of which can have associated parameters. There can be zero or more members, and their names are unique in the scope of theDictionary
they occur within.
There are also a few lower-level types used to construct structured field values:
BareItem
is used asItem
’s value or as a parameter value inParameters
.Parameters
are an ordered map of key-value pairs that are associated with anItem
orInnerList
. The keys are unique within the scope theParameters
they occur within, and the values areBareItem
.InnerList
is an array of zero or moreItems
. Can have associatedParameters
.ListEntry
represents eitherItem
orInnerList
as a member ofList
or as member-value inDictionary
.
§Examples
§Parsing
use sfv::Parser;
// Parsing a structured field value of Item type.
let input = "12.445;foo=bar";
let item = Parser::new(input).parse_item()?;
println!("{:#?}", item);
// Parsing a structured field value of List type.
let input = r#"1;a=tok, ("foo" "bar");baz, ()"#;
let list = Parser::new(input).parse_list()?;
println!("{:#?}", list);
// Parsing a structured field value of Dictionary type.
let input = "a=?0, b, c; foo=bar, rating=1.5, fruits=(apple pear)";
let dict = Parser::new(input).parse_dictionary()?;
println!("{:#?}", dict);
§Getting Parsed Value Members
use sfv::*;
let input = "u=2, n=(* foo 2)";
let dict = Parser::new(input).parse_dictionary()?;
match dict.get("u") {
Some(ListEntry::Item(item)) => match &item.bare_item {
BareItem::Token(val) => { /* ... */ }
BareItem::Integer(val) => { /* ... */ }
BareItem::Boolean(val) => { /* ... */ }
BareItem::Decimal(val) => { /* ... */ }
BareItem::String(val) => { /* ... */ }
BareItem::ByteSequence(val) => { /* ... */ }
BareItem::Date(val) => { /* ... */ }
BareItem::DisplayString(val) => { /* ... */ }
},
Some(ListEntry::InnerList(inner_list)) => { /* ... */ }
None => { /* ... */ }
}
§Serialization
Serializes an Item
:
use sfv::{Decimal, ItemSerializer, KeyRef, StringRef};
let serialized_item = ItemSerializer::new()
.bare_item(StringRef::from_str("foo")?)
.parameter(KeyRef::from_str("key")?, Decimal::try_from(13.45655)?)
.finish();
assert_eq!(serialized_item, r#""foo";key=13.457"#);
Serializes a List
:
use sfv::{KeyRef, ListSerializer, StringRef, TokenRef};
let mut ser = ListSerializer::new();
ser.bare_item(TokenRef::from_str("tok")?);
{
let mut ser = ser.inner_list();
ser.bare_item(99).parameter(KeyRef::from_str("key")?, false);
ser.bare_item(StringRef::from_str("foo")?);
ser.finish().parameter(KeyRef::from_str("bar")?, true);
}
let serialized_list = ser.finish()?;
assert_eq!(
serialized_list,
r#"tok, (99;key=?0 "foo");bar"#
);
Serializes a Dictionary
:
use sfv::{DictSerializer, KeyRef, StringRef};
let mut ser = DictSerializer::new();
ser.bare_item(KeyRef::from_str("key1")?, StringRef::from_str("apple")?);
ser.bare_item(KeyRef::from_str("key2")?, true);
ser.bare_item(KeyRef::from_str("key3")?, false);
let serialized_dict = ser.finish()?;
assert_eq!(
serialized_dict,
r#"key1="apple", key2, key3=?0"#
);
§Crate features
-
parsed-types
(enabled by default) – When enabled, exposes fully owned typesItem
,Dictionary
,List
, and their components, which can be obtained fromParser::parse_item
, etc. These types are implemented using theindexmap
crate, so disabling this feature can avoid that dependency if parsing using a visitor (Parser::parse_item_with_visitor
, etc.) is sufficient. -
arbitrary
– Implements theArbitrary
trait for this crate’s types, making them easier to use with fuzzing.
Modules§
- visitor
- Contains traits for parsing structured-field values incrementally.
Structs§
- Date
- A structured field value date.
- Decimal
- A structured field value decimal.
- Dict
Serializer - Serializes
Dictionary
field value components incrementally. - Error
- An error that occurs during parsing or serialization.
- Inner
List - An array of
Item
s with associatedParameters
. - Inner
List Serializer - Serializes inner lists incrementally.
- Integer
- A structured field value integer.
- Item
- An item-type structured field value.
- Item
Serializer - Serializes
Item
field value components incrementally. - Key
- An owned structured field value key.
- KeyRef
- A borrowed structured field value key.
- List
Serializer - Serializes
List
field value components incrementally. - Parameter
Serializer - Serializes parameters incrementally.
- Parser
- Exposes methods for parsing input into a structured field value.
- String
- An owned structured field value string.
- String
Ref - A borrowed structured field value string.
- Token
- An owned structured field value token.
- Token
Ref - A borrowed structured field value token.
Enums§
- Generic
Bare Item - An abstraction over multiple kinds of ownership of a bare item.
- List
Entry - A member of a
List
orDictionary
. - Version
- A version for serialized structured field values.
Traits§
- Serialize
Value - Serializes a structured field value into a string.
Functions§
- integer
- Creates an
Integer
, panicking if the value is out of range. - key_ref
- Creates a
&KeyRef
, panicking if the value is invalid. - string_
ref - Creates a
&StringRef
, panicking if the value is invalid. - token_
ref - Creates a
&TokenRef
, panicking if the value is invalid.
Type Aliases§
- Bare
Item - A bare item that owns its data.
- Bare
Item From Input - A bare item that borrows data from input when possible.
- Dictionary
- A dictionary-type structured field value.
- List
- A list-type structured field value.
- Parameters
- Parameters of an
Item
orInnerList
. - RefBare
Item - A bare item that borrows its data.