Skip to content

Commit 1afadf1

Browse files
committed
refactor!: rename *Str.src() -> .to_str() to be more rusty/idiomatic
BREAKING CHANGE: All public .src() methods are now .to_str()
1 parent 3c97d9a commit 1afadf1

File tree

10 files changed

+213
-96
lines changed

10 files changed

+213
-96
lines changed

examples/parse_canonical.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ fn main() {
1313
match result {
1414
Ok(ref_str) => {
1515
let input = escape(&input);
16-
let name = escape(ref_str.name());
17-
let domain = escape(ref_str.domain());
18-
let path = escape(ref_str.path());
16+
let name = escape(ref_str.name().to_str());
17+
let domain = escape(ref_str.domain_str());
18+
let path = escape(ref_str.path_str());
1919
let tag = escape(ref_str.tag().unwrap_or(""));
20-
let digest_algo = escape(ref_str.digest().algorithm().src());
21-
let digest_encoded = escape(ref_str.digest().encoded().src());
20+
let digest_algo = escape(ref_str.digest().algorithm().to_str());
21+
let digest_encoded = escape(ref_str.digest().encoded().to_str());
2222
let err = "";
2323
println!(
2424
"{input}\t{name}\t{domain}\t{path}\t{tag}\t{digest_algo}\t{digest_encoded}\t{err}"

examples/parse_stdin.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@ fn main() {
1313
match result {
1414
Ok(ref_str) => {
1515
let input = escape(&input);
16-
let name = escape(ref_str.name());
17-
let domain = escape(ref_str.domain().unwrap_or(""));
18-
let path = escape(ref_str.path());
16+
let name = escape(ref_str.name().to_str());
17+
let domain = escape(ref_str.domain().map(|d| d.to_str()).unwrap_or(""));
18+
let path = escape(ref_str.path().to_str());
1919
let tag = escape(ref_str.tag().unwrap_or(""));
20-
let digest_algo = escape(ref_str.digest().map(|d| d.algorithm().src()).unwrap_or(""));
21-
let digest_encoded = escape(ref_str.digest().map(|d| d.encoded().src()).unwrap_or(""));
20+
let digest_algo = escape(
21+
ref_str
22+
.digest()
23+
.map(|d| d.algorithm().to_str())
24+
.unwrap_or(""),
25+
);
26+
let digest_encoded =
27+
escape(ref_str.digest().map(|d| d.encoded().to_str()).unwrap_or(""));
2228
let err = "";
2329
println!(
2430
"{input}\t{name}\t{domain}\t{path}\t{tag}\t{digest_algo}\t{digest_encoded}\t{err}"

src/ambiguous/domain_or_tagged_ref.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ use PortOrTagKind::Port;
4646
pub(crate) type Error = err::Error<u16>;
4747
/// represents a colon-delimited string of the form "left:right"
4848
pub(crate) enum DomainOrRefSpan<'src> {
49-
Domain(DomainSpan<'src>), // TODO: document why a DomainSpan can't be a PathSpan
49+
/// A span that must be a domain since either:
50+
/// - it's started by an IPv6 address
51+
/// - it's followed by a `/`
52+
Domain(DomainSpan<'src>),
53+
/// A span that must be a path since either:
54+
/// - its left side contains underscores
55+
/// - it contains a tag with non-digit characters
56+
/// - it's followed by a `@`
5057
TaggedRef((PathSpan<'src>, Option<TagSpan<'src>>)),
5158
}
5259

@@ -122,7 +129,7 @@ impl<'src> DomainOrRefSpan<'src> {
122129
}
123130
Some(b'/') => {
124131
// needs to be a name
125-
return if right.is_some() {
132+
if right.is_some() {
126133
// right must be a port, so left must be a domain
127134
DomainSpan::from_ambiguous(left, right).map(Self::Domain)
128135
} else {
@@ -149,13 +156,10 @@ impl<'src> DomainOrRefSpan<'src> {
149156
DomainSpan::from_ambiguous(left, right).map(Self::Domain)
150157
}
151158
Any => {
152-
return Err(Error::at(
153-
len.try_into().unwrap(),
154-
err::Kind::HostOrPathMissing,
155-
))
159+
Error::at(len.try_into().unwrap(), err::Kind::HostOrPathMissing).into()
156160
}
157161
}
158-
};
162+
}
159163
}
160164
_ => unreachable!(
161165
"PortOrTagSpan::new() only terminates successfully at '/', '@', or EOF"

src/digest/algorithm.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,15 @@ impl<'src> AlgorithmSpan<'src> {
8282

8383
pub struct AlgorithmStr<'src>(&'src str);
8484
impl<'src> AlgorithmStr<'src> {
85-
pub fn src(&self) -> &'src str {
85+
#[inline]
86+
pub fn to_str(&self) -> &'src str {
8687
self.0
8788
}
8889
pub fn len(&self) -> usize {
89-
self.src().len()
90+
self.to_str().len()
9091
}
9192
pub fn is_empty(&self) -> bool {
92-
self.src().is_empty()
93+
self.to_str().is_empty()
9394
}
9495
pub fn from_prefix(src: &'src str) -> Result<(Self, Compliance), Error> {
9596
let (span, compliance) = AlgorithmSpan::new(src)?;
@@ -103,10 +104,10 @@ impl<'src> AlgorithmStr<'src> {
103104
Self(span.span_of(src))
104105
}
105106
pub fn parts(&self) -> impl Iterator<Item = &str> {
106-
self.src().split(|c| is_separator(c as u8))
107+
self.to_str().split(|c| is_separator(c as u8))
107108
}
108109
pub fn compliance(&self) -> Compliance {
109-
let mut bytes = self.src().bytes();
110+
let mut bytes = self.to_str().bytes();
110111
match bytes.next().unwrap() {
111112
b'a'..=b'z' => {}
112113
b'0'..=b'9' => return Compliance::Oci,

src/digest/encoded.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl<'src> EncodedSpan<'src> {
7171

7272
pub struct EncodedStr<'src>(&'src str);
7373
impl<'src> EncodedStr<'src> {
74-
pub fn src(&self) -> &'src str {
74+
pub fn to_str(&self) -> &'src str {
7575
self.0
7676
}
7777
// no implementation of from_prefix(&str) because digests MUST terminate a
@@ -82,7 +82,7 @@ impl<'src> EncodedStr<'src> {
8282
}
8383
/// validates whether every ascii character is a lowercase hex digit
8484
fn is_lower_hex(&self) -> Result<(), Error> {
85-
self.src().bytes().enumerate().try_for_each(|(i, c)| {
85+
self.to_str().bytes().enumerate().try_for_each(|(i, c)| {
8686
if matches!(c, b'a'..=b'f' | b'0'..=b'9') {
8787
Ok(())
8888
} else {
@@ -93,10 +93,10 @@ impl<'src> EncodedStr<'src> {
9393
/// check that the encoded string is an appropriate hex length for the registered
9494
/// algorithms `sha256` and `sha512`.
9595
fn validate_registered_algorithms(&self, algorithm: &AlgorithmStr<'src>) -> Result<(), Error> {
96-
match algorithm.src() {
96+
match algorithm.to_str() {
9797
"sha256" | "sha512" => {
9898
self.is_lower_hex()?;
99-
match (algorithm.src(), self.len()) {
99+
match (algorithm.to_str(), self.len()) {
100100
("sha256", 64) => Ok(()),
101101
("sha512", 128) => Ok(()),
102102
(_, _) => Error::at(

src/digest/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,12 @@ impl<'src> DigestStr<'src> {
140140
pub fn new(src: &'src str) -> Result<Option<Self>, Error> {
141141
Ok(DigestSpan::new(src)?.map(|span| Self { src, span }))
142142
}
143+
#[inline]
143144
pub(crate) fn from_span(src: &'src str, span: DigestSpan<'src>) -> Self {
144145
Self { src, span }
145146
}
146-
pub fn src(self) -> &'src str {
147+
#[inline]
148+
pub fn to_str(self) -> &'src str {
147149
self.src
148150
}
149151
pub fn algorithm(&self) -> AlgorithmStr<'src> {
@@ -155,6 +157,7 @@ impl<'src> DigestStr<'src> {
155157
self.span.encoded,
156158
)
157159
}
160+
#[inline]
158161
pub fn compliance(&self) -> Compliance {
159162
self.span.compliance
160163
}

0 commit comments

Comments
 (0)