icydb_schema/node/
primary_key.rs1use crate::prelude::*;
2
3#[derive(Clone, Debug, Serialize)]
10pub struct PrimaryKey {
11 fields: &'static [&'static str],
12 source: PrimaryKeySource,
13}
14
15impl PrimaryKey {
16 #[must_use]
18 pub const fn new(fields: &'static [&'static str], source: PrimaryKeySource) -> Self {
19 Self { fields, source }
20 }
21
22 #[must_use]
24 pub const fn fields(&self) -> &'static [&'static str] {
25 self.fields
26 }
27
28 #[must_use]
30 pub const fn field(&self) -> &'static str {
31 self.fields[0]
32 }
33
34 #[must_use]
36 pub const fn source(&self) -> PrimaryKeySource {
37 self.source
38 }
39}
40
41#[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}