use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InvalidServiceValue {
kind: &'static str,
reason: &'static str,
value: String,
}
impl InvalidServiceValue {
fn new(kind: &'static str, reason: &'static str, value: &str) -> Self {
Self {
kind,
reason,
value: value.to_owned(),
}
}
}
impl fmt::Display for InvalidServiceValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"invalid {}: {} ({:?})",
self.kind, self.reason, self.value
)
}
}
impl std::error::Error for InvalidServiceValue {}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ServiceAccountName(String);
impl ServiceAccountName {
pub fn try_new(name: impl Into<String>) -> Result<Self, InvalidServiceValue> {
const KIND: &str = "service-account name";
let name = name.into();
if name.is_empty() {
return Err(InvalidServiceValue::new(KIND, "must not be empty", &name));
}
if name.len() > 32 {
return Err(InvalidServiceValue::new(
KIND,
"longer than 32 bytes",
&name,
));
}
if !name.bytes().all(|b| b.is_ascii_graphic()) {
return Err(InvalidServiceValue::new(
KIND,
"must be ASCII graphic characters",
&name,
));
}
if name.contains([':', '/']) {
return Err(InvalidServiceValue::new(
KIND,
"must not contain ':' or '/'",
&name,
));
}
Ok(Self(name))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EnvVarName(String);
impl EnvVarName {
pub fn try_new(name: impl Into<String>) -> Result<Self, InvalidServiceValue> {
const KIND: &str = "environment-variable name";
let name = name.into();
if name.is_empty() {
return Err(InvalidServiceValue::new(KIND, "must not be empty", &name));
}
if name.len() > 128 {
return Err(InvalidServiceValue::new(
KIND,
"longer than 128 bytes",
&name,
));
}
if !name.bytes().all(|b| b.is_ascii_graphic()) {
return Err(InvalidServiceValue::new(
KIND,
"must be ASCII graphic characters",
&name,
));
}
if name.contains('=') {
return Err(InvalidServiceValue::new(
KIND,
"must not contain '='",
&name,
));
}
Ok(Self(name))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BinaryName(String);
impl BinaryName {
pub fn try_new(name: impl Into<String>) -> Result<Self, InvalidServiceValue> {
const KIND: &str = "binary name";
let name = name.into();
if name.is_empty() {
return Err(InvalidServiceValue::new(KIND, "must not be empty", &name));
}
if name.len() > 64 {
return Err(InvalidServiceValue::new(
KIND,
"longer than 64 bytes",
&name,
));
}
if !name.bytes().all(|b| b.is_ascii_graphic()) {
return Err(InvalidServiceValue::new(
KIND,
"must be ASCII graphic characters",
&name,
));
}
if name.contains(['/', '\\', '@']) {
return Err(InvalidServiceValue::new(
KIND,
"must not contain '/', '\\', or '@'",
&name,
));
}
Ok(Self(name))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct LaunchdLabelPrefix(String);
impl LaunchdLabelPrefix {
pub fn try_new(prefix: impl Into<String>) -> Result<Self, InvalidServiceValue> {
const KIND: &str = "launchd label prefix";
let prefix = prefix.into();
if prefix.is_empty() {
return Err(InvalidServiceValue::new(KIND, "must not be empty", &prefix));
}
if prefix.len() > 128 {
return Err(InvalidServiceValue::new(
KIND,
"longer than 128 bytes",
&prefix,
));
}
let valid_segments = prefix.split('.').all(|segment| {
!segment.is_empty()
&& segment
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
});
if !valid_segments {
return Err(InvalidServiceValue::new(
KIND,
"must be dot-separated segments of ASCII alphanumerics, '-', or '_'",
&prefix,
));
}
Ok(Self(prefix))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ServiceInstance(String);
impl ServiceInstance {
pub fn try_new(instance: impl Into<String>) -> Result<Self, InvalidServiceValue> {
const KIND: &str = "service instance";
let instance = instance.into();
if instance.is_empty() {
return Err(InvalidServiceValue::new(
KIND,
"must not be empty",
&instance,
));
}
if instance.len() > 128 {
return Err(InvalidServiceValue::new(
KIND,
"longer than 128 bytes",
&instance,
));
}
if !instance.bytes().all(|b| b.is_ascii_graphic()) {
return Err(InvalidServiceValue::new(
KIND,
"must be ASCII graphic characters",
&instance,
));
}
if instance.contains(['/', '\\', '@']) {
return Err(InvalidServiceValue::new(
KIND,
"must not contain '/', '\\', or '@'",
&instance,
));
}
Ok(Self(instance))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<u16> for ServiceInstance {
fn from(port: u16) -> Self {
Self(port.to_string())
}
}
macro_rules! string_value_traits {
($($ty:ident),+) => {$(
impl fmt::Display for $ty {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl std::str::FromStr for $ty {
type Err = InvalidServiceValue;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::try_new(s)
}
}
impl AsRef<str> for $ty {
fn as_ref(&self) -> &str {
&self.0
}
}
)+};
}
string_value_traits!(
ServiceAccountName,
EnvVarName,
BinaryName,
LaunchdLabelPrefix,
ServiceInstance
);