Skip to main content

icydb_schema/node/
primary_key.rs

1use crate::prelude::*;
2
3///
4/// PrimaryKey
5///
6/// Structured primary-key metadata for an entity schema.
7///
8
9#[derive(Clone, Debug, Serialize)]
10pub struct PrimaryKey {
11    fields: &'static [&'static str],
12    source: PrimaryKeySource,
13}
14
15impl PrimaryKey {
16    /// Build one primary-key declaration from ordered field names and source.
17    #[must_use]
18    pub const fn new(fields: &'static [&'static str], source: PrimaryKeySource) -> Self {
19        Self { fields, source }
20    }
21
22    /// Borrow the ordered primary-key field names.
23    #[must_use]
24    pub const fn fields(&self) -> &'static [&'static str] {
25        self.fields
26    }
27
28    /// Borrow the scalar primary-key field name used by the current runtime.
29    #[must_use]
30    pub const fn field(&self) -> &'static str {
31        self.fields[0]
32    }
33
34    /// Borrow the primary-key source declaration.
35    #[must_use]
36    pub const fn source(&self) -> PrimaryKeySource {
37        self.source
38    }
39}
40
41///
42/// PrimaryKeySource
43///
44/// Declares where primary-key values originate.
45///
46
47#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize)]
48pub enum PrimaryKeySource {
49    #[default]
50    Internal,
51
52    External,
53}
54
55#[cfg(test)]
56mod tests {
57    use super::{PrimaryKey, PrimaryKeySource};
58
59    #[test]
60    fn primary_key_keeps_ordered_field_list_and_scalar_projection() {
61        let primary_key = PrimaryKey::new(&["tenant_id", "local_id"], PrimaryKeySource::External);
62
63        assert_eq!(primary_key.fields(), ["tenant_id", "local_id"]);
64        assert_eq!(primary_key.field(), "tenant_id");
65        assert_eq!(primary_key.source(), PrimaryKeySource::External);
66    }
67}