cipherstash_dynamodb/traits/
primary_key.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#[derive(Clone)]
pub struct PrimaryKeyParts {
    pub pk: String,
    pub sk: String,
}

pub trait PrimaryKey: private::Sealed {
    type Pk;
    type Sk;

    fn into_parts(self, type_name: &str, sort_key_prefix: Option<&str>) -> PrimaryKeyParts;
}

impl PrimaryKey for Pk {
    type Pk = String;
    type Sk = ();

    fn into_parts(self, type_name: &str, _sort_key_prefix: Option<&str>) -> PrimaryKeyParts {
        PrimaryKeyParts {
            pk: self.0,
            sk: type_name.into(),
        }
    }
}

impl PrimaryKey for PkSk {
    type Pk = String;
    type Sk = String;

    fn into_parts(self, _type_name: &str, sort_key_prefix: Option<&str>) -> PrimaryKeyParts {
        PrimaryKeyParts {
            pk: self.0,
            sk: if let Some(prefix) = sort_key_prefix {
                format!("{prefix}#{}", self.1)
            } else {
                self.1
            },
        }
    }
}

pub struct Pk(pub String);

impl Pk {
    pub fn new(pk: impl Into<String>) -> Self {
        Self(pk.into())
    }
}

impl<P: Into<String>> From<P> for Pk {
    fn from(value: P) -> Self {
        Self::new(value)
    }
}

pub struct PkSk(pub String, pub String);

impl<Pk: Into<String>, Sk: Into<String>> From<(Pk, Sk)> for PkSk {
    fn from(value: (Pk, Sk)) -> Self {
        Self::new(value.0, value.1)
    }
}

impl PkSk {
    pub fn new(pk: impl Into<String>, sk: impl Into<String>) -> Self {
        Self(pk.into(), sk.into())
    }
}

mod private {
    use super::*;

    pub trait Sealed {}

    impl Sealed for Pk {}
    impl Sealed for PkSk {}
}