use std::fmt;
use std::str::FromStr;
use std::sync::Arc;
use serde::de::{self, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TenantId(Arc<str>);
impl TenantId {
pub fn new(id: &str) -> Result<Self, TenantIdError> {
if id.is_empty() {
return Err(TenantIdError::Empty);
}
let len = id.chars().count();
if len > 64 {
return Err(TenantIdError::TooLong { len });
}
for (index, ch) in id.char_indices() {
let valid = if index == 0 {
ch.is_ascii_alphanumeric()
} else {
ch.is_ascii_alphanumeric() || matches!(ch, '_' | '-')
};
if !valid {
return Err(TenantIdError::InvalidChar { ch, index });
}
}
Ok(TenantId(Arc::from(id)))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for TenantId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl fmt::Debug for TenantId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl FromStr for TenantId {
type Err = TenantIdError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::new(s)
}
}
impl TryFrom<String> for TenantId {
type Error = TenantIdError;
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::new(&value)
}
}
impl TryFrom<&str> for TenantId {
type Error = TenantIdError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::new(value)
}
}
impl AsRef<str> for TenantId {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Serialize for TenantId {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&self.0)
}
}
impl<'de> Deserialize<'de> for TenantId {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct TenantVisitor;
impl Visitor<'_> for TenantVisitor {
type Value = TenantId;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a tenant id string")
}
fn visit_str<E: de::Error>(self, value: &str) -> Result<TenantId, E> {
TenantId::new(value).map_err(de::Error::custom)
}
}
deserializer.deserialize_str(TenantVisitor)
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum TenantIdError {
#[error("tenant id is empty")]
Empty,
#[error("tenant id is {len} characters; the limit is 64")]
TooLong {
len: usize,
},
#[error("tenant id has invalid character {ch:?} at index {index}")]
InvalidChar {
ch: char,
index: usize,
},
}
pub fn tenant_at(tenants: &[Option<TenantId>], i: usize) -> Option<&TenantId> {
if tenants.is_empty() {
None
} else {
tenants.get(i).and_then(Option::as_ref)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn valid_ids_are_accepted() {
for id in ["a", "tenant-a", "T1-prod_x", &"x".repeat(64)] {
let tenant = TenantId::new(id).unwrap();
assert_eq!(tenant.as_str(), id);
}
}
#[test]
fn empty_id_is_rejected() {
assert_eq!(TenantId::new(""), Err(TenantIdError::Empty));
}
#[test]
fn too_long_id_is_rejected() {
let err = TenantId::new(&"x".repeat(65)).unwrap_err();
assert_eq!(err, TenantIdError::TooLong { len: 65 });
}
#[test]
fn invalid_leading_characters_are_rejected() {
for id in ["-a", ".a", "_a"] {
let err = TenantId::new(id).unwrap_err();
assert!(
matches!(err, TenantIdError::InvalidChar { index: 0, .. }),
"{id}: {err:?}"
);
}
}
#[test]
fn invalid_characters_are_rejected() {
for id in ["a/b", "a b", "a{b", "ténant", "a.b"] {
let err = TenantId::new(id).unwrap_err();
assert!(
matches!(err, TenantIdError::InvalidChar { .. }),
"{id}: {err:?}"
);
}
}
#[test]
fn serde_round_trip() {
let tenant = TenantId::new("tenant-a").unwrap();
let json = serde_json::to_string(&tenant).unwrap();
assert_eq!(json, "\"tenant-a\"");
let parsed: TenantId = serde_json::from_str(&json).unwrap();
assert_eq!(parsed, tenant);
}
#[test]
fn deserialize_rejects_an_invalid_id() {
let err = serde_json::from_str::<TenantId>("\"a/b\"").unwrap_err();
assert!(err.to_string().contains("invalid character"), "{err}");
}
#[test]
fn tenant_at_reads_the_parallel_slice() {
let tenants = vec![Some(TenantId::new("a").unwrap()), None];
assert_eq!(tenant_at(&tenants, 0).map(TenantId::as_str), Some("a"));
assert!(tenant_at(&tenants, 1).is_none());
assert!(tenant_at(&tenants, 2).is_none());
assert!(tenant_at(&[], 0).is_none());
}
}