use serde::de::{self, Deserializer, Visitor};
use serde::{Deserialize, Serialize};
macro_rules! add_id {
(
$(#[$meta:meta])*
$name:ident,
$prefix:expr
) => {
#[doc = concat!("An ID of a Neos ", stringify!($name), "(`", $prefix, "{id}`)")]
#[doc = concat!("use neos::id::", stringify!($name), ";")]
#[doc = concat!("let id1 = ", stringify!($name), "::try_from(\"", $prefix, "totally-legit-id\").unwrap();")]
#[doc = concat!("let id2 = ", stringify!($name), "::try_from(\"", $prefix, "other-legit-id\").unwrap();")]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Hash)]
#[serde(transparent)]
$(#[$meta])*
pub struct $name(String);
impl AsRef<str> for $name {
#[must_use]
fn as_ref(&self) -> &str {
&self.0
}
}
impl TryFrom<String> for $name {
type Error = &'static str;
fn try_from(v: String) -> Result<Self, Self::Error> {
if !v.starts_with($prefix) {
return Err(concat!("should start with `", $prefix , "`"));
}
Ok($name(v))
}
}
impl TryFrom<&'static str> for $name {
type Error = &'static str;
fn try_from(v: &'static str) -> Result<Self, Self::Error> {
Self::try_from(v.to_owned())
}
}
impl From<$name> for String {
fn from(id: $name) -> String {
id.0
}
}
impl From<$name> for Any {
fn from(id: $name) -> Any {
Any::$name(id)
}
}
impl<'de> serde::de::Deserialize<'de> for $name {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct IdVisitor;
impl<'de> Visitor<'de> for IdVisitor {
type Value = $name;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter
.write_str(concat!("a string, ", stringify!($name), "that must start with `", $prefix, "`"))
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
$name::try_from(v.to_owned()).map_err(|_| {
de::Error::invalid_value(
serde::de::Unexpected::Str(v),
&concat!("start with `", $prefix , "`"),
)
})
}
}
deserializer.deserialize_str(IdVisitor)
}
}
};
}
add_id!(User, "U-");
add_id!(Group, "G-");
add_id!(Session, "S-");
add_id!(Record, "R-");
add_id!(Machine, "M-");
impl Session {
#[must_use]
pub fn is_custom_user(&self) -> bool { self.0.starts_with("S-U-") }
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
#[serde(untagged)]
pub enum Any {
User(User),
Group(Group),
Session(Session),
Record(Record),
Machine(Machine),
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
#[serde(untagged)]
pub enum Owner {
User(User),
Group(Group),
Machine(Machine),
}
impl From<User> for Owner {
fn from(user: User) -> Self { Self::User(user) }
}
impl From<Group> for Owner {
fn from(group: Group) -> Self { Self::Group(group) }
}