icedwright-probe 0.1.4

What an iced app links for icedwright: semantic annotations, typed test ids and named tasks, all pass-through unless a feature turns them on
Documentation
//! Typed test ids.

use std::borrow::Cow;
use std::fmt;

/// A test id the view and the tests name with the same value, so a typo does not compile.
///
/// Derive it on an enum with `#[derive(TestId)]`: unit variants become their name in kebab
/// case, and `#[test_id("delete-{0}")]` sets a string or a format string over the fields.
pub trait TestId: Copy + fmt::Debug + 'static {
    /// The string written into the tree.
    fn test_id(&self) -> Cow<'static, str>;

    /// The Rust path of the variant, such as `Ids::Delete`, for error messages and the
    /// recorder's output.
    fn rust_path(&self) -> &'static str;

    /// The variant's path from its crate root, such as `todo::ui::Ids::Delete`, which the
    /// raw tree reports so the recorder can name the variant from a test. The derive builds it
    /// with `module_path!()`; an impl written by hand gives [`rust_path`](TestId::rust_path).
    fn qualified_path(&self) -> &'static str {
        self.rust_path()
    }
}

/// A test id as the tree and the locators carry it.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TestIdRef<'a> {
    /// The string.
    pub id: Cow<'a, str>,
    /// The Rust path of the variant it came from, when it came from a [`TestId`].
    pub rust_path: Option<&'static str>,
}

impl TestIdRef<'_> {
    /// The same id with its string owned.
    #[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)
    }
}

/// What `.test_id(..)`, [`test_id`](crate::test_id) and `get_by_test_id(..)` accept: a
/// [`TestId`] value, or a string for an id built at run time.
pub trait IntoTestId<'a> {
    /// The id as the tree carries it.
    fn into_test_id(self) -> TestIdRef<'a>;

    /// The [`TestId::qualified_path`] of a typed id; `None` for a string.
    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())
    }
}