Skip to content

Commit da767f9

Browse files
authored
Merge pull request #53 from LebedevRI/camerasxml_parsers
`cameras.xml` parsing
2 parents 6d45dde + 6a3ae04 commit da767f9

File tree

62 files changed

+6469
-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.

62 files changed

+6469
-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/metadata/xmltokendesparsifier",
1920
"src/metadata/xmltokenizer",
2021
"src/metadata/xmlparser",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
rawspeed-metadata-xmlparser = { path = "../xmlparser" }
17+
18+
[dev-dependencies]
19+
20+
[lib]
21+
path = "mod.rs"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use super::BodyStr;
2+
use super::id_attr;
3+
use super::xmlparser;
4+
5+
impl_elt_with_body_matcher!(
6+
#[derive(Debug, Clone, Copy, PartialEq)]
7+
struct Alias<'a> {
8+
id: Option<id_attr::Id<'a>>,
9+
value: BodyStr<'a>,
10+
}
11+
);
12+
13+
#[cfg(test)]
14+
mod tests;
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
use super::super::Str;
2+
use super::super::alias::Alias;
3+
use super::BodyStr;
4+
use super::id_attr::Id;
5+
use super::xmlparser;
6+
7+
type T<'a> = Alias<'a>;
8+
9+
#[expect(non_snake_case)]
10+
fn Err(str: &'static str) -> Result<T<'static>, String> {
11+
Result::Err(str.to_owned())
12+
}
13+
14+
#[test]
15+
#[expect(clippy::too_many_lines)]
16+
fn parse_test() {
17+
let inputs: Vec<&'static str> = vec![
18+
"",
19+
"<",
20+
"<Alias",
21+
"<Alias ",
22+
"<NotAlias ",
23+
"<Aliasid",
24+
"<Alias id",
25+
"<Alias id ",
26+
"<Alias not_id ",
27+
"<Alias id=",
28+
"<Alias id=foo",
29+
"<Alias id=\"foo\"",
30+
"<Alias id=\"foo\">",
31+
"<Alias id=\"foo\"><",
32+
"<Alias id=\"foo\"></",
33+
"<Alias id=\"foo\"></Alias",
34+
"<Alias>Bar</Alias>",
35+
"<Alias id=\"foo\">Bar</Alias>",
36+
"<Alias id=\"foo\"> Baq Quux </Alias>",
37+
];
38+
let expected: Vec<(&str, xmlparser::Result<T<'_>>)> = vec![
39+
(
40+
"",
41+
Err("While trying to match `\"Lt\"`, encountered end of stream"),
42+
),
43+
(
44+
"<",
45+
Err(
46+
"While trying to match `\"ElementName\"`, encountered end of stream",
47+
),
48+
),
49+
(
50+
"<Alias",
51+
Err(
52+
"While trying to match `\"ElementName\"`, but the following was encountered instead: `Garbage(\"Alias\")`",
53+
),
54+
),
55+
(
56+
"<Alias ",
57+
Err(
58+
"While trying to match `\"Gt\"`, but the following was encountered instead: `Garbage(\" \")`",
59+
),
60+
),
61+
(
62+
"<NotAlias ",
63+
Err(
64+
"Error while parsing element, expected `\"Alias\"`, but instead found: `\"NotAlias\"`",
65+
),
66+
),
67+
(
68+
"<Aliasid",
69+
Err(
70+
"While trying to match `\"ElementName\"`, but the following was encountered instead: `Garbage(\"Aliasid\")`",
71+
),
72+
),
73+
(
74+
"<Alias id",
75+
Err(
76+
"While trying to match `\"Gt\"`, but the following was encountered instead: `Garbage(\"id\")`",
77+
),
78+
),
79+
(
80+
"<Alias id ",
81+
Err(
82+
"While trying to match `\"Gt\"`, but the following was encountered instead: `ElementAttributeName(\"id\")`",
83+
),
84+
),
85+
(
86+
"<Alias not_id ",
87+
Err(
88+
"While trying to match `\"Gt\"`, but the following was encountered instead: `ElementAttributeName(\"not_id\")`",
89+
),
90+
),
91+
(
92+
"<Alias id=",
93+
Err(
94+
"While trying to match `\"Gt\"`, but the following was encountered instead: `ElementAttributeName(\"id\")`",
95+
),
96+
),
97+
(
98+
"<Alias id=foo",
99+
Err(
100+
"While trying to match `\"Gt\"`, but the following was encountered instead: `ElementAttributeName(\"id\")`",
101+
),
102+
),
103+
(
104+
"<Alias id=\"foo\"",
105+
Err("While trying to match `\"Gt\"`, encountered end of stream"),
106+
),
107+
(
108+
"<Alias id=\"foo\">",
109+
Err(
110+
"While trying to match `\"ElementContentVerbatim\"`, encountered end of stream",
111+
),
112+
),
113+
(
114+
"<Alias id=\"foo\"><",
115+
Err(
116+
"While trying to match `\"ElementContentVerbatim\"`, but the following was encountered instead: `Lt(\"<\")`",
117+
),
118+
),
119+
(
120+
"<Alias id=\"foo\"></",
121+
Err(
122+
"While trying to match `\"ElementContentVerbatim\"`, but the following was encountered instead: `Lt(\"<\")`",
123+
),
124+
),
125+
(
126+
"<Alias id=\"foo\"></Alias",
127+
Err(
128+
"While trying to match `\"ElementContentVerbatim\"`, but the following was encountered instead: `Lt(\"<\")`",
129+
),
130+
),
131+
(
132+
"<Alias>Bar</Alias>",
133+
Ok(Alias {
134+
id: None,
135+
value: BodyStr { val: "Bar" },
136+
}),
137+
),
138+
(
139+
"<Alias id=\"foo\">Bar</Alias>",
140+
Ok(Alias {
141+
id: Some(Id {
142+
val: Str { val: "foo" },
143+
}),
144+
value: BodyStr { val: "Bar" },
145+
}),
146+
),
147+
(
148+
"<Alias id=\"foo\"> Baq Quux </Alias>",
149+
Ok(Alias {
150+
id: Some(Id {
151+
val: Str { val: "foo" },
152+
}),
153+
value: BodyStr { val: " Baq Quux " },
154+
}),
155+
),
156+
];
157+
let mut results = vec![];
158+
for input in inputs {
159+
results.push((input, xmlparser::parse_str::<T<'_>>(input)));
160+
}
161+
assert_eq!(results, expected);
162+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use super::alias;
2+
use super::xmlparser;
3+
4+
#[derive(Debug, Clone, PartialEq)]
5+
pub struct IndividualAliases<'a> {
6+
pub values: Vec<alias::Alias<'a>>,
7+
}
8+
9+
impl<'a, 'b> xmlparser::Parse<'a, 'b> for IndividualAliases<'a> {
10+
fn parse(
11+
input: &'b mut xmlparser::ParseStream<'a>,
12+
) -> xmlparser::Result<Self> {
13+
const EXPECTED_NAME: &str = "Alias";
14+
let mut values = Vec::new();
15+
while let Ok(row) = input.parse() {
16+
values.push(row);
17+
}
18+
if values.is_empty() {
19+
return Err(format!(
20+
"unexpected end of input, expected `{EXPECTED_NAME}`"
21+
));
22+
}
23+
Ok(Self { values })
24+
}
25+
}
26+
27+
impl_elt_with_body_matcher!(
28+
#[derive(Debug, Clone, PartialEq)]
29+
struct Aliases<'a> {
30+
value: IndividualAliases<'a>,
31+
}
32+
);
33+
34+
#[cfg(test)]
35+
mod tests;

0 commit comments

Comments
 (0)