use crate::types::report::MustBeRelative;
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum InvalidAirslReq {
#[error("\"{text}\" is not a version range")]
NotVersionRange {
text: String,
},
#[error("\"{text}\" uses an operator this agentstacks does not support")]
UnsupportedOperator {
text: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum InvalidExcludeGlob {
#[error(transparent)]
MustBeRelative(#[from] MustBeRelative),
#[error("\"{text}\" is not a valid glob")]
NotAGlob {
text: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("\"{0}\" (expected boolean, integer, string)")]
pub struct InvalidOptionType(String);
impl InvalidOptionType {
pub(crate) fn new(value: &str) -> Self {
Self(value.to_owned())
}
#[must_use]
pub fn value(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum InvalidPackPath {
#[error("\"\" must not be empty")]
Empty,
#[error("\".\" must name a file inside the pack")]
IsDot,
#[error(transparent)]
MustBeRelative(#[from] MustBeRelative),
#[error("\"{text}\" must use / as the separator")]
Backslash {
text: String,
},
#[error("\"{text}\" must stay inside the pack")]
MustStayInsidePack {
text: String,
},
#[error("\"{text}\" must not have a . segment")]
DotSegment {
text: String,
},
#[error("\"{text}\" must not end with /")]
TrailingSlash {
text: String,
},
#[error("\"{text}\" must not have an empty segment")]
EmptySegment {
text: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("\"{0}\" is not semver")]
pub struct InvalidVersion(String);
impl InvalidVersion {
pub(crate) fn new(value: &str) -> Self {
Self(value.to_owned())
}
#[must_use]
pub fn value(&self) -> &str {
&self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn invalid_airsl_req_displays_each_message() {
let cases = [
(
InvalidAirslReq::NotVersionRange {
text: "nope".to_owned(),
},
"\"nope\" is not a version range",
),
(
InvalidAirslReq::UnsupportedOperator {
text: ">=0.1".to_owned(),
},
"\">=0.1\" uses an operator this agentstacks does not support",
),
];
for (error, message) in cases {
assert_eq!(error.to_string(), message);
}
}
#[test]
fn invalid_exclude_glob_displays_each_message() {
let cases = [
(
InvalidExcludeGlob::from(MustBeRelative::new("/x")),
"\"/x\" must be relative",
),
(
InvalidExcludeGlob::NotAGlob {
text: "[".to_owned(),
},
"\"[\" is not a valid glob",
),
];
for (error, message) in cases {
assert_eq!(error.to_string(), message);
}
}
#[test]
fn invalid_option_type_names_the_allowed_set() {
let error = InvalidOptionType::new("number");
assert_eq!(
error.to_string(),
"\"number\" (expected boolean, integer, string)"
);
assert_eq!(error.value(), "number");
}
#[test]
fn invalid_pack_path_displays_each_message() {
let text = || "p".to_owned();
let cases = [
(InvalidPackPath::Empty, "\"\" must not be empty"),
(
InvalidPackPath::IsDot,
"\".\" must name a file inside the pack",
),
(
InvalidPackPath::from(MustBeRelative::new(text())),
"\"p\" must be relative",
),
(
InvalidPackPath::Backslash { text: text() },
"\"p\" must use / as the separator",
),
(
InvalidPackPath::MustStayInsidePack { text: text() },
"\"p\" must stay inside the pack",
),
(
InvalidPackPath::DotSegment { text: text() },
"\"p\" must not have a . segment",
),
(
InvalidPackPath::TrailingSlash { text: text() },
"\"p\" must not end with /",
),
(
InvalidPackPath::EmptySegment { text: text() },
"\"p\" must not have an empty segment",
),
];
for (error, message) in cases {
assert_eq!(error.to_string(), message);
}
}
#[test]
fn invalid_version_display_quotes_the_value() {
assert_eq!(
InvalidVersion::new("1.2").to_string(),
"\"1.2\" is not semver"
);
}
#[test]
fn value_returns_the_refused_text() {
assert_eq!(InvalidVersion::new("1.2").value(), "1.2");
}
}