Skip to content

Commit d924099

Browse files
committed
cameras.xml parsing
1 parent 6799ccb commit d924099

File tree

79 files changed

+6548
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+6548
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ members = [
1515
"src/memory/endianness",
1616
"src/memory/fixed_length_load",
1717
"src/memory/variable_length_load",
18+
"src/metadata/camerasxml_parser",
1819
"src/misc/md5",
1920
"src/std",
2021
"src/std/ndslice",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[package]
2+
name = "rawspeed-metadata-camerasxml_parser"
3+
version.workspace = true
4+
authors.workspace = true
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
documentation.workspace = true
8+
homepage.workspace = true
9+
repository.workspace = true
10+
license.workspace = true
11+
12+
[lints]
13+
workspace = true
14+
15+
[dependencies]
16+
proc-macro2 = { version = "1.0", default-features = false, features = [] }
17+
syn = { version = "2.0", default-features = false, features = ["parsing", "extra-traits"] }
18+
19+
[dev-dependencies]
20+
21+
[lib]
22+
path = "mod.rs"
23+
24+
[[bin]]
25+
name = "xmllint"
26+
path = "xmllint.rs"
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use super::*;
2+
3+
#[derive(Debug, PartialEq, Eq)]
4+
struct MaybeId {
5+
pub value: Option<id_attr::Id>,
6+
}
7+
8+
impl syn::parse::Parse for MaybeId {
9+
fn parse(input: syn::parse::ParseStream<'_>) -> syn::parse::Result<Self> {
10+
if input.peek(kw::id) {
11+
return Ok(Self {
12+
value: Some(input.parse()?),
13+
});
14+
}
15+
Ok(Self { value: None })
16+
}
17+
}
18+
19+
#[derive(Debug, PartialEq, Eq)]
20+
pub(crate) struct Alias {
21+
pub start_lt_token: syn::Token![<],
22+
pub start_token: kw::Alias,
23+
pub id: MaybeId,
24+
pub start_gt_token: syn::Token![>],
25+
pub value: VerbatimStr,
26+
pub end_lt_token: syn::Token![<],
27+
pub end_slash_token: syn::Token![/],
28+
pub end_token: kw::Alias,
29+
pub end_gt_token: syn::Token![>],
30+
}
31+
32+
impl syn::parse::Parse for Alias {
33+
fn parse(input: syn::parse::ParseStream<'_>) -> syn::parse::Result<Self> {
34+
let lookahead = input.lookahead1();
35+
if !lookahead.peek(syn::Token![<]) {
36+
return Err(lookahead.error());
37+
}
38+
Ok(Self {
39+
start_lt_token: input.parse()?,
40+
start_token: input.parse()?,
41+
id: input.parse()?,
42+
start_gt_token: input.parse()?,
43+
value: input.parse()?,
44+
end_lt_token: input.parse()?,
45+
end_slash_token: input.parse()?,
46+
end_token: input.parse()?,
47+
end_gt_token: input.parse()?,
48+
})
49+
}
50+
}
51+
52+
#[cfg(test)]
53+
mod tests;
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
use super::*;
2+
3+
#[test]
4+
#[should_panic(expected = "unexpected end of input, expected `<`")]
5+
fn parse_alias_incorrect0_test() {
6+
let src = r#""#;
7+
panic!("{}", syn::parse_str::<Alias>(src).unwrap_err());
8+
}
9+
10+
#[test]
11+
#[should_panic(expected = "unexpected end of input, expected `Alias`")]
12+
fn parse_alias_incorrect1_test() {
13+
let src = r#"<"#;
14+
panic!("{}", syn::parse_str::<Alias>(src).unwrap_err());
15+
}
16+
17+
#[test]
18+
#[should_panic(expected = "expected `>`")]
19+
fn parse_alias_incorrect2_test() {
20+
let src = r#"<Alias"#;
21+
panic!("{}", syn::parse_str::<Alias>(src).unwrap_err());
22+
}
23+
24+
#[test]
25+
#[should_panic(expected = "expected `Alias`")]
26+
fn parse_alias_incorrect3_test() {
27+
let src = r#"<Aliasid"#;
28+
panic!("{}", syn::parse_str::<Alias>(src).unwrap_err());
29+
}
30+
31+
#[test]
32+
#[should_panic(expected = "expected `=`")]
33+
fn parse_alias_incorrect4_test() {
34+
let src = r#"<Alias id"#;
35+
panic!("{}", syn::parse_str::<Alias>(src).unwrap_err());
36+
}
37+
38+
#[test]
39+
#[should_panic(expected = "unexpected end of input, expected string literal")]
40+
fn parse_alias_incorrect5_test() {
41+
let src = r#"<Alias id="#;
42+
panic!("{}", syn::parse_str::<Alias>(src).unwrap_err());
43+
}
44+
45+
#[test]
46+
#[should_panic(expected = "expected string literal")]
47+
fn parse_alias_incorrect6_test() {
48+
let src = r#"<Alias id=foo"#;
49+
panic!("{}", syn::parse_str::<Alias>(src).unwrap_err());
50+
}
51+
52+
#[test]
53+
#[should_panic(expected = "expected `>`")]
54+
fn parse_alias_incorrect7_test() {
55+
let src = r#"<Alias id="foo""#;
56+
panic!("{}", syn::parse_str::<Alias>(src).unwrap_err());
57+
}
58+
59+
#[test]
60+
#[should_panic(
61+
expected = "unexpected end of input, no `<` was found after this point"
62+
)]
63+
fn parse_alias_incorrect8_test() {
64+
let src = r#"<Alias id="foo">"#;
65+
panic!("{}", syn::parse_str::<Alias>(src).unwrap_err());
66+
}
67+
68+
#[test]
69+
#[should_panic(expected = "expected `/`")]
70+
fn parse_alias_incorrect9_test() {
71+
let src = r#"<Alias id="foo"><"#;
72+
panic!("{}", syn::parse_str::<Alias>(src).unwrap_err());
73+
}
74+
75+
#[test]
76+
#[should_panic(expected = "expected `Alias`")]
77+
fn parse_alias_incorrect10_test() {
78+
let src = r#"<Alias id="foo"></"#;
79+
panic!("{}", syn::parse_str::<Alias>(src).unwrap_err());
80+
}
81+
82+
#[test]
83+
#[should_panic(expected = "expected `>`")]
84+
fn parse_alias_incorrect11_test() {
85+
let src = r#"<Alias id="foo"></Alias"#;
86+
panic!("{}", syn::parse_str::<Alias>(src).unwrap_err());
87+
}
88+
89+
#[test]
90+
fn parse_alias_test() -> Result<(), syn::Error> {
91+
let src = r#"<Alias>Bar</Alias>"#;
92+
let repr = r#"Alias {
93+
start_lt_token: Lt,
94+
start_token: Keyword [Alias],
95+
id: MaybeId {
96+
value: None,
97+
},
98+
start_gt_token: Gt,
99+
value: VerbatimStr {
100+
value: LitStr {
101+
token: "Bar",
102+
},
103+
},
104+
end_lt_token: Lt,
105+
end_slash_token: Slash,
106+
end_token: Keyword [Alias],
107+
end_gt_token: Gt,
108+
}"#;
109+
let expr = syn::parse_str::<Alias>(src)?;
110+
assert_eq!(format!("{expr:#?}"), repr);
111+
Ok(())
112+
}
113+
114+
#[test]
115+
fn parse_alias_with_id_test() -> Result<(), syn::Error> {
116+
let src = r#"<Alias id="foo">Bar</Alias>"#;
117+
let repr = r#"Alias {
118+
start_lt_token: Lt,
119+
start_token: Keyword [Alias],
120+
id: MaybeId {
121+
value: Some(
122+
Id {
123+
token: Keyword [id],
124+
eq_token: Eq,
125+
value: LitStr {
126+
token: "foo",
127+
},
128+
},
129+
),
130+
},
131+
start_gt_token: Gt,
132+
value: VerbatimStr {
133+
value: LitStr {
134+
token: "Bar",
135+
},
136+
},
137+
end_lt_token: Lt,
138+
end_slash_token: Slash,
139+
end_token: Keyword [Alias],
140+
end_gt_token: Gt,
141+
}"#;
142+
let expr = syn::parse_str::<Alias>(src)?;
143+
assert_eq!(format!("{expr:#?}"), repr);
144+
Ok(())
145+
}
146+
147+
#[test]
148+
fn parse_alias_with_id_and_space_test() -> Result<(), syn::Error> {
149+
let src = r#"<Alias id="foo"> Baq Quux </Alias>"#;
150+
let repr = r#"Alias {
151+
start_lt_token: Lt,
152+
start_token: Keyword [Alias],
153+
id: MaybeId {
154+
value: Some(
155+
Id {
156+
token: Keyword [id],
157+
eq_token: Eq,
158+
value: LitStr {
159+
token: "foo",
160+
},
161+
},
162+
),
163+
},
164+
start_gt_token: Gt,
165+
value: VerbatimStr {
166+
value: LitStr {
167+
token: "Baq Quux",
168+
},
169+
},
170+
end_lt_token: Lt,
171+
end_slash_token: Slash,
172+
end_token: Keyword [Alias],
173+
end_gt_token: Gt,
174+
}"#;
175+
let expr = syn::parse_str::<Alias>(src)?;
176+
assert_eq!(format!("{expr:#?}"), repr);
177+
Ok(())
178+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use super::*;
2+
3+
#[derive(Debug, PartialEq, Eq)]
4+
struct IndividualAliases {
5+
pub values: Vec<alias::Alias>,
6+
}
7+
8+
impl syn::parse::Parse for IndividualAliases {
9+
fn parse(input: syn::parse::ParseStream<'_>) -> syn::parse::Result<Self> {
10+
let mut values = Vec::new();
11+
while input.peek(syn::Token![<]) && input.peek2(kw::Alias) {
12+
values.push(input.parse()?);
13+
}
14+
if values.is_empty() {
15+
return Err(syn::Error::new(
16+
input.span(),
17+
"unexpected end of input, expected `Alias`",
18+
));
19+
}
20+
Ok(Self { values })
21+
}
22+
}
23+
24+
#[derive(Debug, PartialEq, Eq)]
25+
pub(crate) struct Aliases {
26+
pub start_lt_token: syn::Token![<],
27+
pub start_token: kw::Aliases,
28+
pub start_gt_token: syn::Token![>],
29+
pub value: IndividualAliases,
30+
pub end_lt_token: syn::Token![<],
31+
pub end_slash_token: syn::Token![/],
32+
pub end_token: kw::Aliases,
33+
pub end_gt_token: syn::Token![>],
34+
}
35+
36+
impl syn::parse::Parse for Aliases {
37+
fn parse(input: syn::parse::ParseStream<'_>) -> syn::parse::Result<Self> {
38+
let lookahead = input.lookahead1();
39+
if !lookahead.peek(syn::Token![<]) {
40+
return Err(lookahead.error());
41+
}
42+
Ok(Self {
43+
start_lt_token: input.parse()?,
44+
start_token: input.parse()?,
45+
start_gt_token: input.parse()?,
46+
value: input.parse()?,
47+
end_lt_token: input.parse()?,
48+
end_slash_token: input.parse()?,
49+
end_token: input.parse()?,
50+
end_gt_token: input.parse()?,
51+
})
52+
}
53+
}
54+
55+
#[cfg(test)]
56+
mod tests;

0 commit comments

Comments
 (0)