Skip to content

Commit

Permalink
Merge pull request #19 from scpwiki/layout-parse
Browse files Browse the repository at this point in the history
Add FromStr impl for Layout
  • Loading branch information
emmiegit authored Aug 2, 2024
2 parents 2b19da9 + ffaa106 commit 23f499d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
edition = "2021"

Expand Down
53 changes: 53 additions & 0 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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
Expand Down Expand Up @@ -47,3 +49,54 @@ impl Layout {
}
}
}

impl FromStr for Layout {
type Err = LayoutParseError;

fn from_str(s: &str) -> Result<Self, LayoutParseError> {
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<Layout, LayoutParseError> = $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");
}
4 changes: 2 additions & 2 deletions src/parsing/rule/impls/block/blocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -42,7 +42,7 @@ mod prelude {
);
}

#[cfg(not(debug))]
#[cfg(not(debug_assertions))]
#[inline]
pub fn assert_generic_name(_: &[&str], _: &str, _: &str) {}

Expand Down
4 changes: 2 additions & 2 deletions src/parsing/rule/impls/block/blocks/ruby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down

0 comments on commit 23f499d

Please sign in to comment.