Skip to main content

mant_protocol/
selector.rs

1//! Strong wire identities for selecting nodes in projected document views.
2
3use std::{borrow::Borrow, fmt, ops::Deref};
4
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8/// Canonical structural address emitted for a node in a projected outline.
9#[derive(
10    Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
11)]
12#[serde(transparent)]
13pub struct NodePath(String);
14
15impl NodePath {
16    /// Wrap a canonical path produced by a trusted projection.
17    #[must_use]
18    pub fn new(value: impl Into<String>) -> Self {
19        Self(value.into())
20    }
21
22    /// Borrow the canonical path string.
23    #[must_use]
24    pub fn as_str(&self) -> &str {
25        &self.0
26    }
27
28    /// Consume the path and return its owned string.
29    #[must_use]
30    pub fn into_string(self) -> String {
31        self.0
32    }
33}
34
35impl From<String> for NodePath {
36    fn from(value: String) -> Self {
37        Self(value)
38    }
39}
40
41impl From<&str> for NodePath {
42    fn from(value: &str) -> Self {
43        Self(value.to_owned())
44    }
45}
46
47impl AsRef<str> for NodePath {
48    fn as_ref(&self) -> &str {
49        self.as_str()
50    }
51}
52
53impl Borrow<str> for NodePath {
54    fn borrow(&self) -> &str {
55        self.as_str()
56    }
57}
58
59impl Deref for NodePath {
60    type Target = str;
61
62    fn deref(&self) -> &Self::Target {
63        self.as_str()
64    }
65}
66
67impl fmt::Display for NodePath {
68    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
69        formatter.write_str(self.as_str())
70    }
71}
72
73impl PartialEq<str> for NodePath {
74    fn eq(&self, other: &str) -> bool {
75        self.as_str() == other
76    }
77}
78
79impl PartialEq<&str> for NodePath {
80    fn eq(&self, other: &&str) -> bool {
81        self.as_str() == *other
82    }
83}