Skip to content

Commit

Permalink
fix(build/parser): parse empty string args for macros as Nones (#88)
Browse files Browse the repository at this point in the history
* fix(build/parser): replace emptry string args for macos with `None`s

---------

Co-authored-by: Florian Dieminger <[email protected]>
  • Loading branch information
yin1999 and fiji-flo authored Jan 22, 2025
1 parent ad2c303 commit 4f5751f
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 42 deletions.
10 changes: 10 additions & 0 deletions crates/rari-doc/src/templ/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ fn to_arg(pair: Pair<'_, Rule>) -> Option<Arg> {
Rule::single_quoted_string => pair.into_inner().next().and_then(to_arg),
Rule::double_quoted_string => pair.into_inner().next().and_then(to_arg),
Rule::backquoted_quoted_string => pair.into_inner().next().and_then(to_arg),
Rule::empty_string => None,
Rule::sq_string => {
let s = pair.as_span().as_str();
Some(Arg::String(
Expand Down Expand Up @@ -177,4 +178,13 @@ mod test {
let p = parse(r#"dasd \\{{foo}} 200 {{bar}}"#);
println!("{:#?}", p);
}

#[test]
fn with_empty_string_arg() {
let p = parse(r#"{{foo("")}}"#);
assert!(matches!(
p.unwrap().first(),
Some(Token::Macro(macro_token)) if macro_token.args.first() == Some(&None)
));
}
}
3 changes: 2 additions & 1 deletion crates/rari-doc/src/templ/rari-templ.pest
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ string = _{

boolean = { "true" | "false" }
none = { "" }
empty_string = { "\"\"" | "''" | "``" }


all_chars = _{'a'..'z' | 'A'..'Z' | "_" | "-" | '0'..'9'}
Expand All @@ -47,7 +48,7 @@ ident = ${
all_chars*
}

arg = _{ string | float | int | boolean | none }
arg = _{ empty_string | string | float | int | boolean | none }
kwargs = !{ arg ~ ("," ~ arg )* ~ ","? }
fn_call = !{ ident ~ "(" ~ kwargs? ~ ")" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@ pub fn embed_gh_live_sample(
let mut out = String::new();
out.push_str("<iframe ");
if let Some(width) = width {
if !width.is_empty() {
write!(&mut out, r#"width="{}" "#, width)?;
}
write!(&mut out, r#"width="{}" "#, width)?;
}
if let Some(height) = height {
if !height.is_empty() {
write!(&mut out, r#"height="{}" "#, height)?;
}
write!(&mut out, r#"height="{}" "#, height)?;
}

out.extend([
Expand Down
16 changes: 6 additions & 10 deletions crates/rari-doc/src/templ/templs/embeds/embed_live_sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,14 @@ pub fn embed_live_sample(
r#"" "#
]);
if let Some(width) = width {
if !width.is_empty() {
write!(&mut out, r#"width="{}" "#, width)?;
}
write!(&mut out, r#"width="{}" "#, width)?;
}
if let Some(height) = height {
if !height.is_empty() {
// TODO: fix this
if height.as_int() < 60 {
write!(&mut out, r#"height="60" "#)?;
} else {
write!(&mut out, r#"height="{}" "#, height)?;
}
// TODO: fix this
if height.as_int() < 60 {
write!(&mut out, r#"height="60" "#)?;
} else {
write!(&mut out, r#"height="{}" "#, height)?;
}
}
out.extend([
Expand Down
10 changes: 4 additions & 6 deletions crates/rari-doc/src/templ/templs/embeds/jsfiddle_embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@ pub fn embded_jsfiddle(
let mut out = String::new();
out.push_str(r#"<p><iframe allowfullscreen="allowfullscreen" width="756" "#);
if let Some(height) = height {
if !height.is_empty() {
write!(&mut out, r#"height="{}" "#, height)?;
}
write!(&mut out, r#"height="{}" "#, height)?;
}
out.extend([
r#"src=""#,
url.as_str(),
"embedded/",
options.as_deref().unwrap_or_default(),
if options.as_ref().map(|s| s.is_empty()).unwrap_or_default() {
""
} else {
if options.as_ref().map(|s| !s.is_empty()).unwrap_or_default() {
"/"
} else {
""
},
r#""></iframe></p>"#,
]);
Expand Down
16 changes: 7 additions & 9 deletions crates/rari-doc/src/templ/templs/links/domxref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ pub fn domxref(
&api[first_char_index..],
);
if let Some(anchor) = anchor {
if !anchor.is_empty() {
if !anchor.starts_with('#') {
url.push('#');
display_with_fallback = Cow::Owned(format!("{}.{}", display_with_fallback, anchor));
}
url.push_str(&anchor);
if let Some(anchor) = anchor.strip_prefix('#') {
display_with_fallback = Cow::Owned(format!("{}.{}", display_with_fallback, anchor));
}
if !anchor.starts_with('#') {
url.push('#');
display_with_fallback = Cow::Owned(format!("{}.{}", display_with_fallback, anchor));
}
url.push_str(&anchor);
if let Some(anchor) = anchor.strip_prefix('#') {
display_with_fallback = Cow::Owned(format!("{}.{}", display_with_fallback, anchor));
}
}

Expand Down
4 changes: 1 addition & 3 deletions crates/rari-doc/src/templ/templs/links/rfc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ pub fn rfc(
content: Option<String>,
anchor: Option<AnyArg>,
) -> Result<String, DocError> {
let content = content.and_then(|c| if c.is_empty() { None } else { Some(c) });
let anchor_str = anchor.and_then(|a| if a.is_empty() { None } else { Some(a) });
let (content, anchor): (String, String) = match (content, anchor_str) {
let (content, anchor): (String, String) = match (content, anchor) {
(None, None) => Default::default(),
(None, Some(anchor)) => (
format!(
Expand Down
7 changes: 0 additions & 7 deletions crates/rari-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ pub struct AnyArg {
}

impl AnyArg {
pub fn is_empty(&self) -> bool {
if let Arg::String(s, _) = &self.value {
s.is_empty()
} else {
false
}
}
pub fn as_int(&self) -> i64 {
match &self.value {
Arg::String(s, _) => s
Expand Down

0 comments on commit 4f5751f

Please sign in to comment.