@@ -4,9 +4,9 @@ use anyhow::anyhow;
4
4
use async_trait:: async_trait;
5
5
use sqlite:: ConnectionThreadSafe ;
6
6
7
- use mithril_common:: StdResult ;
8
7
use mithril_common:: certificate_chain:: { CertificateRetriever , CertificateRetrieverError } ;
9
8
use mithril_common:: entities:: { Certificate , Epoch } ;
9
+ use mithril_common:: { StdError , StdResult } ;
10
10
use mithril_persistence:: sqlite:: ConnectionExtensions ;
11
11
12
12
use crate :: database:: query:: {
@@ -30,49 +30,56 @@ impl CertificateRepository {
30
30
/// Return the certificate corresponding to the given hash if any.
31
31
pub async fn get_certificate < T > ( & self , hash : & str ) -> StdResult < Option < T > >
32
32
where
33
- T : From < CertificateRecord > ,
33
+ T : TryFrom < CertificateRecord > ,
34
+ T :: Error : Into < StdError > ,
34
35
{
35
36
let record = self
36
37
. connection
37
38
. fetch_first ( GetCertificateRecordQuery :: by_certificate_id ( hash) ) ?;
38
39
39
- Ok ( record. map ( |c| c. into ( ) ) )
40
+ record. map ( |c| c. try_into ( ) . map_err ( Into :: into ) ) . transpose ( )
40
41
}
41
42
42
43
/// Return the latest certificates.
43
44
pub async fn get_latest_certificates < T > ( & self , last_n : usize ) -> StdResult < Vec < T > >
44
45
where
45
- T : From < CertificateRecord > ,
46
+ T : TryFrom < CertificateRecord > ,
47
+ T :: Error : Into < StdError > ,
46
48
{
47
49
let cursor = self . connection . fetch ( GetCertificateRecordQuery :: all ( ) ) ?;
48
50
49
- Ok ( cursor. take ( last_n) . map ( |v| v. into ( ) ) . collect ( ) )
51
+ cursor
52
+ . take ( last_n)
53
+ . map ( |c| c. try_into ( ) . map_err ( Into :: into) )
54
+ . collect ( )
50
55
}
51
56
52
57
/// Return the latest genesis certificate.
53
58
pub async fn get_latest_genesis_certificate < T > ( & self ) -> StdResult < Option < T > >
54
59
where
55
- T : From < CertificateRecord > ,
60
+ T : TryFrom < CertificateRecord > ,
61
+ T :: Error : Into < StdError > ,
56
62
{
57
63
let record = self
58
64
. connection
59
65
. fetch_first ( GetCertificateRecordQuery :: all_genesis ( ) ) ?;
60
66
61
- Ok ( record. map ( |c| c. into ( ) ) )
67
+ record. map ( |c| c. try_into ( ) . map_err ( Into :: into ) ) . transpose ( )
62
68
}
63
69
64
70
/// Return the first certificate signed per epoch as the reference
65
71
/// certificate for this Epoch. This will be the parent certificate for all
66
72
/// other certificates issued within this Epoch.
67
73
pub async fn get_master_certificate_for_epoch < T > ( & self , epoch : Epoch ) -> StdResult < Option < T > >
68
74
where
69
- T : From < CertificateRecord > ,
75
+ T : TryFrom < CertificateRecord > ,
76
+ T :: Error : Into < StdError > ,
70
77
{
71
78
let record = self
72
79
. connection
73
80
. fetch_first ( MasterCertificateQuery :: for_epoch ( epoch) ) ?;
74
81
75
- Ok ( record. map ( |c| c. into ( ) ) )
82
+ record. map ( |c| c. try_into ( ) . map_err ( Into :: into ) ) . transpose ( )
76
83
}
77
84
78
85
/// Create a new certificate in the database.
@@ -86,7 +93,7 @@ impl CertificateRepository {
86
93
panic ! ( "No entity returned by the persister, certificate = {certificate:#?}" )
87
94
} ) ;
88
95
89
- Ok ( record. into ( ) )
96
+ record. try_into ( )
90
97
}
91
98
92
99
/// Create many certificates at once in the database.
@@ -105,7 +112,7 @@ impl CertificateRepository {
105
112
let new_certificates =
106
113
self . connection . fetch ( InsertCertificateRecordQuery :: many ( records) ) ?;
107
114
108
- Ok ( new_certificates. map ( |cert| cert. into ( ) ) . collect ( ) )
115
+ new_certificates. map ( |cert| cert. try_into ( ) ) . collect :: < StdResult < _ > > ( )
109
116
}
110
117
111
118
/// Create, or replace if they already exist, many certificates at once in the database.
@@ -125,7 +132,7 @@ impl CertificateRepository {
125
132
. connection
126
133
. fetch ( InsertOrReplaceCertificateRecordQuery :: many ( records) ) ?;
127
134
128
- Ok ( new_certificates. map ( |cert| cert. into ( ) ) . collect ( ) )
135
+ new_certificates. map ( |cert| cert. try_into ( ) ) . collect :: < StdResult < _ > > ( )
129
136
}
130
137
131
138
/// Delete all the given certificates from the database
@@ -335,7 +342,7 @@ mod tests {
335
342
async fn get_master_certificate_one_cert_in_current_epoch_recorded_returns_that_one ( ) {
336
343
let connection = Arc :: new ( main_db_connection ( ) . unwrap ( ) ) ;
337
344
let certificate = CertificateRecord :: dummy_genesis ( "1" , Epoch ( 1 ) ) ;
338
- let expected_certificate: Certificate = certificate. clone ( ) . into ( ) ;
345
+ let expected_certificate: Certificate = certificate. clone ( ) . try_into ( ) . unwrap ( ) ;
339
346
insert_certificate_records ( & connection, vec ! [ certificate] ) ;
340
347
341
348
let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
@@ -357,7 +364,8 @@ mod tests {
357
364
CertificateRecord :: dummy_db_snapshot( "2" , "1" , Epoch ( 1 ) , 2 ) ,
358
365
CertificateRecord :: dummy_db_snapshot( "3" , "1" , Epoch ( 1 ) , 3 ) ,
359
366
] ;
360
- let expected_certificate: Certificate = certificates. first ( ) . unwrap ( ) . clone ( ) . into ( ) ;
367
+ let expected_certificate: Certificate =
368
+ certificates. first ( ) . unwrap ( ) . clone ( ) . try_into ( ) . unwrap ( ) ;
361
369
insert_certificate_records ( & connection, certificates) ;
362
370
363
371
let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
@@ -379,7 +387,8 @@ mod tests {
379
387
CertificateRecord :: dummy_db_snapshot( "2" , "1" , Epoch ( 1 ) , 2 ) ,
380
388
CertificateRecord :: dummy_db_snapshot( "3" , "1" , Epoch ( 1 ) , 3 ) ,
381
389
] ;
382
- let expected_certificate: Certificate = certificates. first ( ) . unwrap ( ) . clone ( ) . into ( ) ;
390
+ let expected_certificate: Certificate =
391
+ certificates. first ( ) . unwrap ( ) . clone ( ) . try_into ( ) . unwrap ( ) ;
383
392
insert_certificate_records ( & connection, certificates) ;
384
393
385
394
let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
@@ -402,7 +411,8 @@ mod tests {
402
411
CertificateRecord :: dummy_db_snapshot( "3" , "1" , Epoch ( 1 ) , 3 ) ,
403
412
CertificateRecord :: dummy_db_snapshot( "4" , "1" , Epoch ( 2 ) , 4 ) ,
404
413
] ;
405
- let expected_certificate: Certificate = certificates. last ( ) . unwrap ( ) . clone ( ) . into ( ) ;
414
+ let expected_certificate: Certificate =
415
+ certificates. last ( ) . unwrap ( ) . clone ( ) . try_into ( ) . unwrap ( ) ;
406
416
insert_certificate_records ( & connection, certificates) ;
407
417
408
418
let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
@@ -427,7 +437,8 @@ mod tests {
427
437
CertificateRecord :: dummy_db_snapshot( "5" , "4" , Epoch ( 2 ) , 5 ) ,
428
438
CertificateRecord :: dummy_db_snapshot( "6" , "4" , Epoch ( 2 ) , 6 ) ,
429
439
] ;
430
- let expected_certificate: Certificate = certificates. get ( 3 ) . unwrap ( ) . clone ( ) . into ( ) ;
440
+ let expected_certificate: Certificate =
441
+ certificates. get ( 3 ) . unwrap ( ) . clone ( ) . try_into ( ) . unwrap ( ) ;
431
442
insert_certificate_records ( & connection, certificates) ;
432
443
433
444
let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
@@ -469,7 +480,8 @@ mod tests {
469
480
CertificateRecord :: dummy_db_snapshot( "3" , "1" , Epoch ( 1 ) , 3 ) ,
470
481
CertificateRecord :: dummy_genesis( "4" , Epoch ( 1 ) ) ,
471
482
] ;
472
- let expected_certificate: Certificate = certificates. last ( ) . unwrap ( ) . clone ( ) . into ( ) ;
483
+ let expected_certificate: Certificate =
484
+ certificates. last ( ) . unwrap ( ) . clone ( ) . try_into ( ) . unwrap ( ) ;
473
485
insert_certificate_records ( & connection, certificates) ;
474
486
475
487
let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
@@ -494,7 +506,8 @@ mod tests {
494
506
CertificateRecord :: dummy_db_snapshot( "5" , "1" , Epoch ( 2 ) , 5 ) ,
495
507
CertificateRecord :: dummy_genesis( "6" , Epoch ( 2 ) ) ,
496
508
] ;
497
- let expected_certificate: Certificate = certificates. last ( ) . unwrap ( ) . clone ( ) . into ( ) ;
509
+ let expected_certificate: Certificate =
510
+ certificates. last ( ) . unwrap ( ) . clone ( ) . try_into ( ) . unwrap ( ) ;
498
511
insert_certificate_records ( & connection, certificates) ;
499
512
500
513
let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
@@ -517,7 +530,8 @@ mod tests {
517
530
CertificateRecord :: dummy_db_snapshot( "3" , "1" , Epoch ( 1 ) , 3 ) ,
518
531
CertificateRecord :: dummy_genesis( "4" , Epoch ( 2 ) ) ,
519
532
] ;
520
- let expected_certificate: Certificate = certificates. last ( ) . unwrap ( ) . clone ( ) . into ( ) ;
533
+ let expected_certificate: Certificate =
534
+ certificates. last ( ) . unwrap ( ) . clone ( ) . try_into ( ) . unwrap ( ) ;
521
535
insert_certificate_records ( & connection, certificates) ;
522
536
523
537
let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
@@ -578,7 +592,8 @@ mod tests {
578
592
CertificateRecord :: dummy_db_snapshot( "3" , "1" , Epoch ( 1 ) , 3 ) ,
579
593
] ;
580
594
insert_certificate_records ( & connection, records. clone ( ) ) ;
581
- let certificates: Vec < Certificate > = records. into_iter ( ) . map ( |c| c. into ( ) ) . collect ( ) ;
595
+ let certificates: Vec < Certificate > =
596
+ records. into_iter ( ) . map ( |c| c. try_into ( ) . unwrap ( ) ) . collect ( ) ;
582
597
583
598
// Delete all records except the first
584
599
repository
0 commit comments