use aws_smithy_types::config_bag::{Storable, StoreReplace};
use std::borrow::Cow;
use std::error::Error;
use std::fmt;
use std::sync::atomic::{AtomicBool, Ordering};
static APP_NAME_LEN_RECOMMENDATION_WARN_EMITTED: AtomicBool = AtomicBool::new(false);
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AppName(Cow<'static, str>);
impl AsRef<str> for AppName {
fn as_ref(&self) -> &str {
&self.0
}
}
impl fmt::Display for AppName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl Storable for AppName {
type Storer = StoreReplace<AppName>;
}
impl AppName {
pub fn new(app_name: impl Into<Cow<'static, str>>) -> Result<Self, InvalidAppName> {
let app_name = app_name.into();
if app_name.is_empty() {
return Err(InvalidAppName);
}
fn valid_character(c: char) -> bool {
match c {
_ if c.is_ascii_alphanumeric() => true,
'!' | '#' | '$' | '%' | '&' | '\'' | '*' | '+' | '-' | '.' | '^' | '_' | '`'
| '|' | '~' => true,
_ => false,
}
}
if !app_name.chars().all(valid_character) {
return Err(InvalidAppName);
}
if app_name.len() > 50 {
if let Ok(false) = APP_NAME_LEN_RECOMMENDATION_WARN_EMITTED.compare_exchange(
false,
true,
Ordering::Acquire,
Ordering::Relaxed,
) {
tracing::warn!(
"The `app_name` set when configuring the SDK client is recommended \
to have no more than 50 characters."
)
}
}
Ok(Self(app_name))
}
}
#[derive(Debug)]
#[non_exhaustive]
pub struct InvalidAppName;
impl Error for InvalidAppName {}
impl fmt::Display for InvalidAppName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"The app name can only have alphanumeric characters, or any of \
'!' | '#' | '$' | '%' | '&' | '\\'' | '*' | '+' | '-' | \
'.' | '^' | '_' | '`' | '|' | '~'"
)
}
}
#[cfg(test)]
mod tests {
use super::AppName;
use crate::app_name::APP_NAME_LEN_RECOMMENDATION_WARN_EMITTED;
use std::sync::atomic::Ordering;
#[test]
fn validation() {
assert!(AppName::new("asdf1234ASDF!#$%&'*+-.^_`|~").is_ok());
assert!(AppName::new("foo bar").is_err());
assert!(AppName::new("🚀").is_err());
assert!(AppName::new("").is_err());
}
#[tracing_test::traced_test]
#[test]
fn log_warn_once() {
assert!(!APP_NAME_LEN_RECOMMENDATION_WARN_EMITTED.load(Ordering::Relaxed));
AppName::new("not-long").unwrap();
assert!(!logs_contain(
"is recommended to have no more than 50 characters"
));
assert!(!APP_NAME_LEN_RECOMMENDATION_WARN_EMITTED.load(Ordering::Relaxed));
AppName::new("greaterthanfiftycharactersgreaterthanfiftycharacters").unwrap();
assert!(logs_contain(
"is recommended to have no more than 50 characters"
));
assert!(APP_NAME_LEN_RECOMMENDATION_WARN_EMITTED.load(Ordering::Relaxed));
tracing_test::internal::global_buf().lock().unwrap().clear();
AppName::new("greaterthanfiftycharactersgreaterthanfiftycharacters").unwrap();
assert!(!logs_contain(
"is recommended to have no more than 50 characters"
));
}
}