This crate provides utilities to handle quoted strings like such appearing
in Mediat Types (both MIME (i.e. Mail) and HTTP). As there are many small but significant
differences in different specifications this crate does not provide
a specific implementation. Instead a QuotedStringSpec
trait is
exposed. Implementing it (on zero-sized structs) should allow the
usage with any quoted-string specification.
quote_if_needed
("e
): quotes content (if needed), the UnquotedValidator
part
of QuotedStringSpec
can be used to specify which values are valid without needing
to be representated as a quoted string. E.g. in a Media Type a parameter value of abc
can
and should be directly represented on benefit of quoted_if_needed
is that it returns a
Cow
so the string is only copied if it actually needs to be represented as quoted string.
to_content
: retrieve the content of a quoted string which means that the
surrounding '"'
quotes will be removed and any quoted-pair (e.g. "\\\""
/r#"\""#
) will be
replaced with it's value. This function returns Cow::Borrowed
if there are no quoted-pairs
preventing needless allocations.
ContentChars
: an iterator over the chars of the content of a quoted-string,
i.e. it will strip the surrounding DQUOTE
s and will ()on the fly) unquote
quoted-pais not needing any extra memory allocations. This can be used to
semantically compare two quoted strings regardless of how they used
quoted-pair
s, it implements Eq
.
parse
(&validate
): parses a quoted-string positioned at the start of the input.
It is written to be easily integrable with nom
(through does not require nom
in any way, using it standalone is as easy)
```rust extern crate quoted_string;
// we use a QuotedStringSpec provided for testing here, // not that it's made to hitt some edcases in a simple way // so it does not correspond to any used real Spec use quotedstring::testutils::TestSpec; type Spec = TestSpec;
use quotedstring::{parse, quote, quoteifneeded, tocontent};
fn main() {
let res = parse::
// TestSpec specifies us-ascii words with 6 letters need no quoting
let (out, _meta) = quote_if_needed::<Spec>("simple").unwrap();
assert_eq!(&*out, "simple");
}
```
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
QuotedStringSpec
as there are to many differences between quoted-string's
in Media Type occurring in HTTP and thus occurring in MIME (Mail)