Skip to main content

nautilus_core/column/
marker.rs

1//! `ColumnMarker` — lightweight column identifier with borrowed fast paths.
2
3use std::borrow::Cow;
4
5pub(crate) fn build_column_alias(table: &str, name: &str) -> String {
6    let mut alias = String::with_capacity(table.len() + name.len() + 2);
7    alias.push_str(table);
8    alias.push_str("__");
9    alias.push_str(name);
10    alias
11}
12
13/// A lightweight column identifier for use in selection descriptors.
14///
15/// Stores either borrowed static metadata or owned runtime strings so callers
16/// can avoid allocations for generated/schema-known columns without giving up
17/// support for dynamic names.
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct ColumnMarker {
20    /// Table name.
21    pub table: Cow<'static, str>,
22    /// Column name.
23    pub name: Cow<'static, str>,
24}
25
26impl ColumnMarker {
27    /// Create a new column marker.
28    ///
29    /// Accepts any type that implements `Into<String>`, so both
30    /// `&str` literals and owned `String` values work without ceremony.
31    pub fn new(table: impl Into<String>, name: impl Into<String>) -> Self {
32        Self {
33            table: Cow::Owned(table.into()),
34            name: Cow::Owned(name.into()),
35        }
36    }
37
38    /// Create a new marker backed by borrowed static metadata.
39    pub const fn from_static(table: &'static str, name: &'static str) -> Self {
40        Self {
41            table: Cow::Borrowed(table),
42            name: Cow::Borrowed(name),
43        }
44    }
45
46    /// Returns the join-safe alias for this column.
47    ///
48    /// The alias uses the format "table__column" which is safe to use
49    /// in queries with joins, preventing column name conflicts.
50    ///
51    /// # Example
52    ///
53    /// ```
54    /// use nautilus_core::ColumnMarker;
55    ///
56    /// let marker = ColumnMarker::new("users", "id");
57    /// assert_eq!(marker.alias(), "users__id");
58    /// ```
59    pub fn alias(&self) -> String {
60        build_column_alias(&self.table, &self.name)
61    }
62}