Skip to content

Commit

Permalink
Move trim_source_for_rem to Span::trim_remainder (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten authored Sep 9, 2024
1 parent 4246154 commit 141a281
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 115 deletions.
4 changes: 2 additions & 2 deletions src/attributes/element_attribute.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{primitives::trim_source_for_rem, span::ParseResult, HasSpan, Span};
use crate::{span::ParseResult, HasSpan, Span};

/// This struct represents a single element attribute.
///
Expand Down Expand Up @@ -60,7 +60,7 @@ impl<'src> ElementAttribute<'src> {
return None;
}

let source = trim_source_for_rem(source, value.rem);
let source = source.trim_remainder(value.rem);

let shorthand_items = if name.is_none() && parse_shorthand {
parse_shorthand_items(source)
Expand Down
3 changes: 1 addition & 2 deletions src/blocks/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::slice::Iter;

use crate::{
blocks::{parse_utils::parse_blocks_until, Block, ContentModel, IsBlock},
primitives::trim_source_for_rem,
span::ParseResult,
strings::CowStr,
HasSpan, Span,
Expand All @@ -28,7 +27,7 @@ impl<'src> SectionBlock<'src> {
let source = source.discard_empty_lines();
let level = parse_title_line(source)?;
let blocks = parse_blocks_until(level.rem, |i| peer_or_ancestor_section(*i, level.t.0))?;
let source = trim_source_for_rem(source, blocks.rem);
let source = source.trim_remainder(blocks.rem);

Some(ParseResult {
t: Self {
Expand Down
4 changes: 2 additions & 2 deletions src/document/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{primitives::trim_source_for_rem, span::ParseResult, strings::CowStr, HasSpan, Span};
use crate::{span::ParseResult, strings::CowStr, HasSpan, Span};

/// Document attributes are effectively document-scoped variables for the
/// AsciiDoc language. The AsciiDoc language defines a set of built-in
Expand Down Expand Up @@ -45,7 +45,7 @@ impl<'src> Attribute<'src> {
RawAttributeValue::Value(value.rem)
};

let source = trim_source_for_rem(source, attr_line.rem);
let source = source.trim_remainder(attr_line.rem);
Some(ParseResult {
t: Self {
name: name.t,
Expand Down
6 changes: 2 additions & 4 deletions src/document/header.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::slice::Iter;

use crate::{
document::Attribute, primitives::trim_source_for_rem, span::ParseResult, HasSpan, Span,
};
use crate::{document::Attribute, span::ParseResult, HasSpan, Span};

/// An AsciiDoc document may begin with a document header. The document header
/// encapsulates the document title, author and revision information,
Expand All @@ -29,7 +27,7 @@ impl<'src> Header<'src> {
rem = attr.rem;
}

let source = trim_source_for_rem(source, rem);
let source = source.trim_remainder(rem);

// Header must be followed by an empty line or EOF.
let pr = rem.take_empty_line()?;
Expand Down
10 changes: 4 additions & 6 deletions src/inlines/inline.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::{
inlines::InlineMacro, primitives::trim_source_for_rem, span::ParseResult, HasSpan, Span,
};
use crate::{inlines::InlineMacro, span::ParseResult, HasSpan, Span};

/// An inline element is a phrase (i.e., span of content) within a block element
/// or one of its attributes in an AsciiDoc document.
Expand Down Expand Up @@ -63,7 +61,7 @@ impl<'src> Inline<'src> {
}

Some(ParseResult {
t: Self::Sequence(inlines, trim_source_for_rem(source, line.rem)),
t: Self::Sequence(inlines, source.trim_remainder(line.rem)),
rem: line.rem,
})
}
Expand All @@ -88,7 +86,7 @@ impl<'src> Inline<'src> {
rem: next,
})
} else {
let source = trim_source_for_rem(source, next);
let source = source.trim_remainder(next);
Some(ParseResult {
t: Self::Sequence(inlines, source),
rem: next,
Expand Down Expand Up @@ -131,7 +129,7 @@ fn parse_uninterpreted(source: Span<'_>) -> ParseResult<Span> {
}

ParseResult {
t: trim_source_for_rem(source, rem),
t: source.trim_remainder(rem),
rem,
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/inlines/macro.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{primitives::trim_source_for_rem, span::ParseResult, HasSpan, Span};
use crate::{span::ParseResult, HasSpan, Span};

/// An inline macro can be used in an inline context to create new inline
/// content.
Expand All @@ -24,7 +24,7 @@ impl<'src> InlineMacro<'src> {
let open_brace = target.rem.take_prefix("[")?;
let attrlist = open_brace.rem.take_while(|c| c != ']');
let close_brace = attrlist.rem.take_prefix("]")?;
let source = trim_source_for_rem(source, close_brace.rem);
let source = source.trim_remainder(close_brace.rem);

Some(ParseResult {
t: Self {
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ pub use document::Document;

pub mod inlines;

pub(crate) mod primitives;

mod span;
pub use span::{HasSpan, Span};

Expand Down
18 changes: 0 additions & 18 deletions src/primitives/mod.rs

This file was deleted.

17 changes: 17 additions & 0 deletions src/span/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,21 @@ impl<'src> Span<'src> {
// Didn't find closing delimiter.
None
}

/// Given a second [`Span`], which must be a trailing remainder of `self`,
/// return the portion of `self` that excludes the second (remainder).
///
/// Note that the trailing remainder condition is not enforced.
pub(crate) fn trim_remainder(self, rem: Span<'src>) -> Span<'src> {
// Sanity check: If rem is longer than source, we can't trim.
let rlen = rem.len();
let slen = self.len();

if rlen >= slen {
self.slice(0..0)
} else {
let trim_len = slen - rlen;
self.slice(0..trim_len)
}
}
}
1 change: 0 additions & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ mod blocks;
mod document;
pub(crate) mod fixtures;
mod inlines;
mod primitives;
mod span;
mod strings;
76 changes: 0 additions & 76 deletions src/tests/primitives/mod.rs

This file was deleted.

77 changes: 77 additions & 0 deletions src/tests/span/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,80 @@ mod take_quoted_string {
);
}
}

mod trim_remainder {
use pretty_assertions_sorted::assert_eq;

use crate::{tests::fixtures::TSpan, Span};

fn advanced_span(source: &'static str, skip: usize) -> Span<'static> {
let span = Span::new(source);
span.slice_from(skip..)
}

#[test]
fn empty_spans() {
let source = advanced_span("abcdef", 6);
let rem = Span::new("");

assert_eq!(
source.trim_remainder(rem),
TSpan {
data: "",
line: 1,
col: 7,
offset: 6
}
);
}

#[test]
fn rem_equals_source() {
let source = advanced_span("abcdef", 6);
let rem = Span::new("abcdef");

assert_eq!(
source.trim_remainder(rem),
TSpan {
data: "",
line: 1,
col: 7,
offset: 6
}
);
}

#[test]
fn rem_too_long() {
// This is nonsense input, but we should at least not panic in this case.

let source = advanced_span("abcdef", 6);
let rem = Span::new("abcdef_bogus_bogus");

assert_eq!(
source.trim_remainder(rem),
TSpan {
data: "",
line: 1,
col: 7,
offset: 6
}
);
}

#[test]
fn rem_is_subset_of_source() {
let source = advanced_span("abcdef", 2);
let rem = advanced_span("abcdef", 4);

assert_eq!(
source.trim_remainder(rem),
TSpan {
data: "cd",
line: 1,
col: 3,
offset: 2
}
);
}
}

0 comments on commit 141a281

Please sign in to comment.