Skip to main content

yew_datatable_core/column/
column_id.rs

1//! Unique identifier for a table column.
2//!
3//! Column IDs are used to reference columns throughout the table API
4//! for operations like sorting, filtering, visibility, and pinning.
5
6use std::fmt;
7
8/// Unique identifier for a column.
9///
10/// Column IDs are used to reference columns throughout the table API
11/// for operations like sorting, filtering, visibility, and pinning.
12#[derive(Clone, PartialEq, Eq, Hash)]
13pub struct ColumnId(String);
14
15impl ColumnId {
16    /// Creates a new column ID from a string.
17    ///
18    /// # Parameters
19    ///
20    /// - `id`: The column identifier string.
21    ///
22    /// # Returns
23    ///
24    /// - `ColumnId`: A new column identifier.
25    pub fn new(id: impl Into<String>) -> Self {
26        // Convert the input into a string and wrap it.
27        Self(id.into())
28    }
29
30    /// Returns the column ID as a string slice.
31    ///
32    /// # Returns
33    ///
34    /// - `&str`: The column identifier as a string slice.
35    pub fn as_str(&self) -> &str {
36        &self.0
37    }
38}
39
40/// Formats the column identifier for debug output.
41impl fmt::Debug for ColumnId {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        // Format the column ID with a descriptive wrapper.
44        write!(f, "ColumnId({})", self.0)
45    }
46}
47
48/// Displays the column identifier as a plain string.
49impl fmt::Display for ColumnId {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        // Display the raw column ID string.
52        write!(f, "{}", self.0)
53    }
54}
55
56/// Creates a `ColumnId` from a string slice.
57impl From<&str> for ColumnId {
58    fn from(s: &str) -> Self {
59        Self::new(s)
60    }
61}
62
63/// Creates a `ColumnId` from an owned `String`.
64impl From<String> for ColumnId {
65    fn from(s: String) -> Self {
66        Self::new(s)
67    }
68}
69
70/// Provides a string slice reference to the inner identifier.
71impl AsRef<str> for ColumnId {
72    fn as_ref(&self) -> &str {
73        &self.0
74    }
75}
76
77/// Serializes the column identifier as a plain string.
78#[cfg(feature = "serde")]
79impl serde::Serialize for ColumnId {
80    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
81    where
82        S: serde::Serializer,
83    {
84        // Serialize the inner string.
85        self.0.serialize(serializer)
86    }
87}
88
89/// Deserializes a column identifier from a string value.
90#[cfg(feature = "serde")]
91impl<'de> serde::Deserialize<'de> for ColumnId {
92    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
93    where
94        D: serde::Deserializer<'de>,
95    {
96        // Deserialize a string and wrap it as a ColumnId.
97        String::deserialize(deserializer).map(Self::new)
98    }
99}