Skip to content

Commit 734f6db

Browse files
committed
refactor: improve names for errors indicating missing fields
1 parent 4014648 commit 734f6db

File tree

9 files changed

+32
-34
lines changed

9 files changed

+32
-34
lines changed

src/ambiguous/domain_or_tagged_ref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fn map_err(e: err::Error<u8>) -> err::Error<u16> {
8080
impl<'src> DomainOrRefSpan<'src> {
8181
pub(crate) fn new(src: &'src str) -> Result<Self, Error> {
8282
let left = HostOrPathSpan::new(src, HostOrPathKind::Any)?
83-
.ok_or(Error::at(0, err::Kind::HostOrPathNoMatch))?;
83+
.ok_or(Error::at(0, err::Kind::HostOrPathMissing))?;
8484
let right_src = &src[left.len()..];
8585
let right = match right_src.bytes().next() {
8686
Some(b':') => {
@@ -151,7 +151,7 @@ impl<'src> DomainOrRefSpan<'src> {
151151
Any => {
152152
return Err(Error::at(
153153
len.try_into().unwrap(),
154-
err::Kind::HostOrPathNoMatch,
154+
err::Kind::HostOrPathMissing,
155155
))
156156
}
157157
}

src/digest/algorithm.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl_span_methods_on_tuple!(AlgorithmSpan, u8, NonZeroU8);
3636

3737
type Error = err::Error<u16>;
3838
use err::Kind::{
39-
AlgorithmInvalidChar, AlgorithmInvalidNumericPrefix, AlgorithmNoMatch, InvalidOciAlgorithm,
39+
AlgorithmInvalidChar, AlgorithmInvalidNumericPrefix, AlgorithmMissing, InvalidOciAlgorithm,
4040
};
4141
fn try_add(a: NonZeroU8, b: u8) -> Result<NonZeroU8, Error> {
4242
a.checked_add(b)
@@ -49,7 +49,7 @@ pub const ALGORITHM_MAX_LEN: u8 = u8::MAX;
4949
impl<'src> AlgorithmSpan<'src> {
5050
pub(crate) fn new(src: &'src str) -> Result<(Self, Compliance), Error> {
5151
let (mut len, mut compliance) =
52-
component(src, Compliance::Universal)?.ok_or(Error::at(0, AlgorithmNoMatch))?;
52+
component(src, Compliance::Universal)?.ok_or(Error::at(0, AlgorithmMissing))?;
5353
let max_len = src.len().try_into().unwrap_or(ALGORITHM_MAX_LEN);
5454
loop {
5555
if u8::from(len) >= max_len {
@@ -65,7 +65,7 @@ impl<'src> AlgorithmSpan<'src> {
6565
}
6666
let (component_len, component_compliance) =
6767
component(&src[len.as_usize()..], compliance)?
68-
.ok_or(Error::at(u8::from(len).into(), AlgorithmNoMatch))?;
68+
.ok_or(Error::at(u8::from(len).into(), AlgorithmMissing))?;
6969
len = try_add(len, component_len.into())?;
7070
compliance = component_compliance; // narrow compliance from Universal -> (Oci | Distribution)
7171
}
@@ -76,7 +76,7 @@ impl<'src> AlgorithmSpan<'src> {
7676
if span.len() == src.len() {
7777
Ok((span, compliance))
7878
} else {
79-
Error::at(span.short_len().upcast().into(), AlgorithmNoMatch).into()
79+
Error::at(span.short_len().upcast().into(), AlgorithmMissing).into()
8080
}
8181
}
8282
}

src/digest/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ impl<'src> DigestSpan<'src> {
9292
let rest = &src[len.as_usize()..];
9393
len = match rest.bytes().next() {
9494
Some(b':') => len.checked_add(1).ok_or(err::Kind::AlgorithmTooLong),
95-
None => Err(err::Kind::AlgorithmNoMatch),
95+
None => Err(err::Kind::AlgorithmMissing),
9696
_ => Err(err::Kind::AlgorithmInvalidChar),
9797
}
9898
.map_err(|kind| Error::at(len.into(), kind))?;
9999
let rest = &src[len.as_usize()..];
100100
let (encoded, compliance) = EncodedSpan::new(rest, compliance)?
101-
.ok_or(Error::at(len.into(), err::Kind::EncodedNoMatch))?;
101+
.ok_or(Error::at(len.into(), err::Kind::EncodedMissing))?;
102102

103103
{
104104
let rest = &src[len.as_usize()..];

src/domain/host.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn disambiguate_err(e: Error) -> Error {
3737
let kind = match e.kind() {
3838
err::Kind::HostOrPathInvalidChar => err::Kind::HostInvalidChar,
3939
err::Kind::HostOrPathTooLong => err::Kind::HostTooLong,
40-
err::Kind::HostOrPathNoMatch => err::Kind::HostNoMatch,
40+
err::Kind::HostOrPathMissing => err::Kind::HostMissing,
4141
_ => e.kind(),
4242
};
4343
Error::at(e.index(), kind)
@@ -140,7 +140,7 @@ impl<'src> HostStr<'src> {
140140
let result = HostSpan::new(src)?;
141141
let len = result.as_ref().map(|r| r.short_len().into()).unwrap_or(0);
142142
if (len as usize) != src.len() {
143-
return Err(Error::at(len, crate::err::Kind::HostNoMatch));
143+
return Err(Error::at(len, crate::err::Kind::HostInvalidChar));
144144
}
145145
Ok(result.map(|r| Self::from_span_of(src, r)))
146146
}

src/domain/ipv6.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<'src> Ipv6Span<'src> {
130130
let mut index: NonZeroU8 = match ascii.next() {
131131
None => return Ok(None),
132132
Some(b'[') => Ok(nonzero!(u8, 1)), // consume the opening bracket
133-
Some(_) => Err(Error::at(0, err::Kind::Ipv6NoMatch)),
133+
Some(_) => Err(Error::at(0, err::Kind::Ipv6InvalidChar)),
134134
}?;
135135
let mut state = State(0);
136136
loop {
@@ -140,7 +140,8 @@ impl<'src> Ipv6Span<'src> {
140140
b'a'..=b'f' | b'A'..=b'F' | b'0'..=b'9' => state.increment_position_in_group(),
141141
b':' => state.set_colon(),
142142
b']' => break, // done!
143-
_ => Err(err::Kind::Ipv6MissingClosingBracket),
143+
b'/' => Err(err::Kind::Ipv6MissingClosingBracket),
144+
_ => Err(err::Kind::Ipv6InvalidChar),
144145
}
145146
.map_err(|kind| Error::at(index.upcast(), kind))?;
146147
} else {

src/domain/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<'src> DomainStr<'src> {
131131
let result = DomainSpan::new(src)?;
132132
let len = result.as_ref().map(|r| r.short_len().into()).unwrap_or(0);
133133
if (len as usize) != src.len() {
134-
return Err(Error::at(len, ErrorKind::HostNoMatch));
134+
return Err(Error::at(len, ErrorKind::HostInvalidChar));
135135
}
136136
Ok(result.map(|span| Self { src, span }))
137137
}

src/err.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
// since ErrorKind can fit 256 unique errors, use it for all non-ambiguous cases
66
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
77
pub enum Kind {
8-
// FIXME: rename errors from *NoMatch to *Missing
9-
108
// ambiguous::host_or_path ---------------------------------
119
/// unable to match a host or path of length > 0. This is caused by
1210
/// attempting to parse an empty string.
13-
HostOrPathNoMatch,
11+
HostOrPathMissing,
1412
/// parsing the host or path section exceeded 255 characters.
1513
HostOrPathTooLong,
1614
HostOrPathInvalidChar,
@@ -25,12 +23,12 @@ pub enum Kind {
2523
/// the name (including host, port, and path) is over 255 characters long.
2624
NameTooLong,
2725
// name::domain::host --------------------------------------------
28-
HostNoMatch,
26+
HostMissing,
2927
HostComponentInvalidEnd,
3028
HostInvalidChar,
3129
HostTooLong,
3230
// name::domain::ipv6 -------------------------------------------
33-
Ipv6NoMatch,
31+
Ipv6InvalidChar,
3432
Ipv6TooLong,
3533
Ipv6BadColon,
3634
Ipv6TooManyHexDigits,
@@ -44,7 +42,7 @@ pub enum Kind {
4442
/// an empty port was observed (like "host:/", or "host:" at the end of the string)
4543
PortMissing,
4644
// name::path ----------------------------------------------------
47-
PathNoMatch,
45+
PathMissing,
4846
PathComponentInvalidEnd,
4947
PathInvalidChar,
5048
PathTooLong,
@@ -55,8 +53,7 @@ pub enum Kind {
5553

5654
// digest::algorithm ----------------------------------------
5755
/// 0-length algorithm in an "algorithm:encoded" section detected
58-
// TODO: deprecate in favor of Option<Algorithm>
59-
AlgorithmNoMatch,
56+
AlgorithmMissing,
6057
/// If parsing in OCI-digest mode, uppercase letters are not allowed.
6158
InvalidOciAlgorithm,
6259
/// At least one algorithm component starts with a number, which is allowed
@@ -72,7 +69,7 @@ pub enum Kind {
7269
AlgorithmTooLong,
7370
// digest::encoded ------------------------------------------
7471
/// Nothing after the ":" in an "algorithm:encoded" section.
75-
EncodedNoMatch,
72+
EncodedMissing,
7673
/// a non-base64 character was encountered.
7774
EncodedInvalidChar,
7875
///non-lower-hex characters are not allowed when parsing in `distribution/reference` mode
@@ -86,7 +83,7 @@ pub enum Kind {
8683
EncodingTooLong,
8784
// reference ----------------------------------------
8885
/// empty string or non-canonical reference
89-
RefNoMatch,
86+
RefMissing,
9087
}
9188

9289
/// The `Error` type contains an `err::Kind` and an index within the source string.

src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct RefSpan<'src> {
3737
impl<'src> RefSpan<'src> {
3838
pub fn new(src: &'src str) -> Result<Self, Error> {
3939
if src.is_empty() {
40-
return Error::at(0, err::Kind::RefNoMatch).into();
40+
return Error::at(0, err::Kind::RefMissing).into();
4141
};
4242
let prefix = DomainOrRefSpan::new(src)?;
4343
let rest = &src[prefix.len()..];
@@ -183,19 +183,19 @@ pub struct CanonicalSpan<'src> {
183183
}
184184
impl<'src> CanonicalSpan<'src> {
185185
pub fn new(src: &'src str) -> Result<Self, Error> {
186-
let domain = DomainSpan::new(src)?.ok_or(Error::at(0, err::Kind::HostNoMatch))?;
186+
let domain = DomainSpan::new(src)?.ok_or(Error::at(0, err::Kind::HostMissing))?;
187187
let mut len = domain.short_len().into();
188188
match &src[len as usize..].bytes().next() {
189189
Some(b'/') => Ok(()),
190190
Some(_) => Err(err::Kind::PathInvalidChar),
191-
None => Err(err::Kind::RefNoMatch),
191+
None => Err(err::Kind::PathMissing),
192192
}
193193
.map_err(|kind| Error::at(len, kind))?;
194194

195195
let path = PathSpan::new(&src[len as usize..])
196196
.map_err(|e| e.into())
197197
.map_err(|e: Error| e + len)?
198-
.ok_or(Error::at(len, err::Kind::PathNoMatch))?;
198+
.ok_or(Error::at(len, err::Kind::PathMissing))?;
199199
len += path.short_len().upcast() as u16;
200200
if len > NAME_TOTAL_MAX_LENGTH as u16 {
201201
return Error::at(len, err::Kind::NameTooLong).into();
@@ -207,11 +207,11 @@ impl<'src> CanonicalSpan<'src> {
207207
len += match src.as_bytes()[len as usize..].iter().next() {
208208
Some(b'@') => Ok(1),
209209
Some(_) => Error::at(len, err::Kind::PathInvalidChar).into(),
210-
None => Error::at(len, err::Kind::RefNoMatch).into(),
210+
None => Error::at(len, err::Kind::AlgorithmMissing).into(),
211211
}?;
212212
let digest = DigestSpan::new(&src[len as usize..])
213213
.map_err(|e| e + len)?
214-
.ok_or(Error::at(len, err::Kind::AlgorithmNoMatch))?;
214+
.ok_or(Error::at(len, err::Kind::AlgorithmMissing))?;
215215
Ok(Self {
216216
domain,
217217
path,
@@ -354,14 +354,14 @@ impl<'src> TryInto<CanonicalSpan<'src>> for RefSpan<'src> {
354354
type Error = Error;
355355
fn try_into(self) -> Result<CanonicalSpan<'src>, self::Error> {
356356
// a canonical reference needs a domain, path, and digest
357-
let domain = self.domain.ok_or(Error::at(0, err::Kind::HostNoMatch))?;
357+
let domain = self.domain.ok_or(Error::at(0, err::Kind::HostMissing))?;
358358
let path = self.path.ok_or(Error::at(
359359
self.path_index().try_into().unwrap(),
360-
err::Kind::PathNoMatch,
360+
err::Kind::PathMissing,
361361
))?;
362362
let digest = self.digest.ok_or(Error::at(
363363
self.digest_index().try_into().unwrap(), // safe to unwrap since host + path + tag + algorithm MUST be under u16::MAX
364-
err::Kind::AlgorithmNoMatch, // TODO: more specific error?
364+
err::Kind::AlgorithmMissing, // TODO: more specific error?
365365
))?;
366366

367367
Ok(CanonicalSpan {
@@ -564,7 +564,7 @@ mod tests {
564564
src.push_str("0@");
565565
src.push_str(&"0".repeat(255));
566566
src.push_str(":");
567-
should_fail_with(&src, Error::at(258, err::Kind::EncodedNoMatch))
567+
should_fail_with(&src, Error::at(258, err::Kind::EncodedMissing))
568568
};
569569
}
570570
#[test]

src/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type Error = err::Error<u8>;
3535
/// adapt ambiguous error kinds into path-specific error kinds
3636
fn map_error(e: Error) -> Error {
3737
let kind = match e.kind() {
38-
err::Kind::HostOrPathNoMatch => err::Kind::PathNoMatch,
38+
err::Kind::HostOrPathMissing => err::Kind::PathMissing,
3939
err::Kind::HostOrPathInvalidChar => err::Kind::PathInvalidChar,
4040
err::Kind::HostOrPathTooLong => err::Kind::PathTooLong,
4141
_ => e.kind(),

0 commit comments

Comments
 (0)