Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Jackson committed Jan 21, 2023
1 parent fdb46f5 commit d8c806a
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 47 deletions.
12 changes: 6 additions & 6 deletions src/de/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,14 @@ where
// that value should be taken from the text content of the
// XML node
seed.deserialize(VALUE_KEY.into_deserializer()).map(Some)
},
}
DeEvent::Text(_) | DeEvent::CData(_) => {
self.source = ValueSource::Text;
// Deserialize `key` from special attribute name which means
// that value should be taken from the text content of the
// XML node
seed.deserialize(TEXT_KEY.into_deserializer()).map(Some)
},
}
// Used to deserialize collections of enums, like:
// <root>
// <A/>
Expand All @@ -272,13 +272,13 @@ where
DeEvent::Start(e) if self.has_value_field && not_in(self.fields, e, decoder)? => {
self.source = ValueSource::Content;
seed.deserialize(VALUE_KEY.into_deserializer()).map(Some)
},
}
DeEvent::Start(e) => {
self.source = ValueSource::Nested;

let de = QNameDeserializer::from_elem(e.name(), decoder)?;
seed.deserialize(de).map(Some)
},
}
// Stop iteration after reaching a closing tag
DeEvent::End(e) if e.name() == self.start.name() => Ok(None),
// This is a unmatched closing tag, so the XML is invalid
Expand Down Expand Up @@ -701,7 +701,7 @@ where
DeEvent::Start(e) if !self.filter.is_suitable(e, decoder)? => {
self.map.de.skip()?;
continue;
},
}
// Stop iteration when list elements ends
#[cfg(not(feature = "overlapped-lists"))]
DeEvent::Start(e) if !self.filter.is_suitable(e, decoder)? => Ok(None),
Expand Down Expand Up @@ -847,7 +847,7 @@ where
// TODO: May be assert that here we expect only matching closing tag?
self.map.de.read_to_end(e.name())?;
value
},
}
// SAFETY: we use that deserializer only when Start(element), Text,
// or CData event Start(tag), Text, CData was peeked already
_ => unreachable!(),
Expand Down
76 changes: 69 additions & 7 deletions src/events/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,16 +632,11 @@ impl<'a> IterState<'a> {
Some((_, b'\'')) => b'\'',

Some((offset, &q)) => {
if let Some(q) = self
.custom_quotes
.iter()
.find(|(o, _)| *o == q)
.map(|(_, c)| *c)
{
if let Some((_, c)) = self.custom_quotes.iter().find(|(o, _)| *o == q) {
// Input: ` key = q`
// | ^
// offset
q
*c
} else {
// Input: ` key = x`
// | ^
Expand Down Expand Up @@ -1057,6 +1052,26 @@ mod xml {
assert_eq!(iter.next(), None);
}

#[test]
fn double_squoted() {
let mut iter = Attributes::new_with_custom_quotes(
r#"tag key=`value()=>} other=`(e) => {}}"#,
3,
&[(b'`', b'}')],
);

assert_eq!(
iter.next(),
Some(Ok(Attribute {
key: QName(b"key"),
value: Cow::Borrowed(b"value()=>"),
quote: Attr::CustomQ(b'`', b'}', (), ())
}))
);
assert_eq!(iter.next(), None);
assert_eq!(iter.next(), None);
}

/// Attribute have a value enclosed in double quotes
#[test]
fn double_quoted() {
Expand All @@ -1082,6 +1097,35 @@ mod xml {
assert_eq!(iter.next(), None);
}

/// Attribute have a value enclosed in custom quotes
#[test]
fn custom_quoted() {
let mut iter = Attributes::new_with_custom_quotes(
r#"tag key=`value` regular='attribute'"#,
3,
&[(b'`', b'`')],
);

assert_eq!(
iter.next(),
Some(Ok(Attribute {
key: QName(b"key"),
value: Cow::Borrowed(b"value"),
quote: Attr::CustomQ(b'`', b'`', (), ())
}))
);
assert_eq!(
iter.next(),
Some(Ok(Attribute {
key: QName(b"regular"),
value: Cow::Borrowed(b"attribute"),
quote: Attr::SingleQ((), ())
}))
);
assert_eq!(iter.next(), None);
assert_eq!(iter.next(), None);
}

/// Attribute have a value, not enclosed in quotes
#[test]
fn unquoted() {
Expand Down Expand Up @@ -1271,6 +1315,24 @@ mod xml {
assert_eq!(iter.next(), None);
}

/// Attribute have a value, not enclosed in quotes
#[test]
fn custom_quoted() {
let mut iter =
Attributes::new_with_custom_quotes(r#"tag key = `value` "#, 3, &[(b'`', b'`')]);

assert_eq!(
iter.next(),
Some(Ok(Attribute {
key: QName(b"key"),
value: Cow::Borrowed(b"value"),
quote: Attr::CustomQ(b'`', b'`', (), ())
}))
);
assert_eq!(iter.next(), None);
assert_eq!(iter.next(), None);
}

/// Attribute have a value, not enclosed in quotes
#[test]
fn unquoted() {
Expand Down
12 changes: 6 additions & 6 deletions src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl<'ns> Debug for ResolveResult<'ns> {
write!(f, "Unknown(")?;
write_byte_string(f, p)?;
write!(f, ")")
},
}
}
}
}
Expand Down Expand Up @@ -424,7 +424,7 @@ impl NamespaceResolver {
value_len: v.len(),
level,
});
},
}
Some(PrefixDeclaration::Named(prefix)) => {
let start = buffer.len();
buffer.extend_from_slice(prefix);
Expand All @@ -435,8 +435,8 @@ impl NamespaceResolver {
value_len: v.len(),
level,
});
},
None => {},
}
None => {}
}
} else {
break;
Expand All @@ -457,14 +457,14 @@ impl NamespaceResolver {
None => {
buffer.clear();
self.bindings.clear();
},
}
// drop all namespaces past the last valid namespace
Some(last_valid_pos) => {
if let Some(len) = self.bindings.get(last_valid_pos + 1).map(|n| n.start) {
buffer.truncate(len);
self.bindings.truncate(last_valid_pos + 1);
}
},
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,8 +776,8 @@ impl BangType {
if i == 0 && buf.ends_with(b"--") {
return Some((&[], i + 1)); // +1 for `>`
}
},
Self::Comment => {},
}
Self::Comment => {}
Self::CData => {
if chunk[..i].ends_with(b"]]") {
return Some((&chunk[..i], i + 1)); // +1 for `>`
Expand All @@ -792,7 +792,7 @@ impl BangType {
if i == 0 && buf.ends_with(b"]]") {
return Some((&[], i + 1)); // +1 for `>`
}
},
}
Self::DocType => {
let content = &chunk[..i];
let balance = memchr::memchr2_iter(b'<', b'>', content)
Expand All @@ -801,7 +801,7 @@ impl BangType {
if balance == 0 {
return Some((content, i + 1)); // +1 for `>`
}
},
}
}
}
None
Expand Down Expand Up @@ -854,7 +854,7 @@ impl ReadElementState {
} else {
*self
}
},
}

// all other bytes: no state change
_ => *self,
Expand Down
6 changes: 3 additions & 3 deletions src/reader/ns_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,20 @@ impl<R> NsReader<R> {
Ok(Event::Start(e)) => {
self.ns_resolver.push(&e, &mut self.buffer);
Ok(Event::Start(e))
},
}
Ok(Event::Empty(e)) => {
self.ns_resolver.push(&e, &mut self.buffer);
// notify next `read_event_impl()` invocation that it needs to pop this
// namespace scope
self.pending_pop = true;
Ok(Event::Empty(e))
},
}
Ok(Event::End(e)) => {
// notify next `read_event_impl()` invocation that it needs to pop this
// namespace scope
self.pending_pop = true;
Ok(Event::End(e))
},
}
e => e,
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/encodings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn test_koi8_r_encoding() {
match r.read_event_into(&mut buf) {
Ok(Event::Text(e)) => {
e.unescape().unwrap();
},
}
Ok(Event::Eof) => break,
_ => (),
}
Expand Down
14 changes: 7 additions & 7 deletions tests/namespaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ fn default_ns_shadowing_empty() {
Ok((ns, Start(e))) => {
assert_eq!(ns, Bound(Namespace(b"urn:example:o")));
assert_eq!(e.name(), QName(b"e"));
},
}
e => panic!("Expected Start event (<outer>), got {:?}", e),
}
}
Expand All @@ -243,7 +243,7 @@ fn default_ns_shadowing_empty() {
assert_eq!(ns, Bound(Namespace(b"urn:example:i")));
assert_eq!(e.name(), QName(b"e"));
e
},
}
e => panic!("Expecting Empty event, got {:?}", e),
};

Expand Down Expand Up @@ -274,7 +274,7 @@ fn default_ns_shadowing_empty() {
Ok((ns, End(e))) => {
assert_eq!(ns, Bound(Namespace(b"urn:example:o")));
assert_eq!(e.name(), QName(b"e"));
},
}
e => panic!("Expected End event (<outer>), got {:?}", e),
}
}
Expand All @@ -292,7 +292,7 @@ fn default_ns_shadowing_expanded() {
Ok((ns, Start(e))) => {
assert_eq!(ns, Bound(Namespace(b"urn:example:o")));
assert_eq!(e.name(), QName(b"e"));
},
}
e => panic!("Expected Start event (<outer>), got {:?}", e),
}
}
Expand All @@ -304,7 +304,7 @@ fn default_ns_shadowing_expanded() {
assert_eq!(ns, Bound(Namespace(b"urn:example:i")));
assert_eq!(e.name(), QName(b"e"));
e
},
}
e => panic!("Expecting Start event (<inner>), got {:?}", e),
};
let mut attrs = e
Expand Down Expand Up @@ -334,15 +334,15 @@ fn default_ns_shadowing_expanded() {
Ok((ns, End(e))) => {
assert_eq!(ns, Bound(Namespace(b"urn:example:i")));
assert_eq!(e.name(), QName(b"e"));
},
}
e => panic!("Expected End event (</inner>), got {:?}", e),
}
// </outer>
match r.read_resolved_event() {
Ok((ns, End(e))) => {
assert_eq!(ns, Bound(Namespace(b"urn:example:o")));
assert_eq!(e.name(), QName(b"e"));
},
}
e => panic!("Expected End event (</outer>), got {:?}", e),
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn test_attributes_empty() {
}))
);
assert_eq!(attrs.next(), None);
},
}
e => panic!("Expecting Empty event, got {:?}", e),
}
}
Expand All @@ -70,7 +70,7 @@ fn test_attribute_equal() {
}))
);
assert_eq!(attrs.next(), None);
},
}
e => panic!("Expecting Empty event, got {:?}", e),
}
}
Expand All @@ -85,7 +85,7 @@ fn test_comment_starting_with_gt() {
Ok(Comment(e)) => {
assert_eq!(e.as_ref(), b">");
break;
},
}
Ok(Eof) => panic!("Expecting Comment"),
_ => (),
}
Expand Down Expand Up @@ -181,7 +181,7 @@ fn test_issue299() -> Result<(), Error> {
"mismatch att count on '{:?}'",
reader.decoder().decode(e.name().as_ref())
);
},
}
Eof => break,
_ => (),
}
Expand Down
Loading

0 comments on commit d8c806a

Please sign in to comment.