diff --git a/Cargo.toml b/Cargo.toml index 8142b460..296937b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ keywords = ["wikidot", "wikijump", "ftml", "parsing", "html"] categories = ["parser-implementations"] exclude = [".gitignore", ".editorconfig"] -version = "1.25.1" +version = "1.26.0" authors = ["Emmie Smith "] edition = "2021" diff --git a/src/layout.rs b/src/layout.rs index 6ac30b84..1d10a8a0 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -18,6 +18,8 @@ * along with this program. If not, see . */ +use std::str::FromStr; + /// Describes the desired (HTML) DOM layout to be emitted. /// /// This is used as a transition mechanism between our dependencies on the pecularities @@ -47,3 +49,54 @@ impl Layout { } } } + +impl FromStr for Layout { + type Err = LayoutParseError; + + fn from_str(s: &str) -> Result { + if s.eq_ignore_ascii_case("wikidot") { + Ok(Layout::Wikidot) + } else if s.eq_ignore_ascii_case("wikijump") { + Ok(Layout::Wikijump) + } else { + Err(LayoutParseError) + } + } +} + +#[derive(Debug)] +pub struct LayoutParseError; + +#[test] +fn test_layout() { + macro_rules! check { + ($input:expr, $expected:ident $(,)?) => {{ + let actual: Layout = $input.parse().expect("Invalid layout string"); + let expected = Layout::$expected; + + assert_eq!( + actual, expected, + "Actual layout enum doesn't match expected", + ); + }}; + } + + macro_rules! check_err { + ($input:expr $(,)?) => {{ + let result: Result = $input.parse(); + result.expect_err("Unexpected valid layout string"); + }}; + } + + check!("wikidot", Wikidot); + check!("Wikidot", Wikidot); + check!("WIKIDOT", Wikidot); + + check!("wikijump", Wikijump); + check!("Wikijump", Wikijump); + check!("WIKIJUMP", Wikijump); + + check_err!("invalid"); + check_err!("XXX"); + check_err!("foobar"); +} diff --git a/src/parsing/rule/impls/block/blocks/mod.rs b/src/parsing/rule/impls/block/blocks/mod.rs index 7865c168..c864685a 100644 --- a/src/parsing/rule/impls/block/blocks/mod.rs +++ b/src/parsing/rule/impls/block/blocks/mod.rs @@ -25,7 +25,7 @@ mod prelude { pub use crate::parsing::ParseError; pub use crate::tree::{Container, ContainerType, Element}; - #[cfg(debug)] + #[cfg(debug_assertions)] pub fn assert_generic_name( expected_names: &[&str], actual_name: &str, @@ -42,7 +42,7 @@ mod prelude { ); } - #[cfg(not(debug))] + #[cfg(not(debug_assertions))] #[inline] pub fn assert_generic_name(_: &[&str], _: &str, _: &str) {} diff --git a/src/parsing/rule/impls/block/blocks/ruby.rs b/src/parsing/rule/impls/block/blocks/ruby.rs index d15e99d4..f6846621 100644 --- a/src/parsing/rule/impls/block/blocks/ruby.rs +++ b/src/parsing/rule/impls/block/blocks/ruby.rs @@ -97,8 +97,8 @@ fn parse_block<'r, 't>( // Ensure it contains no partials cfg_if! { - if #[cfg(debug)] { - for element in elements { + if #[cfg(debug_assertions)] { + for element in &elements { if let Element::Partial(_) = element { panic!("Found partial after conversion"); }