Use as_variant crate for shorter code

This commit is contained in:
Jonas Platte
2023-08-09 13:46:50 +02:00
parent ed03c0d2fa
commit 399adc911f
11 changed files with 37 additions and 104 deletions

View File

@@ -15,6 +15,7 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[dependencies]
as_variant = { workspace = true }
html5ever = "0.26.0"
phf = { version = "0.11.1", features = ["macros"] }
tracing = { workspace = true, features = ["attributes"] }

View File

@@ -1,5 +1,6 @@
use std::{collections::BTreeSet, fmt, io};
use as_variant::as_variant;
use html5ever::{
local_name, namespace_url, ns, parse_fragment,
serialize::{serialize, Serialize, SerializeOpts, Serializer, TraversalScope},
@@ -290,26 +291,17 @@ impl Node {
/// Returns the `ElementData` of this `Node` if it is a `NodeData::Element`.
pub fn as_element(&self) -> Option<&ElementData> {
match &self.data {
NodeData::Element(data) => Some(data),
_ => None,
}
as_variant!(&self.data, NodeData::Element)
}
/// Returns the mutable `ElementData` of this `Node` if it is a `NodeData::Element`.
pub fn as_element_mut(&mut self) -> Option<&mut ElementData> {
match &mut self.data {
NodeData::Element(data) => Some(data),
_ => None,
}
as_variant!(&mut self.data, NodeData::Element)
}
/// Returns the mutable text content of this `Node`, if it is a `NodeData::Text`.
pub fn as_text_mut(&mut self) -> Option<&mut StrTendril> {
match &mut self.data {
NodeData::Text(data) => Some(data),
_ => None,
}
as_variant!(&mut self.data, NodeData::Text)
}
}