Skip to content

Commit

Permalink
SectionBlock::parse relays warnings up the chain
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten committed Sep 20, 2024
1 parent 0960655 commit b5e66aa
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 25 deletions.
10 changes: 7 additions & 3 deletions src/blocks/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ impl<'src> Block<'src> {
}

if line.item.starts_with('=') {
if let Some(section_block) = SectionBlock::parse(source) {
if let Some(mut maw_section_block) = SectionBlock::parse(source) {
if !maw_section_block.warnings.is_empty() {
warnings.append(&mut maw_section_block.warnings);

Check warning on line 79 in src/blocks/block.rs

View check run for this annotation

Codecov / codecov/patch

src/blocks/block.rs#L79

Added line #L79 was not covered by tests
}

return MatchAndWarnings {
item: Some(MatchedItem {
item: Self::Section(section_block.item),
after: section_block.after,
item: Self::Section(maw_section_block.item.item),
after: maw_section_block.item.after,
}),
warnings,
};
Expand Down
25 changes: 13 additions & 12 deletions src/blocks/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
blocks::{parse_utils::parse_blocks_until, Block, ContentModel, IsBlock},
span::MatchedItem,
strings::CowStr,
warnings::MatchAndWarnings,
HasSpan, Span,
};

Expand All @@ -23,27 +24,27 @@ pub struct SectionBlock<'src> {
}

impl<'src> SectionBlock<'src> {
pub(crate) fn parse(source: Span<'src>) -> Option<MatchedItem<'src, Self>> {
pub(crate) fn parse(source: Span<'src>) -> Option<MatchAndWarnings<MatchedItem<'src, Self>>> {
let source = source.discard_empty_lines();
let level = parse_title_line(source)?;

let maw_blocks =
parse_blocks_until(level.after, |i| peer_or_ancestor_section(*i, level.item.0));

if !maw_blocks.warnings.is_empty() {
todo!("Propagate warnings up the chain");
}

let blocks = maw_blocks.item;
let source = source.trim_remainder(blocks.after);

Some(MatchedItem {
item: Self {
level: level.item.0,
title: level.item.1,
blocks: blocks.item,
source,
Some(MatchAndWarnings {
item: MatchedItem {
item: Self {
level: level.item.0,
title: level.item.1,
blocks: blocks.item,
source,
},
after: blocks.after,
},
after: blocks.after,
warnings: maw_blocks.warnings,
})
}

Expand Down
178 changes: 168 additions & 10 deletions src/tests/blocks/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ use pretty_assertions_sorted::assert_eq;
use crate::{
blocks::{ContentModel, IsBlock, SectionBlock},
tests::fixtures::{
blocks::{TBlock, TSectionBlock, TSimpleBlock},
attributes::{TAttrlist, TElementAttribute},
blocks::{TBlock, TMacroBlock, TSectionBlock, TSimpleBlock},
inlines::TInline,
warnings::TWarning,
TSpan,
},
warnings::WarningType,
Span,
};

Expand Down Expand Up @@ -42,7 +45,9 @@ fn err_missing_space_before_title() {

#[test]
fn simplest_section_block() {
let mi = SectionBlock::parse(Span::new("== Section Title")).unwrap();
let mi = SectionBlock::parse(Span::new("== Section Title"))
.unwrap()
.unwrap_if_no_warnings();

assert_eq!(mi.item.content_model(), ContentModel::Compound);
assert_eq!(mi.item.context().deref(), "section");
Expand Down Expand Up @@ -80,7 +85,9 @@ fn simplest_section_block() {

#[test]
fn has_child_block() {
let mi = SectionBlock::parse(Span::new("== Section Title\n\nabc")).unwrap();
let mi = SectionBlock::parse(Span::new("== Section Title\n\nabc"))
.unwrap()
.unwrap_if_no_warnings();

assert_eq!(mi.item.content_model(), ContentModel::Compound);
assert_eq!(mi.item.context().deref(), "section");
Expand Down Expand Up @@ -124,9 +131,158 @@ fn has_child_block() {
}

#[test]
fn dont_stop_at_peer_section() {
let mi =
SectionBlock::parse(Span::new("== Section Title\n\nabc\n\n=== Section 2\n\ndef")).unwrap();
fn has_child_block_with_errors() {
let maw = SectionBlock::parse(Span::new(
"== Section Title\n\nfoo::bar[alt=Sunset,width=300,,height=400]",
))
.unwrap();

let mi = maw.item.clone();

assert_eq!(mi.item.content_model(), ContentModel::Compound);
assert_eq!(mi.item.context().deref(), "section");

assert_eq!(
mi.item,
TSectionBlock {
level: 1,
title: TSpan {
data: "Section Title",
line: 1,
col: 4,
offset: 3,
},
blocks: vec![TBlock::Macro(TMacroBlock {
name: TSpan {
data: "foo",
line: 3,
col: 1,
offset: 18,
},
target: Some(TSpan {
data: "bar",
line: 3,
col: 6,
offset: 23,
}),
attrlist: TAttrlist {
attributes: vec!(
TElementAttribute {
name: Some(TSpan {
data: "alt",
line: 3,
col: 10,
offset: 27,
}),
shorthand_items: vec![],
value: TSpan {
data: "Sunset",
line: 3,
col: 14,
offset: 31,
},
source: TSpan {
data: "alt=Sunset",
line: 3,
col: 10,
offset: 27,
},
},
TElementAttribute {
name: Some(TSpan {
data: "width",
line: 3,
col: 21,
offset: 38,
}),
shorthand_items: vec![],
value: TSpan {
data: "300",
line: 3,
col: 27,
offset: 44,
},
source: TSpan {
data: "width=300",
line: 3,
col: 21,
offset: 38,
},
},
TElementAttribute {
name: Some(TSpan {
data: "height",
line: 3,
col: 32,
offset: 49,
}),
shorthand_items: vec![],
value: TSpan {
data: "400",
line: 3,
col: 39,
offset: 56,
},
source: TSpan {
data: "height=400",
line: 3,
col: 32,
offset: 49,
},
}
),
source: TSpan {
data: "alt=Sunset,width=300,,height=400",
line: 3,
col: 10,
offset: 27,
}
},
source: TSpan {
data: "foo::bar[alt=Sunset,width=300,,height=400]",
line: 3,
col: 1,
offset: 18,
},
})],
source: TSpan {
data: "== Section Title\n\nfoo::bar[alt=Sunset,width=300,,height=400]",
line: 1,
col: 1,
offset: 0,
},
}
);

assert_eq!(
mi.after,
TSpan {
data: "",
line: 3,
col: 43,
offset: 60
}
);

assert_eq!(
maw.warnings,
vec![TWarning {
source: TSpan {
data: ",",
line: 3,
col: 30,
offset: 47,
},
warning: WarningType::EmptyAttributeValue,
}]
);
}

#[test]
fn dont_stop_at_child_section() {
let mi = SectionBlock::parse(Span::new("== Section Title\n\nabc\n\n=== Section 2\n\ndef"))
.unwrap()
.unwrap_if_no_warnings();

assert_eq!(mi.item.content_model(), ContentModel::Compound);
assert_eq!(mi.item.context().deref(), "section");
Expand Down Expand Up @@ -194,8 +350,9 @@ fn dont_stop_at_peer_section() {

#[test]
fn stop_at_peer_section() {
let mi =
SectionBlock::parse(Span::new("== Section Title\n\nabc\n\n== Section 2\n\ndef")).unwrap();
let mi = SectionBlock::parse(Span::new("== Section Title\n\nabc\n\n== Section 2\n\ndef"))
.unwrap()
.unwrap_if_no_warnings();

assert_eq!(mi.item.content_model(), ContentModel::Compound);
assert_eq!(mi.item.context().deref(), "section");
Expand Down Expand Up @@ -241,8 +398,9 @@ fn stop_at_peer_section() {

#[test]
fn stop_at_ancestor_section() {
let mi =
SectionBlock::parse(Span::new("=== Section Title\n\nabc\n\n== Section 2\n\ndef")).unwrap();
let mi = SectionBlock::parse(Span::new("=== Section Title\n\nabc\n\n== Section 2\n\ndef"))
.unwrap()
.unwrap_if_no_warnings();

assert_eq!(mi.item.content_model(), ContentModel::Compound);
assert_eq!(mi.item.context().deref(), "section");
Expand Down

0 comments on commit b5e66aa

Please sign in to comment.