Skip to content

Commit 422ebc1

Browse files
committed
make SourceSpan optional
1 parent 29f9f7b commit 422ebc1

4 files changed

Lines changed: 24 additions & 15 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ thiserror = "1.0.40"
1717
serde = { version = "1.0.159", features = ["derive"] }
1818
serde_json = "1.0.95"
1919

20+
[features]
21+
default = ["source-span"]
22+
source-span = []
23+
2024
[dev-dependencies]
2125
indoc = "2.0.1"
2226
insta = { version = "1.29.0", features = ["json"] }

src/dom/element.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::node::Node;
2+
#[cfg(feature = "source-span")]
23
use super::span::SourceSpan;
34
use serde::{Serialize, Serializer};
45
use std::collections::{BTreeMap, HashMap};
@@ -45,6 +46,7 @@ pub struct Element {
4546
#[serde(skip_serializing_if = "Vec::is_empty")]
4647
pub children: Vec<Node>,
4748

49+
#[cfg(feature = "source-span")]
4850
/// Span of the element in the parsed source
4951
#[serde(skip)]
5052
pub source_span: SourceSpan,
@@ -59,6 +61,7 @@ impl Default for Element {
5961
classes: vec![],
6062
attributes: HashMap::new(),
6163
children: vec![],
64+
#[cfg(feature = "source-span")]
6265
source_span: SourceSpan::default(),
6366
}
6467
}

src/dom/mod.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ use crate::grammar::Grammar;
1010
pub mod element;
1111
pub mod formatting;
1212
pub mod node;
13+
#[cfg(feature = "source-span")]
1314
pub mod span;
1415

16+
#[cfg(feature = "source-span")]
1517
use crate::dom::span::SourceSpan;
1618
use element::{Element, ElementVariant};
1719
use node::Node;
@@ -232,22 +234,21 @@ impl Dom {
232234
}
233235

234236
fn build_node_element(pair: Pair<Rule>, dom: &mut Dom) -> Result<Option<Node>> {
235-
let source_span = {
236-
let pair_span = pair.as_span();
237-
let (start_line, start_column) = pair_span.start_pos().line_col();
238-
let (end_line, end_column) = pair_span.end_pos().line_col();
239-
240-
SourceSpan::new(
241-
String::from(pair_span.as_str()),
242-
start_line,
243-
end_line,
244-
start_column,
245-
end_column,
246-
)
247-
};
248-
249237
let mut element = Element {
250-
source_span,
238+
#[cfg(feature = "source-span")]
239+
source_span: {
240+
let pair_span = pair.as_span();
241+
let (start_line, start_column) = pair_span.start_pos().line_col();
242+
let (end_line, end_column) = pair_span.end_pos().line_col();
243+
244+
SourceSpan::new(
245+
String::from(pair_span.as_str()),
246+
start_line,
247+
end_line,
248+
start_column,
249+
end_column,
250+
)
251+
},
251252
..Element::default()
252253
};
253254

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ pub use crate::dom::Dom;
106106
pub use crate::dom::DomVariant;
107107
pub use crate::dom::element::{Element, ElementVariant};
108108
pub use crate::dom::node::Node;
109+
#[cfg(feature = "source-span")]
109110
pub use crate::dom::span::SourceSpan;
110111
pub use crate::error::Error;
111112
pub use crate::error::Result;

0 commit comments

Comments
 (0)