ailloli_ui_core/ids/logical_window_id.rs
1//! Stable application-window identity independent of native presentations.
2
3use std::fmt;
4
5/// Stable, provider-neutral identifier for a logical application window.
6///
7/// A logical window can outlive any native window or presentation surface used
8/// to display it. Hosts are responsible for rejecting duplicate identifiers
9/// when application windows are declared.
10///
11/// # Examples
12///
13/// ```
14/// use ailloli_ui_core::LogicalWindowId;
15/// let id = LogicalWindowId::new("main");
16/// assert_eq!(id.as_str(), "main");
17/// ```
18#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
19pub struct LogicalWindowId(String);
20
21impl LogicalWindowId {
22 /// Wraps an opaque identifier exactly as supplied.
23 ///
24 /// Empty and duplicate values are representable; the application host
25 /// rejects duplicates when it assembles declared windows.
26 ///
27 /// # Examples
28 ///
29 /// ```
30 /// use ailloli_ui_core::LogicalWindowId;
31 /// assert_eq!(LogicalWindowId::new("main").as_str(), "main");
32 /// ```
33 pub fn new(value: impl Into<String>) -> Self {
34 Self(value.into())
35 }
36
37 /// Borrows the opaque identifier without allocation.
38 ///
39 /// # Examples
40 ///
41 /// ```
42 /// use ailloli_ui_core::LogicalWindowId;
43 /// assert_eq!(LogicalWindowId::new("main").as_str(), "main");
44 /// ```
45 pub fn as_str(&self) -> &str {
46 &self.0
47 }
48
49 /// Consumes the identifier and returns its owned string.
50 ///
51 /// # Examples
52 ///
53 /// ```
54 /// use ailloli_ui_core::LogicalWindowId;
55 /// assert_eq!(LogicalWindowId::new("main").into_string(), String::from("main"));
56 /// ```
57 pub fn into_string(self) -> String {
58 self.0
59 }
60}
61
62impl fmt::Display for LogicalWindowId {
63 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
64 formatter.write_str(self.as_str())
65 }
66}
67
68impl From<String> for LogicalWindowId {
69 fn from(value: String) -> Self {
70 Self::new(value)
71 }
72}
73
74impl From<&str> for LogicalWindowId {
75 fn from(value: &str) -> Self {
76 Self::new(value)
77 }
78}
79
80impl From<&String> for LogicalWindowId {
81 fn from(value: &String) -> Self {
82 Self::new(value.clone())
83 }
84}
85
86impl AsRef<str> for LogicalWindowId {
87 fn as_ref(&self) -> &str {
88 self.as_str()
89 }
90}