use std::borrow::Cow;
use std::fmt;
pub trait TestId: Copy + fmt::Debug + 'static {
fn test_id(&self) -> Cow<'static, str>;
fn rust_path(&self) -> &'static str;
fn qualified_path(&self) -> &'static str {
self.rust_path()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TestIdRef<'a> {
pub id: Cow<'a, str>,
pub rust_path: Option<&'static str>,
}
impl TestIdRef<'_> {
#[must_use]
pub fn into_static(self) -> TestIdRef<'static> {
TestIdRef {
id: Cow::Owned(self.id.into_owned()),
rust_path: self.rust_path,
}
}
}
impl fmt::Display for TestIdRef<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.id)
}
}
pub trait IntoTestId<'a> {
fn into_test_id(self) -> TestIdRef<'a>;
fn qualified_rust_path(&self) -> Option<&'static str> {
None
}
}
impl<'a> IntoTestId<'a> for &'a str {
fn into_test_id(self) -> TestIdRef<'a> {
TestIdRef {
id: Cow::Borrowed(self),
rust_path: None,
}
}
}
impl<'a> IntoTestId<'a> for &'a String {
fn into_test_id(self) -> TestIdRef<'a> {
self.as_str().into_test_id()
}
}
impl IntoTestId<'_> for String {
fn into_test_id(self) -> TestIdRef<'static> {
TestIdRef {
id: Cow::Owned(self),
rust_path: None,
}
}
}
impl<'a> IntoTestId<'a> for Cow<'a, str> {
fn into_test_id(self) -> TestIdRef<'a> {
TestIdRef {
id: self,
rust_path: None,
}
}
}
impl<'a> IntoTestId<'a> for TestIdRef<'a> {
fn into_test_id(self) -> TestIdRef<'a> {
self
}
}
impl<T: TestId> IntoTestId<'_> for T {
fn into_test_id(self) -> TestIdRef<'static> {
TestIdRef {
id: self.test_id(),
rust_path: Some(self.rust_path()),
}
}
fn qualified_rust_path(&self) -> Option<&'static str> {
Some(self.qualified_path())
}
}