Skip to content

Commit

Permalink
Move TagNames struct into config module
Browse files Browse the repository at this point in the history
  • Loading branch information
kailan committed Dec 6, 2024
1 parent c78213e commit 95a8521
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
33 changes: 31 additions & 2 deletions esi/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::parse::TagNames;

/// This struct is used to configure optional behaviour within the ESI processor.
///
/// ## Usage Example
Expand Down Expand Up @@ -44,3 +42,34 @@ impl Configuration {
self
}
}

/// Defines the HTML tag names that the processor will operate on.
#[derive(Clone, Debug)]
pub struct TagNames {
pub include: Vec<u8>,
pub comment: Vec<u8>,
pub remove: Vec<u8>,
pub r#try: Vec<u8>,
pub attempt: Vec<u8>,
pub except: Vec<u8>,
}

impl TagNames {
/// Returns tag names as defined within the ESI specification within the given namespace.
pub fn from_namespace_with_defaults(namespace: &str) -> Self {
Self {
include: format!("{namespace}:include",).into_bytes(),
comment: format!("{namespace}:comment",).into_bytes(),
remove: format!("{namespace}:remove",).into_bytes(),
r#try: format!("{namespace}:try",).into_bytes(),
attempt: format!("{namespace}:attempt",).into_bytes(),
except: format!("{namespace}:except",).into_bytes(),
}
}
}

impl Default for TagNames {
fn default() -> Self {
Self::from_namespace_with_defaults("esi")
}
}
5 changes: 2 additions & 3 deletions esi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ use std::io::{BufRead, Write};

pub use crate::document::{Element, Fragment};
pub use crate::error::Result;
pub use crate::parse::{parse_tags, Event, Include, Tag, Tag::Try, TagNames};

pub use crate::config::Configuration;
pub use crate::parse::{parse_tags, Event, Include, Tag, Tag::Try};
pub use crate::config::{Configuration, TagNames};
pub use crate::error::ExecutionError;

// re-export quick_xml Reader and Writer
Expand Down
31 changes: 1 addition & 30 deletions esi/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ExecutionError, Result};
use crate::{ExecutionError, Result, TagNames};
use log::debug;
use quick_xml::events::{BytesStart, Event as XmlEvent};
use quick_xml::name::QName;
Expand Down Expand Up @@ -43,35 +43,6 @@ pub enum Event<'e> {
ESI(Tag<'e>),
}

#[derive(Clone, Debug)]
pub struct TagNames {
pub include: Vec<u8>,
pub comment: Vec<u8>,
pub remove: Vec<u8>,
pub r#try: Vec<u8>,
pub attempt: Vec<u8>,
pub except: Vec<u8>,
}

impl TagNames {
pub fn from_namespace_with_defaults(namespace: &str) -> Self {
Self {
include: format!("{namespace}:include",).into_bytes(),
comment: format!("{namespace}:comment",).into_bytes(),
remove: format!("{namespace}:remove",).into_bytes(),
r#try: format!("{namespace}:try",).into_bytes(),
attempt: format!("{namespace}:attempt",).into_bytes(),
except: format!("{namespace}:except",).into_bytes(),
}
}
}

impl Default for TagNames {
fn default() -> Self {
Self::from_namespace_with_defaults("esi")
}
}

fn do_parse<'a, R>(
reader: &mut Reader<R>,
callback: &mut dyn FnMut(Event<'a>) -> Result<()>,
Expand Down

0 comments on commit 95a8521

Please sign in to comment.