Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FromStr impl for Layout #19

Merged
merged 4 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading