use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct RoleId(String);
impl RoleId {
pub fn new(role: impl Into<String>) -> Self {
RoleId(role.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl std::fmt::Display for RoleId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl From<String> for RoleId {
fn from(s: String) -> Self {
RoleId(s)
}
}
impl From<&str> for RoleId {
fn from(s: &str) -> Self {
RoleId(s.to_string())
}
}
impl AsRef<str> for RoleId {
fn as_ref(&self) -> &str {
&self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trips_through_serde_as_a_plain_string() {
let role = RoleId::new("map");
let json = serde_json::to_string(&role).unwrap();
assert_eq!(json, "\"map\"");
let back: RoleId = serde_json::from_str(&json).unwrap();
assert_eq!(role, back);
}
}