burrito_secrets/waiters/
sensitive_text.rs

1/*
2 * Copyright (c) 2024.
3 *
4 * Licensed under the MIT license <http://opensource.org/licenses/MIT>.
5 */
6use crate::database::{Entry, Metadata};
7use bson::doc;
8use serde::{Deserialize, Serialize};
9use std::collections::BTreeMap;
10use crate::waiters::Waiter;
11
12#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
13#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
14pub struct SensitiveText {
15    pub plaintext: String,
16    #[serde(flatten)]
17    pub additional_fields: BTreeMap<String, bson::Bson>,
18}
19
20impl SensitiveText {
21    pub fn new(plaintext: &str) -> Self {
22        Self {
23            plaintext: plaintext.to_string(),
24            additional_fields: BTreeMap::new(),
25        }
26    }
27}
28
29impl Waiter for SensitiveText {
30    fn name() -> String {
31        "sensitive_text".to_string()
32    }
33
34    fn version() -> String {
35        "0.0.0".to_string()
36    }
37    fn into_entry(self) -> Entry {
38        let entry = bson::ser::to_document(&self).unwrap();
39
40        entry.and_defaults::<Self>()
41    }
42
43    fn from_entry(entry: Entry) -> anyhow::Result<Self> {
44        Self::verify_version(&entry)?;
45
46        let entry: SensitiveText = bson::de::from_document(entry)?;
47
48        Ok(entry)
49    }
50}
51
52impl Metadata for SensitiveText {
53    fn get_meta(&self, key: &str) -> Option<&bson::Bson> {
54        self.additional_fields.get(key)
55    }
56
57    fn set_meta(&mut self, metadata: (&str, impl Serialize)) {
58        self.additional_fields.insert(metadata.0.to_string(), bson::to_bson(&metadata.1).unwrap());
59    }
60}