mithril_signer/database/record/
protocol_initializer_record.rs

1use chrono::{DateTime, Utc};
2
3use mithril_common::{crypto_helper::ProtocolInitializer, entities::Epoch};
4use mithril_persistence::sqlite::{HydrationError, Projection, SqLiteEntity};
5
6/// Protocol initializer.
7#[derive(Debug)]
8pub struct ProtocolInitializerRecord {
9    /// Epoch
10    pub epoch: Epoch,
11
12    /// Protocol Initializer
13    pub protocol_initializer: ProtocolInitializer,
14
15    /// DateTime of the record creation.
16    pub created_at: DateTime<Utc>,
17}
18
19impl SqLiteEntity for ProtocolInitializerRecord {
20    fn hydrate(row: sqlite::Row) -> Result<Self, HydrationError>
21    where
22        Self: Sized,
23    {
24        let epoch_int = row.read::<i64, _>(0);
25        let protocol = row.read::<&str, _>(1);
26        let datetime = &row.read::<&str, _>(2);
27
28        let record = Self {
29            protocol_initializer: serde_json::from_str(protocol).map_err(|e| {
30                HydrationError::InvalidData(format!(
31                    "Could not cast string ({}) to ProtocolInitializer. Error: '{e}'",
32                    protocol
33                ))
34            })?,
35            epoch: Epoch(epoch_int.try_into().map_err(|e| {
36                HydrationError::InvalidData(format!(
37                    "Could not cast i64 ({epoch_int}) to u64. Error: '{e}'"
38                ))
39            })?),
40            created_at: DateTime::parse_from_rfc3339(datetime)
41                .map_err(|e| {
42                    HydrationError::InvalidData(format!(
43                        "Could not turn string '{datetime}' to rfc3339 Datetime. Error: {e}"
44                    ))
45                })?
46                .with_timezone(&Utc),
47        };
48
49        Ok(record)
50    }
51
52    fn get_projection() -> Projection {
53        let mut projection = Projection::default();
54        projection.add_field("epoch", "{:protocol_initializer:}.epoch", "integer");
55        projection.add_field("protocol", "{:protocol_initializer:}.protocol", "integer");
56        projection.add_field("created_at", "{:protocol_initializer:}.created_at", "text");
57
58        projection
59    }
60}