Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricereix committed Oct 14, 2023
1 parent dba0161 commit b9dacc9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
56 changes: 41 additions & 15 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,55 @@ use super::{Comment, Expression, ExpressionKind, Grammar, Rule, RuleSet};
use crate::Quantifier;
use std::collections::HashMap;


pub fn format_html(g: &Grammar, s: &str, section_header: &str, section_id: bool) -> String {
let non_terminals = g.non_terminals();
g.to_html(s, &non_terminals, section_header, section_id)
}

impl Grammar {
pub fn to_html(&self, input: &str, used_by: &HashMap<String, Vec<String>>, section_header: &str, section_id: bool) -> String {
pub fn to_html(
&self,
input: &str,
used_by: &HashMap<String, Vec<String>>,
section_header: &str,
section_id: bool,
) -> String {
let mut s = "".to_string();
for ruleset in &self.rulesets {
s.push_str(ruleset.to_html(input, used_by, section_header, section_id).as_str());
s.push_str(
ruleset
.to_html(input, used_by, section_header, section_id)
.as_str(),
);
}
s
}
}

impl RuleSet {
pub fn to_html(&self, input: &str, used_by: &HashMap<String, Vec<String>>, section_header: &str, section_id: bool) -> String {
pub fn to_html(
&self,
input: &str,
used_by: &HashMap<String, Vec<String>>,
section_header: &str,
section_id: bool,
) -> String {
let mut s = r#"<div class="grammar-ruleset">"#.to_string();
let section_id = if section_id {
format!(" id=\"{}\"", encode_html(&comment_to_id(&self.comment.value)))
format!(
" id=\"{}\"",
encode_html(&comment_to_id(&self.comment.value))
)
} else {
"".to_string()
};
s.push_str(format!(r#"<{section_header}{section_id}>{}</{section_header}>"#, self.comment.to_html()).as_str());
s.push_str(
format!(
r#"<{section_header}{section_id}>{}</{section_header}>"#,
self.comment.to_html()
)
.as_str(),
);

for rule in &self.rules {
let used_by = match used_by.get(&rule.id) {
Expand All @@ -47,16 +71,15 @@ impl Comment {
}

fn comment_to_id(value: &str) -> String {
value
.to_lowercase()
.replace('/', "-")
.replace(' ', "-")
.replace("----", "-")
.replace("---", "-")
.replace("--", "-")
value
.to_lowercase()
.replace('/', "-")
.replace(' ', "-")
.replace("----", "-")
.replace("---", "-")
.replace("--", "-")
}


impl Rule {
pub fn to_html(&self, input: &str, used_by: &[String]) -> String {
let mut s = r#"<div class="grammar-rule">"#.to_string();
Expand Down Expand Up @@ -370,7 +393,10 @@ mod tests {

#[test]
fn test_comment_to_id() {
assert_eq!(comment_to_id("Template / Expression"), "template-expression");
assert_eq!(
comment_to_id("Template / Expression"),
"template-expression"
);
assert_eq!(comment_to_id("Lexical Grammar"), "lexical-grammar");
}
}
5 changes: 4 additions & 1 deletion src/main/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ fn main() {
ExitCode::ErrorValidation.exit()
}

println!("{}", format_html(&g, &content, &options.section_header, options.section_id));
println!(
"{}",
format_html(&g, &content, &options.section_header, options.section_id)
);
ExitCode::Success.exit()
}

Expand Down
7 changes: 5 additions & 2 deletions src/main/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ pub fn parse_options() -> CliOptions {
};

let verbose = matches.is_present("verbose");
let section_header = matches.value_of("section_header").unwrap_or_default().to_string();
let section_header = matches
.value_of("section_header")
.unwrap_or_default()
.to_string();
let section_id = matches.is_present("section_id");
if input_file.is_none() && atty::is(atty::Stream::Stdin) {
command.clone().print_help().unwrap();
Expand All @@ -64,6 +67,6 @@ pub fn parse_options() -> CliOptions {
input_file,
verbose,
section_header,
section_id
section_id,
}
}

0 comments on commit b9dacc9

Please sign in to comment.