cipherstash_dynamodb/traits/
primary_key.rs

1#[derive(Clone)]
2pub struct PrimaryKeyParts {
3    pub pk: String,
4    pub sk: String,
5}
6
7pub trait PrimaryKey: private::Sealed {
8    type Pk;
9    type Sk;
10
11    fn into_parts(self, type_name: &str, sort_key_prefix: Option<&str>) -> PrimaryKeyParts;
12}
13
14impl PrimaryKey for Pk {
15    type Pk = String;
16    type Sk = ();
17
18    fn into_parts(self, type_name: &str, _sort_key_prefix: Option<&str>) -> PrimaryKeyParts {
19        PrimaryKeyParts {
20            pk: self.0,
21            sk: type_name.into(),
22        }
23    }
24}
25
26impl PrimaryKey for PkSk {
27    type Pk = String;
28    type Sk = String;
29
30    fn into_parts(self, _type_name: &str, sort_key_prefix: Option<&str>) -> PrimaryKeyParts {
31        PrimaryKeyParts {
32            pk: self.0,
33            sk: if let Some(prefix) = sort_key_prefix {
34                format!("{prefix}#{}", self.1)
35            } else {
36                self.1
37            },
38        }
39    }
40}
41
42pub struct Pk(pub String);
43
44impl Pk {
45    pub fn new(pk: impl Into<String>) -> Self {
46        Self(pk.into())
47    }
48}
49
50impl<P: Into<String>> From<P> for Pk {
51    fn from(value: P) -> Self {
52        Self::new(value)
53    }
54}
55
56pub struct PkSk(pub String, pub String);
57
58impl<Pk: Into<String>, Sk: Into<String>> From<(Pk, Sk)> for PkSk {
59    fn from(value: (Pk, Sk)) -> Self {
60        Self::new(value.0, value.1)
61    }
62}
63
64impl PkSk {
65    pub fn new(pk: impl Into<String>, sk: impl Into<String>) -> Self {
66        Self(pk.into(), sk.into())
67    }
68}
69
70mod private {
71    use super::*;
72
73    pub trait Sealed {}
74
75    impl Sealed for Pk {}
76    impl Sealed for PkSk {}
77}