Skip to content

Commit

Permalink
Attrlist::parse never returned None, so it doesn't need to return…
Browse files Browse the repository at this point in the history
… `Option`
  • Loading branch information
scouten committed Sep 16, 2024
1 parent 65556f5 commit 439cfab
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 57 deletions.
8 changes: 3 additions & 5 deletions src/attributes/attrlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ impl<'src> Attrlist<'src> {
/// the opening or closing square brackets for the attrlist. This is because
/// the rules for closing brackets differ when parsing inline, macro, and
/// block elements.
pub(crate) fn parse(
source: Span<'src>,
) -> MatchAndWarnings<'src, Option<MatchedItem<'src, Self>>> {
pub(crate) fn parse(source: Span<'src>) -> MatchAndWarnings<'src, MatchedItem<'src, Self>> {
let mut after = source;
let mut attributes: Vec<ElementAttribute> = vec![];
let mut parse_shorthand_items = true;
Expand Down Expand Up @@ -88,10 +86,10 @@ impl<'src> Attrlist<'src> {
}

MatchAndWarnings {
item: Some(MatchedItem {
item: MatchedItem {
item: Self { attributes, source },
after,
}),
},
warnings,
}
}
Expand Down
38 changes: 16 additions & 22 deletions src/blocks/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,24 @@ impl<'src> MacroBlock<'src> {
let attrlist = open_brace.after.slice(0..open_brace.after.len() - 1);
// Note that we already checked that this line ends with a close brace.

let maw_attrlist = Attrlist::parse(attrlist);

match maw_attrlist.item {
Some(attrlist) => MatchAndWarnings {
item: Some(MatchedItem {
item: Self {
name: name.item,
target: if target.item.is_empty() {
None
} else {
Some(target.item)
},
attrlist: attrlist.item,
source: line.item,
let attrlist = Attrlist::parse(attrlist);

MatchAndWarnings {
item: Some(MatchedItem {
item: Self {
name: name.item,
target: if target.item.is_empty() {
None
} else {
Some(target.item)
},
attrlist: attrlist.item.item,
source: line.item,
},

after: line.after.discard_empty_lines(),
}),
warnings: maw_attrlist.warnings,
},
None => MatchAndWarnings {
item: None,
warnings: maw_attrlist.warnings,
},
after: line.after.discard_empty_lines(),
}),
warnings: attrlist.warnings,
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/tests/asciidoc_lang/attributes/element_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ mod attrlist {
const ATTRLIST_EXAMPLE: &str =
r#"first-positional,second-positional,named="value of named""#;

let mi = Attrlist::parse(Span::new(ATTRLIST_EXAMPLE))
.unwrap_if_no_warnings()
.unwrap();
let mi = Attrlist::parse(Span::new(ATTRLIST_EXAMPLE)).unwrap_if_no_warnings();

assert_eq!(
mi.item,
Expand Down
38 changes: 11 additions & 27 deletions src/tests/attributes/attrlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,14 @@ use crate::{
#[test]
fn impl_clone() {
// Silly test to mark the #[derive(...)] line as covered.
let b1 = Attrlist::parse(Span::new("abc"))
.unwrap_if_no_warnings()
.unwrap();
let b1 = Attrlist::parse(Span::new("abc")).unwrap_if_no_warnings();
let b2 = b1.item.clone();
assert_eq!(b1.item, b2);
}

#[test]
fn empty_source() {
let mi = Attrlist::parse(Span::new(""))
.unwrap_if_no_warnings()
.unwrap();
let mi = Attrlist::parse(Span::new("")).unwrap_if_no_warnings();

assert_eq!(
mi.item,
Expand Down Expand Up @@ -75,9 +71,7 @@ fn empty_source() {

#[test]
fn only_positional_attributes() {
let mi = Attrlist::parse(Span::new("Sunset,300,400"))
.unwrap_if_no_warnings()
.unwrap();
let mi = Attrlist::parse(Span::new("Sunset,300,400")).unwrap_if_no_warnings();

assert_eq!(
mi.item,
Expand Down Expand Up @@ -311,9 +305,7 @@ fn only_positional_attributes() {

#[test]
fn only_named_attributes() {
let mi = Attrlist::parse(Span::new("alt=Sunset,width=300,height=400"))
.unwrap_if_no_warnings()
.unwrap();
let mi = Attrlist::parse(Span::new("alt=Sunset,width=300,height=400")).unwrap_if_no_warnings();

assert_eq!(
mi.item,
Expand Down Expand Up @@ -579,7 +571,7 @@ fn only_named_attributes() {
fn err_unparsed_remainder_after_value() {
let maw = Attrlist::parse(Span::new("alt=\"Sunset\"width=300"));

let mi = maw.item.as_ref().unwrap().clone();
let mi = maw.item.clone();

assert_eq!(
mi.item,
Expand Down Expand Up @@ -642,7 +634,7 @@ fn err_unparsed_remainder_after_value() {
fn propagates_error_from_element_attribute() {
let maw = Attrlist::parse(Span::new("foo%#id"));

let mi = maw.item.as_ref().unwrap().clone();
let mi = maw.item.clone();

assert_eq!(
mi.item,
Expand Down Expand Up @@ -723,9 +715,7 @@ mod id {

#[test]
fn via_shorthand_syntax() {
let mi = Attrlist::parse(Span::new("#goals"))
.unwrap_if_no_warnings()
.unwrap();
let mi = Attrlist::parse(Span::new("#goals")).unwrap_if_no_warnings();

assert_eq!(
mi.item,
Expand Down Expand Up @@ -796,9 +786,7 @@ mod id {

#[test]
fn via_named_attribute() {
let mi = Attrlist::parse(Span::new("foo=bar,id=goals"))
.unwrap_if_no_warnings()
.unwrap();
let mi = Attrlist::parse(Span::new("foo=bar,id=goals")).unwrap_if_no_warnings();

assert_eq!(
mi.item,
Expand Down Expand Up @@ -930,18 +918,14 @@ mod id {
#[test]
#[should_panic]
fn via_block_anchor_syntax() {
let _pr = Attrlist::parse(Span::new("[goals]"))
.unwrap_if_no_warnings()
.unwrap();
let _pr = Attrlist::parse(Span::new("[goals]")).unwrap_if_no_warnings();

// TO DO (#122): Parse block anchor syntax
}

#[test]
fn shorthand_only_first_attribute() {
let mi = Attrlist::parse(Span::new("foo,blah#goals"))
.unwrap_if_no_warnings()
.unwrap();
let mi = Attrlist::parse(Span::new("foo,blah#goals")).unwrap_if_no_warnings();

assert_eq!(
mi.item,
Expand Down Expand Up @@ -1012,7 +996,7 @@ mod id {
fn err_double_comma() {
let maw = Attrlist::parse(Span::new("alt=Sunset,width=300,,height=400"));

let mi = maw.item.unwrap().clone();
let mi = maw.item.clone();

assert_eq!(
mi.item,
Expand Down

0 comments on commit 439cfab

Please sign in to comment.