3 use crate::format_description
::component
::{Component, NakedComponent}
;
4 use crate::format_description
::error
::InvalidFormatDescription
;
5 use crate::format_description
::{helper, modifier, FormatItem}
;
8 struct ParsedItem
<'a
> {
13 fn parse_component(mut s
: &[u8], index
: &mut usize) -> Result
<Component
, InvalidFormatDescription
> {
14 s
= helper
::consume_whitespace(s
, index
);
16 let component_index
= *index
;
17 let whitespace_loc
= s
19 .position(u8::is_ascii_whitespace
)
21 *index
+= whitespace_loc
;
22 let component_name
= &s
[..whitespace_loc
];
23 s
= &s
[whitespace_loc
..];
24 s
= helper
::consume_whitespace(s
, index
);
26 Ok(NakedComponent
::parse(component_name
, component_index
)?
27 .attach_modifiers(modifier
::Modifiers
::parse(component_name
, s
, index
)?
))
30 fn parse_literal
<'a
>(s
: &'a
[u8], index
: &mut usize) -> ParsedItem
<'a
> {
31 let loc
= s
.iter().position(|&c
| c
== b'
['
).unwrap_or(s
.len());
34 item
: FormatItem
::Literal(&s
[..loc
]),
42 ) -> Result
<ParsedItem
<'a
>, InvalidFormatDescription
> {
43 if let [b'
['
, b'
['
, remaining @
..] = s
{
45 return Ok(ParsedItem
{
46 item
: FormatItem
::Literal(b
"["),
51 if s
.starts_with(b
"[") {
52 if let Some(bracket_index
) = s
.iter().position(|&c
| c
== b'
]'
) {
53 *index
+= 1; // opening bracket
54 let ret_val
= ParsedItem
{
55 item
: FormatItem
::Component(parse_component(&s
[1..bracket_index
], index
)?
),
56 remaining
: &s
[bracket_index
+ 1..],
58 *index
+= 1; // closing bracket
61 Err(InvalidFormatDescription
::UnclosedOpeningBracket { index: *index }
)
64 Ok(parse_literal(s
, index
))
68 pub(crate) fn parse(mut s
: &[u8], span
: Span
) -> Result
<Vec
<FormatItem
<'_
>>, Error
> {
69 let mut compound
= Vec
::new();
73 let ParsedItem { item, remaining }
=
74 parse_item(s
, &mut loc
).map_err(|error
| Error
::InvalidFormatDescription
{
76 span_start
: Some(span
),