Skip to content

Commit b5c129f

Browse files
committed
Remove module name from error types
1 parent 8ea2a2e commit b5c129f

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

crates/stackable-operator/src/builder/pod/container.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use snafu::{ResultExt as _, Snafu};
99

1010
use crate::{
1111
commons::product_image_selection::ResolvedProductImage,
12-
validation::{is_rfc_1123_label, ValidationErrors},
12+
validation::{self, is_rfc_1123_label},
1313
};
1414

1515
type Result<T, E = Error> = std::result::Result<T, E>;
@@ -18,7 +18,7 @@ type Result<T, E = Error> = std::result::Result<T, E>;
1818
pub enum Error {
1919
#[snafu(display("container name {container_name:?} is invalid"))]
2020
InvalidContainerName {
21-
source: ValidationErrors,
21+
source: validation::Errors,
2222
container_name: String,
2323
},
2424
}

crates/stackable-operator/src/commons/networking.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ use std::{fmt::Display, ops::Deref};
33
use schemars::JsonSchema;
44
use serde::{Deserialize, Serialize};
55

6-
use crate::validation::{self, ValidationErrors};
6+
use crate::validation;
77

88
/// A validated hostname type, for use in CRDs.
99
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
1010
#[serde(try_from = "String", into = "String")]
1111
pub struct Hostname(#[validate(regex(path = "validation::RFC_1123_SUBDOMAIN_REGEX"))] String);
1212

1313
impl TryFrom<String> for Hostname {
14-
type Error = ValidationErrors;
14+
type Error = validation::Errors;
1515

1616
fn try_from(value: String) -> Result<Self, Self::Error> {
1717
validation::is_rfc_1123_subdomain(&value)?;
@@ -44,7 +44,7 @@ pub struct KerberosRealmName(
4444
);
4545

4646
impl TryFrom<String> for KerberosRealmName {
47-
type Error = ValidationErrors;
47+
type Error = validation::Errors;
4848

4949
fn try_from(value: String) -> Result<Self, Self::Error> {
5050
validation::is_kerberos_realm_name(&value)?;

crates/stackable-operator/src/validation.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ pub(crate) static KERBEROS_REALM_NAME_REGEX: LazyLock<Regex> = LazyLock::new(||
6262
.expect("failed to compile Kerberos realm name regex")
6363
});
6464

65+
type Result<T = (), E = Errors> = std::result::Result<T, E>;
66+
67+
/// A collection of errors discovered during validation.
6568
#[derive(Debug)]
66-
pub struct ValidationErrors(Vec<ValidationError>);
69+
pub struct Errors(Vec<Error>);
6770

68-
impl Display for ValidationErrors {
71+
impl Display for Errors {
6972
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
7073
for (i, error) in self.0.iter().enumerate() {
7174
let prefix = match i {
@@ -77,10 +80,11 @@ impl Display for ValidationErrors {
7780
Ok(())
7881
}
7982
}
80-
impl std::error::Error for ValidationErrors {}
83+
impl std::error::Error for Errors {}
8184

85+
/// A single validation error.
8286
#[derive(Debug, Snafu)]
83-
pub enum ValidationError {
87+
pub enum Error {
8488
#[snafu(transparent)]
8589
Regex { source: RegexError },
8690
#[snafu(display("input is {length} bytes long but must be no more than {max_length}"))]
@@ -121,7 +125,7 @@ impl Display for RegexError {
121125
impl std::error::Error for RegexError {}
122126

123127
/// Returns [`Ok`] if `value`'s length fits within `max_length`.
124-
fn validate_str_length(value: &str, max_length: usize) -> Result<(), ValidationError> {
128+
fn validate_str_length(value: &str, max_length: usize) -> Result<(), Error> {
125129
if value.len() > max_length {
126130
TooLongSnafu {
127131
length: value.len(),
@@ -139,7 +143,7 @@ fn validate_str_regex(
139143
regex: &'static Regex,
140144
error_msg: &'static str,
141145
examples: &'static [&'static str],
142-
) -> Result<(), ValidationError> {
146+
) -> Result<(), Error> {
143147
if regex.is_match(value) {
144148
Ok(())
145149
} else {
@@ -157,22 +161,20 @@ fn validate_str_regex(
157161
}
158162

159163
/// Returns [`Ok`] if *all* validations are [`Ok`], otherwise returns all errors.
160-
fn validate_all(
161-
validations: impl IntoIterator<Item = Result<(), ValidationError>>,
162-
) -> Result<(), ValidationErrors> {
164+
fn validate_all(validations: impl IntoIterator<Item = Result<(), Error>>) -> Result {
163165
let errors = validations
164166
.into_iter()
165167
.filter_map(|res| res.err())
166168
.collect::<Vec<_>>();
167169
if errors.is_empty() {
168170
Ok(())
169171
} else {
170-
Err(ValidationErrors(errors))
172+
Err(Errors(errors))
171173
}
172174
}
173175

174176
/// Tests for a string that conforms to the definition of a subdomain in DNS (RFC 1123).
175-
pub fn is_rfc_1123_subdomain(value: &str) -> Result<(), ValidationErrors> {
177+
pub fn is_rfc_1123_subdomain(value: &str) -> Result {
176178
validate_all([
177179
validate_str_length(value, RFC_1123_SUBDOMAIN_MAX_LENGTH),
178180
validate_str_regex(
@@ -186,7 +188,7 @@ pub fn is_rfc_1123_subdomain(value: &str) -> Result<(), ValidationErrors> {
186188

187189
/// Tests for a string that conforms to the definition of a label in DNS (RFC 1123).
188190
/// Maximum label length supported by k8s is 63 characters (minimum required).
189-
pub fn is_rfc_1123_label(value: &str) -> Result<(), ValidationErrors> {
191+
pub fn is_rfc_1123_label(value: &str) -> Result {
190192
validate_all([
191193
validate_str_length(value, RFC_1123_LABEL_MAX_LENGTH),
192194
validate_str_regex(
@@ -199,7 +201,7 @@ pub fn is_rfc_1123_label(value: &str) -> Result<(), ValidationErrors> {
199201
}
200202

201203
/// Tests for a string that conforms to the definition of a label in DNS (RFC 1035).
202-
pub fn is_rfc_1035_label(value: &str) -> Result<(), ValidationErrors> {
204+
pub fn is_rfc_1035_label(value: &str) -> Result {
203205
validate_all([
204206
validate_str_length(value, RFC_1035_LABEL_MAX_LENGTH),
205207
validate_str_regex(
@@ -211,7 +213,10 @@ pub fn is_rfc_1035_label(value: &str) -> Result<(), ValidationErrors> {
211213
])
212214
}
213215

214-
pub fn is_kerberos_realm_name(value: &str) -> Result<(), ValidationErrors> {
216+
/// Tests whether a string looks like a reasonable Kerberos realm name.
217+
///
218+
/// This check is much stricter than krb5's own validation,
219+
pub fn is_kerberos_realm_name(value: &str) -> Result {
215220
validate_all([validate_str_regex(
216221
value,
217222
&KERBEROS_REALM_NAME_REGEX,
@@ -238,7 +243,7 @@ fn mask_trailing_dash(mut name: String) -> String {
238243
///
239244
/// * `name` - is the name to check for validity
240245
/// * `prefix` - indicates whether `name` is just a prefix (ending in a dash, which would otherwise not be legal at the end)
241-
pub fn name_is_dns_subdomain(name: &str, prefix: bool) -> Result<(), ValidationErrors> {
246+
pub fn name_is_dns_subdomain(name: &str, prefix: bool) -> Result {
242247
let mut name = name.to_string();
243248
if prefix {
244249
name = mask_trailing_dash(name);
@@ -254,7 +259,7 @@ pub fn name_is_dns_subdomain(name: &str, prefix: bool) -> Result<(), ValidationE
254259
///
255260
/// * `name` - is the name to check for validity
256261
/// * `prefix` - indicates whether `name` is just a prefix (ending in a dash, which would otherwise not be legal at the end)
257-
pub fn name_is_dns_label(name: &str, prefix: bool) -> Result<(), ValidationErrors> {
262+
pub fn name_is_dns_label(name: &str, prefix: bool) -> Result {
258263
let mut name = name.to_string();
259264
if prefix {
260265
name = mask_trailing_dash(name);
@@ -266,7 +271,7 @@ pub fn name_is_dns_label(name: &str, prefix: bool) -> Result<(), ValidationError
266271
/// Validates a namespace name.
267272
///
268273
/// See [`name_is_dns_label`] for more information.
269-
pub fn validate_namespace_name(name: &str, prefix: bool) -> Result<(), ValidationErrors> {
274+
pub fn validate_namespace_name(name: &str, prefix: bool) -> Result {
270275
name_is_dns_label(name, prefix)
271276
}
272277

0 commit comments

Comments
 (0)