Skip to content

Commit 753d9ee

Browse files
committed
Fix lifetimes for as_ref() for &str wrappers
1 parent 5fb4457 commit 753d9ee

File tree

5 files changed

+48
-85
lines changed

5 files changed

+48
-85
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "beancount-parser-lima"
3-
version = "0.14.0"
3+
version = "0.14.1"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
66
description = "A zero-copy parser for Beancount"

src/parsers.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,11 +1136,7 @@ where
11361136
I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
11371137
{
11381138
let tag = select_ref!(Token::Tag(s) => *s);
1139-
tag.try_map(|s, span| {
1140-
TagOrLinkIdentifier::try_from(s)
1141-
.map(Tag)
1142-
.map_err(|e| Rich::custom(span, e.to_string()))
1143-
})
1139+
tag.try_map(|s, span| Tag::try_from(s).map_err(|e| Rich::custom(span, e.to_string())))
11441140
}
11451141

11461142
/// Matches a Link
@@ -1149,11 +1145,7 @@ where
11491145
I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
11501146
{
11511147
let link = select_ref!(Token::Link(s) => *s);
1152-
link.try_map(|s, span| {
1153-
TagOrLinkIdentifier::try_from(s)
1154-
.map(Link)
1155-
.map_err(|e| Rich::custom(span, e.to_string()))
1156-
})
1148+
link.try_map(|s, span| Link::try_from(s).map_err(|e| Rich::custom(span, e.to_string())))
11571149
}
11581150

11591151
/// Matches a Key.

src/types.rs

Lines changed: 42 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,8 +1155,8 @@ impl Display for Account<'_> {
11551155
}
11561156
}
11571157

1158-
impl AsRef<str> for Account<'_> {
1159-
fn as_ref(&self) -> &str {
1158+
impl<'a> AsRef<str> for Account<'a> {
1159+
fn as_ref(&self) -> &'a str {
11601160
self.name
11611161
}
11621162
}
@@ -1197,8 +1197,8 @@ impl ElementType for Account<'_> {
11971197
#[derive(PartialOrd, Ord, PartialEq, Eq, Hash, Copy, Clone, Debug)]
11981198
pub struct Subaccount<'a>(&'a str);
11991199

1200-
impl AsRef<str> for Subaccount<'_> {
1201-
fn as_ref(&self) -> &str {
1200+
impl<'a> AsRef<str> for Subaccount<'a> {
1201+
fn as_ref(&self) -> &'a str {
12021202
self.0
12031203
}
12041204
}
@@ -1273,8 +1273,8 @@ impl ElementType for AccountTypeName<'_> {
12731273
}
12741274
}
12751275

1276-
impl AsRef<str> for AccountTypeName<'_> {
1277-
fn as_ref(&self) -> &str {
1276+
impl<'a> AsRef<str> for AccountTypeName<'a> {
1277+
fn as_ref(&self) -> &'a str {
12781278
self.0
12791279
}
12801280
}
@@ -1348,8 +1348,8 @@ impl ElementType for AccountName<'_> {
13481348
}
13491349
}
13501350

1351-
impl AsRef<str> for AccountName<'_> {
1352-
fn as_ref(&self) -> &str {
1351+
impl<'a> AsRef<str> for AccountName<'a> {
1352+
fn as_ref(&self) -> &'a str {
13531353
self.0
13541354
}
13551355
}
@@ -1452,8 +1452,8 @@ impl ElementType for Currency<'_> {
14521452
}
14531453
}
14541454

1455-
impl AsRef<str> for Currency<'_> {
1456-
fn as_ref(&self) -> &str {
1455+
impl<'a> AsRef<str> for Currency<'a> {
1456+
fn as_ref(&self) -> &'a str {
14571457
self.0
14581458
}
14591459
}
@@ -1774,21 +1774,16 @@ impl Display for SimpleValue<'_> {
17741774
}
17751775
}
17761776

1777-
/// A tag.
1777+
/// A tag without the `#` prefix.
17781778
#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
1779-
pub struct Tag<'a>(pub(crate) TagOrLinkIdentifier<'a>);
1780-
1781-
impl<'a> From<TagOrLinkIdentifier<'a>> for Tag<'a> {
1782-
fn from(id: TagOrLinkIdentifier<'a>) -> Self {
1783-
Self(id)
1784-
}
1785-
}
1779+
pub struct Tag<'a>(pub(crate) &'a str);
17861780

17871781
impl<'a> TryFrom<&'a str> for Tag<'a> {
17881782
type Error = TagOrLinkIdentifierError;
17891783

17901784
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
1791-
TagOrLinkIdentifier::try_from(s).map(Tag)
1785+
validate_tag_or_link_identifier(s)?;
1786+
Ok(Tag(s))
17921787
}
17931788
}
17941789

@@ -1798,9 +1793,9 @@ impl ElementType for Tag<'_> {
17981793
}
17991794
}
18001795

1801-
impl AsRef<str> for Tag<'_> {
1802-
fn as_ref(&self) -> &str {
1803-
self.0.as_ref()
1796+
impl<'a> AsRef<str> for Tag<'a> {
1797+
fn as_ref(&self) -> &'a str {
1798+
self.0
18041799
}
18051800
}
18061801

@@ -1812,25 +1807,23 @@ impl PartialEq<&str> for Tag<'_> {
18121807

18131808
impl Display for Tag<'_> {
18141809
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1815-
write!(f, "#{}", self.0 .0)
1810+
write!(f, "#{}", self.0)
18161811
}
18171812
}
18181813

1819-
/// A link.
1820-
#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
1821-
pub struct Link<'a>(pub(crate) TagOrLinkIdentifier<'a>);
1814+
/// A link without the `^` prefix.
1815+
#[derive(PartialOrd, Ord, PartialEq, Eq, Hash, Copy, Clone, Debug)]
1816+
pub struct TagOrLinkIdentifier<'a>(&'a str);
18221817

1823-
impl<'a> From<TagOrLinkIdentifier<'a>> for Link<'a> {
1824-
fn from(id: TagOrLinkIdentifier<'a>) -> Self {
1825-
Self(id)
1826-
}
1827-
}
1818+
#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
1819+
pub struct Link<'a>(pub(crate) &'a str);
18281820

18291821
impl<'a> TryFrom<&'a str> for Link<'a> {
18301822
type Error = TagOrLinkIdentifierError;
18311823

18321824
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
1833-
TagOrLinkIdentifier::try_from(s).map(Link)
1825+
validate_tag_or_link_identifier(s)?;
1826+
Ok(Link(s))
18341827
}
18351828
}
18361829

@@ -1848,27 +1841,21 @@ impl PartialEq<&str> for Link<'_> {
18481841

18491842
impl Display for Link<'_> {
18501843
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1851-
write!(f, "^{}", self.0 .0)
1844+
write!(f, "^{}", self.0)
18521845
}
18531846
}
18541847

1855-
impl AsRef<str> for Link<'_> {
1856-
fn as_ref(&self) -> &str {
1857-
self.0.as_ref()
1848+
impl<'a> AsRef<str> for Link<'a> {
1849+
fn as_ref(&self) -> &'a str {
1850+
self.0
18581851
}
18591852
}
18601853

1861-
/// The validated identifier part of a `Tag` or `Link` without the `#` or `^` prefix.
1862-
#[derive(PartialOrd, Ord, PartialEq, Eq, Hash, Copy, Clone, Debug)]
1863-
pub struct TagOrLinkIdentifier<'a>(&'a str);
1864-
18651854
/// The valid characters for tags and links besides alphanumeric.
18661855
const TAG_OR_LINK_EXTRA_CHARS: [char; 4] = ['-', '_', '/', '.'];
18671856

1868-
impl TagOrLinkIdentifier<'_> {
1869-
pub(crate) fn is_valid_char(c: &char) -> bool {
1870-
c.is_alphanumeric() || TAG_OR_LINK_EXTRA_CHARS.contains(c)
1871-
}
1857+
fn is_valid_tag_or_link_identifier_char(c: &char) -> bool {
1858+
c.is_alphanumeric() || TAG_OR_LINK_EXTRA_CHARS.contains(c)
18721859
}
18731860

18741861
/// Error type for [TagOrLinkIdentifier] creation.
@@ -1890,31 +1877,15 @@ impl Display for TagOrLinkIdentifierError {
18901877

18911878
impl std::error::Error for TagOrLinkIdentifierError {}
18921879

1893-
impl<'a> TryFrom<&'a str> for TagOrLinkIdentifier<'a> {
1894-
type Error = TagOrLinkIdentifierError;
1895-
1896-
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
1897-
let bad_chars = s
1898-
.chars()
1899-
.filter(|c| !TagOrLinkIdentifier::is_valid_char(c))
1900-
.collect::<Vec<char>>();
1901-
if bad_chars.is_empty() {
1902-
Ok(TagOrLinkIdentifier(s))
1903-
} else {
1904-
Err(TagOrLinkIdentifierError(bad_chars))
1905-
}
1906-
}
1907-
}
1908-
1909-
impl AsRef<str> for TagOrLinkIdentifier<'_> {
1910-
fn as_ref(&self) -> &str {
1911-
self.0
1912-
}
1913-
}
1914-
1915-
impl PartialEq<&str> for TagOrLinkIdentifier<'_> {
1916-
fn eq(&self, other: &&str) -> bool {
1917-
self.0 == *other
1880+
fn validate_tag_or_link_identifier(s: &str) -> Result<(), TagOrLinkIdentifierError> {
1881+
let bad_chars = s
1882+
.chars()
1883+
.filter(|c| !is_valid_tag_or_link_identifier_char(c))
1884+
.collect::<Vec<char>>();
1885+
if bad_chars.is_empty() {
1886+
Ok(())
1887+
} else {
1888+
Err(TagOrLinkIdentifierError(bad_chars))
19181889
}
19191890
}
19201891

@@ -1932,8 +1903,8 @@ impl Key<'_> {
19321903
}
19331904
}
19341905

1935-
impl AsRef<str> for Key<'_> {
1936-
fn as_ref(&self) -> &str {
1906+
impl<'a> AsRef<str> for Key<'a> {
1907+
fn as_ref(&self) -> &'a str {
19371908
self.0
19381909
}
19391910
}

src/types/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ fn test_tag_or_link_identifier_try_from(
8383
s: &str,
8484
expected_raw: Result<&str, TagOrLinkIdentifierError>,
8585
) {
86-
let result = TagOrLinkIdentifier::try_from(s);
86+
let result = Tag::try_from(s);
8787
let expected = match expected_raw {
88-
Ok(s) => Ok(TagOrLinkIdentifier(s)),
88+
Ok(s) => Ok(Tag(s)),
8989
Err(e) => Err(e),
9090
};
9191
// visually check the error display by making a bad test case

0 commit comments

Comments
 (0)