Skip to content

Commit e0b1751

Browse files
committed
Add configuration parameters for tag names
1 parent b9b6484 commit e0b1751

File tree

5 files changed

+263
-220
lines changed

5 files changed

+263
-220
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ members = [
88
]
99

1010
[workspace.package]
11-
version = "0.5.0"
11+
version = "0.6.0-pre1"
1212
authors = ["Kailan Blanks <[email protected]>"]
1313
license = "MIT"
1414
edition = "2018"

esi/src/config.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
#[allow(clippy::return_self_not_must_use)]
99
#[derive(Clone, Debug)]
1010
pub struct Configuration {
11-
/// The XML namespace to use when scanning for ESI tags. Defaults to `esi`.
12-
pub namespace: String,
11+
// Define the tag names that the processor will operate on.
12+
pub tag_names: TagNames,
1313
/// For working with non-HTML ESI templates, e.g. JSON files, this option allows you to disable the unescaping of URLs
1414
pub is_escaped_content: bool,
1515
}
1616

1717
impl Default for Configuration {
1818
fn default() -> Self {
1919
Self {
20-
namespace: String::from("esi"),
20+
tag_names: Default::default(),
2121
is_escaped_content: true,
2222
}
2323
}
@@ -28,7 +28,12 @@ impl Configuration {
2828
///
2929
/// For example, setting this to `test` would cause the processor to only match tags like `<test:include>`.
3030
pub fn with_namespace(mut self, namespace: impl Into<String>) -> Self {
31-
self.namespace = namespace.into();
31+
self.tag_names = TagNames::from_namespace_with_defaults(&namespace.into());
32+
self
33+
}
34+
/// Sets the tag names that the processor will operate on.
35+
pub fn with_tag_names(mut self, tag_names: TagNames) -> Self {
36+
self.tag_names = tag_names;
3237
self
3338
}
3439
/// For working with non-HTML ESI templates, eg JSON files, allows to disable URLs unescaping
@@ -37,3 +42,34 @@ impl Configuration {
3742
self
3843
}
3944
}
45+
46+
/// Defines the HTML tag names that the processor will operate on.
47+
#[derive(Clone, Debug)]
48+
pub struct TagNames {
49+
pub include: Vec<u8>,
50+
pub comment: Vec<u8>,
51+
pub remove: Vec<u8>,
52+
pub r#try: Vec<u8>,
53+
pub attempt: Vec<u8>,
54+
pub except: Vec<u8>,
55+
}
56+
57+
impl TagNames {
58+
/// Returns tag names as defined within the ESI specification within the given namespace.
59+
pub fn from_namespace_with_defaults(namespace: &str) -> Self {
60+
Self {
61+
include: format!("{namespace}:include",).into_bytes(),
62+
comment: format!("{namespace}:comment",).into_bytes(),
63+
remove: format!("{namespace}:remove",).into_bytes(),
64+
r#try: format!("{namespace}:try",).into_bytes(),
65+
attempt: format!("{namespace}:attempt",).into_bytes(),
66+
except: format!("{namespace}:except",).into_bytes(),
67+
}
68+
}
69+
}
70+
71+
impl Default for TagNames {
72+
fn default() -> Self {
73+
Self::from_namespace_with_defaults("esi")
74+
}
75+
}

esi/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ use log::{debug, error, trace};
1313
use std::collections::VecDeque;
1414
use std::io::{BufRead, Write};
1515

16+
pub use crate::config::{Configuration, TagNames};
1617
pub use crate::document::{Element, Fragment};
18+
pub use crate::error::ExecutionError;
1719
pub use crate::error::Result;
1820
pub use crate::parse::{parse_tags, Event, Include, Tag, Tag::Try};
1921

20-
pub use crate::config::Configuration;
21-
pub use crate::error::ExecutionError;
22-
2322
// re-export quick_xml Reader and Writer
2423
pub use quick_xml::{Reader, Writer};
2524

@@ -147,7 +146,7 @@ impl Processor {
147146
// on each tag / event it finds in the document.
148147
// The callback function `handle_events` will handle the event.
149148
parse_tags(
150-
&self.configuration.namespace,
149+
&self.configuration.tag_names,
151150
&mut src_document,
152151
&mut |event| {
153152
event_receiver(

esi/src/parse.rs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{ExecutionError, Result};
1+
use crate::{ExecutionError, Result, TagNames};
22
use log::debug;
33
use quick_xml::events::{BytesStart, Event as XmlEvent};
44
use quick_xml::name::QName;
@@ -43,28 +43,6 @@ pub enum Event<'e> {
4343
ESI(Tag<'e>),
4444
}
4545

46-
// #[derive(Debug)]
47-
struct TagNames {
48-
include: Vec<u8>,
49-
comment: Vec<u8>,
50-
remove: Vec<u8>,
51-
r#try: Vec<u8>,
52-
attempt: Vec<u8>,
53-
except: Vec<u8>,
54-
}
55-
impl TagNames {
56-
fn init(namespace: &str) -> Self {
57-
Self {
58-
include: format!("{namespace}:include",).into_bytes(),
59-
comment: format!("{namespace}:comment",).into_bytes(),
60-
remove: format!("{namespace}:remove",).into_bytes(),
61-
r#try: format!("{namespace}:try",).into_bytes(),
62-
attempt: format!("{namespace}:attempt",).into_bytes(),
63-
except: format!("{namespace}:except",).into_bytes(),
64-
}
65-
}
66-
}
67-
6846
fn do_parse<'a, R>(
6947
reader: &mut Reader<R>,
7048
callback: &mut dyn FnMut(Event<'a>) -> Result<()>,
@@ -185,7 +163,7 @@ where
185163

186164
/// Parses the ESI document from the given `reader` and calls the `callback` closure upon each successfully parsed ESI tag.
187165
pub fn parse_tags<'a, R>(
188-
namespace: &str,
166+
tag_names: &TagNames,
189167
reader: &mut Reader<R>,
190168
callback: &mut dyn FnMut(Event<'a>) -> Result<()>,
191169
) -> Result<()>
@@ -194,8 +172,6 @@ where
194172
{
195173
debug!("Parsing document...");
196174

197-
// Initialize the ESI tags
198-
let tags = TagNames::init(namespace);
199175
// set the initial depth of nested tags
200176
let mut depth = 0;
201177
let mut root = Vec::new();
@@ -208,7 +184,7 @@ where
208184
&mut root,
209185
&mut depth,
210186
&mut current_arm,
211-
&tags,
187+
tag_names,
212188
)?;
213189
debug!("Root: {:?}", root);
214190

0 commit comments

Comments
 (0)