|
1 | 1 | use chrono::{DateTime, Utc};
|
2 |
| -use mithril_common::entities::{CertificatePending, Epoch}; |
| 2 | +use mithril_common::{ |
| 3 | + entities::{CertificatePending, Epoch}, |
| 4 | + StdError, |
| 5 | +}; |
3 | 6 | use mithril_persistence::sqlite::{HydrationError, Projection, SourceAlias, SqLiteEntity};
|
4 | 7 |
|
5 | 8 | /// CertificatePending record is the representation of a stored pending certificate.
|
@@ -61,26 +64,31 @@ impl SqLiteEntity for CertificatePendingRecord {
|
61 | 64 | }
|
62 | 65 | }
|
63 | 66 |
|
64 |
| -impl From<CertificatePending> for CertificatePendingRecord { |
65 |
| - fn from(value: CertificatePending) -> Self { |
66 |
| - Self { |
| 67 | +impl TryFrom<CertificatePending> for CertificatePendingRecord { |
| 68 | + type Error = StdError; |
| 69 | + |
| 70 | + fn try_from(value: CertificatePending) -> Result<Self, Self::Error> { |
| 71 | + let record = Self { |
67 | 72 | epoch: value.epoch,
|
68 |
| - certificate: serde_json::to_string(&value).unwrap(), |
| 73 | + certificate: serde_json::to_string(&value)?, |
69 | 74 | created_at: Utc::now(),
|
70 |
| - } |
| 75 | + }; |
| 76 | + Ok(record) |
71 | 77 | }
|
72 | 78 | }
|
73 | 79 |
|
74 |
| -impl From<CertificatePendingRecord> for CertificatePending { |
75 |
| - fn from(record: CertificatePendingRecord) -> Self { |
76 |
| - let c: CertificatePending = serde_json::from_str(&record.certificate).unwrap(); |
77 |
| - Self { |
| 80 | +impl TryFrom<CertificatePendingRecord> for CertificatePending { |
| 81 | + type Error = StdError; |
| 82 | + fn try_from(record: CertificatePendingRecord) -> Result<Self, Self::Error> { |
| 83 | + let c: CertificatePending = serde_json::from_str(&record.certificate)?; |
| 84 | + let pending_certificate = Self { |
78 | 85 | epoch: record.epoch,
|
79 | 86 | signed_entity_type: c.signed_entity_type,
|
80 | 87 | protocol_parameters: c.protocol_parameters,
|
81 | 88 | next_protocol_parameters: c.next_protocol_parameters,
|
82 | 89 | signers: c.signers,
|
83 | 90 | next_signers: c.next_signers,
|
84 |
| - } |
| 91 | + }; |
| 92 | + Ok(pending_certificate) |
85 | 93 | }
|
86 | 94 | }
|
0 commit comments