use std::borrow::Borrow;
use std::fmt;
use std::ops::Deref;
use std::sync::Arc;
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Name(Arc<str>);
impl Name {
pub fn new(text: impl Into<Arc<str>>) -> Name {
Name(text.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn owner_prefix(&self) -> &str {
self.0.split('.').next().unwrap_or(&self.0)
}
}
impl Deref for Name {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}
impl Borrow<str> for Name {
fn borrow(&self) -> &str {
&self.0
}
}
impl AsRef<str> for Name {
fn as_ref(&self) -> &str {
&self.0
}
}
impl From<&str> for Name {
fn from(text: &str) -> Name {
Name(Arc::from(text))
}
}
impl From<String> for Name {
fn from(text: String) -> Name {
Name(Arc::from(text))
}
}
impl From<Arc<str>> for Name {
fn from(text: Arc<str>) -> Name {
Name(text)
}
}
impl From<&Name> for Name {
fn from(name: &Name) -> Name {
name.clone()
}
}
impl From<Name> for Arc<str> {
fn from(name: Name) -> Arc<str> {
name.0
}
}
impl PartialEq<str> for Name {
fn eq(&self, other: &str) -> bool {
&*self.0 == other
}
}
impl PartialEq<&str> for Name {
fn eq(&self, other: &&str) -> bool {
&*self.0 == *other
}
}
impl fmt::Display for Name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl<'de> serde::Deserialize<'de> for Name {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Name, D::Error> {
String::deserialize(deserializer).map(Name::from)
}
}
impl serde::Serialize for Name {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
impl schemars::JsonSchema for Name {
fn schema_name() -> std::borrow::Cow<'static, str> {
"String".into()
}
fn inline_schema() -> bool {
true
}
fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
String::json_schema(generator)
}
}
impl fmt::Debug for Name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", &*self.0)
}
}
macro_rules! name_newtype {
($(#[$meta:meta])* $newtype:ident) => {
$(#[$meta])*
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct $newtype(pub Name);
impl $newtype {
pub fn new(text: impl Into<Arc<str>>) -> $newtype {
$newtype(Name::new(text))
}
pub fn name(&self) -> &Name {
&self.0
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl Deref for $newtype {
type Target = Name;
fn deref(&self) -> &Name {
&self.0
}
}
impl Borrow<str> for $newtype {
fn borrow(&self) -> &str {
self.0.as_str()
}
}
impl AsRef<str> for $newtype {
fn as_ref(&self) -> &str {
self.0.as_str()
}
}
impl From<&str> for $newtype {
fn from(text: &str) -> $newtype {
$newtype(Name::from(text))
}
}
impl From<String> for $newtype {
fn from(text: String) -> $newtype {
$newtype(Name::from(text))
}
}
impl From<Arc<str>> for $newtype {
fn from(text: Arc<str>) -> $newtype {
$newtype(Name::from(text))
}
}
impl From<Name> for $newtype {
fn from(name: Name) -> $newtype {
$newtype(name)
}
}
impl From<$newtype> for Name {
fn from(newtype: $newtype) -> Name {
newtype.0
}
}
impl PartialEq<str> for $newtype {
fn eq(&self, other: &str) -> bool {
self.0.as_str() == other
}
}
impl PartialEq<&str> for $newtype {
fn eq(&self, other: &&str) -> bool {
self.0.as_str() == *other
}
}
impl<'de> serde::Deserialize<'de> for $newtype {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<$newtype, D::Error> {
String::deserialize(deserializer).map($newtype::from)
}
}
impl serde::Serialize for $newtype {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
impl schemars::JsonSchema for $newtype {
fn schema_name() -> std::borrow::Cow<'static, str> {
"String".into()
}
fn inline_schema() -> bool {
true
}
fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
String::json_schema(generator)
}
}
impl fmt::Display for $newtype {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl fmt::Debug for $newtype {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}({:?})", stringify!($newtype), self.0.as_str())
}
}
};
}
name_newtype! {
ElementName
}
name_newtype! {
RefKind
}
name_newtype! {
CapName
}
name_newtype! {
StageName
}
name_newtype! {
RuleName
}
name_newtype! {
FeatureName
}
name_newtype! {
BackendName
}
name_newtype! {
WorkflowName
}
name_newtype! {
OutputName
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeSet;
#[test]
fn names_compare_by_text() {
let a = Name::from("flm.heading");
let b = Name::new(String::from("flm.heading"));
assert_eq!(a, b);
assert_eq!(a, "flm.heading");
assert_eq!(a.owner_prefix(), "flm");
assert_eq!(Name::from("plain").owner_prefix(), "plain");
}
#[test]
fn newtypes_convert_and_order() {
let cap: CapName = "flm.refs.resolved".into();
let cap2 = CapName::from(Name::from("flm.refs.resolved"));
assert_eq!(cap, cap2);
assert_eq!(cap.as_str(), "flm.refs.resolved");
assert_eq!(&*cap, &Name::from("flm.refs.resolved"));
let set: BTreeSet<StageName> =
["b.x", "a.y"].into_iter().map(StageName::from).collect();
let ordered: Vec<&str> = set.iter().map(|n| n.as_str()).collect();
assert_eq!(ordered, ["a.y", "b.x"]);
assert_eq!(format!("{}", cap), "flm.refs.resolved");
assert_eq!(format!("{:?}", cap), "CapName(\"flm.refs.resolved\")");
}
}