types.rs
1 use auths_verifier::types::{ 2 ChainLink as RustChainLink, VerificationReport as RustVerificationReport, 3 VerificationStatus as RustVerificationStatus, 4 }; 5 use pyo3::prelude::*; 6 7 #[pyclass] 8 #[derive(Clone)] 9 pub struct VerificationResult { 10 #[pyo3(get)] 11 pub valid: bool, 12 #[pyo3(get)] 13 pub error: Option<String>, 14 #[pyo3(get)] 15 pub error_code: Option<String>, 16 } 17 18 #[pymethods] 19 impl VerificationResult { 20 fn __repr__(&self) -> String { 21 if self.valid { 22 "VerificationResult(valid=True)".to_string() 23 } else if let Some(code) = &self.error_code { 24 format!( 25 "VerificationResult(valid=False, error={:?}, error_code={:?})", 26 self.error.as_deref().unwrap_or("None"), 27 code 28 ) 29 } else { 30 format!( 31 "VerificationResult(valid=False, error={:?})", 32 self.error.as_deref().unwrap_or("None") 33 ) 34 } 35 } 36 37 fn __bool__(&self) -> bool { 38 self.valid 39 } 40 } 41 42 #[pyclass] 43 #[derive(Clone)] 44 pub struct VerificationStatus { 45 #[pyo3(get)] 46 pub status_type: String, 47 #[pyo3(get)] 48 pub at: Option<String>, 49 #[pyo3(get)] 50 pub step: Option<usize>, 51 #[pyo3(get)] 52 pub missing_link: Option<String>, 53 #[pyo3(get)] 54 pub required: Option<usize>, 55 #[pyo3(get)] 56 pub verified: Option<usize>, 57 } 58 59 #[pymethods] 60 impl VerificationStatus { 61 fn __repr__(&self) -> String { 62 format!("VerificationStatus(type='{}')", self.status_type) 63 } 64 65 fn is_valid(&self) -> bool { 66 self.status_type == "Valid" 67 } 68 } 69 70 impl From<RustVerificationStatus> for VerificationStatus { 71 fn from(status: RustVerificationStatus) -> Self { 72 match status { 73 RustVerificationStatus::Valid => VerificationStatus { 74 status_type: "Valid".to_string(), 75 at: None, 76 step: None, 77 missing_link: None, 78 required: None, 79 verified: None, 80 }, 81 RustVerificationStatus::Expired { at } => VerificationStatus { 82 status_type: "Expired".to_string(), 83 at: Some(at.to_rfc3339()), 84 step: None, 85 missing_link: None, 86 required: None, 87 verified: None, 88 }, 89 RustVerificationStatus::Revoked { at } => VerificationStatus { 90 status_type: "Revoked".to_string(), 91 at: at.map(|t| t.to_rfc3339()), 92 step: None, 93 missing_link: None, 94 required: None, 95 verified: None, 96 }, 97 RustVerificationStatus::InvalidSignature { step } => VerificationStatus { 98 status_type: "InvalidSignature".to_string(), 99 at: None, 100 step: Some(step), 101 missing_link: None, 102 required: None, 103 verified: None, 104 }, 105 RustVerificationStatus::BrokenChain { missing_link } => VerificationStatus { 106 status_type: "BrokenChain".to_string(), 107 at: None, 108 step: None, 109 missing_link: Some(missing_link), 110 required: None, 111 verified: None, 112 }, 113 RustVerificationStatus::InsufficientWitnesses { required, verified } => { 114 VerificationStatus { 115 status_type: "InsufficientWitnesses".to_string(), 116 at: None, 117 step: None, 118 missing_link: None, 119 required: Some(required), 120 verified: Some(verified), 121 } 122 } 123 } 124 } 125 } 126 127 #[pyclass] 128 #[derive(Clone)] 129 pub struct ChainLink { 130 #[pyo3(get)] 131 pub issuer: String, 132 #[pyo3(get)] 133 pub subject: String, 134 #[pyo3(get)] 135 pub valid: bool, 136 #[pyo3(get)] 137 pub error: Option<String>, 138 } 139 140 #[pymethods] 141 impl ChainLink { 142 fn __repr__(&self) -> String { 143 format!( 144 "ChainLink(issuer='{}', subject='{}', valid={})", 145 self.issuer, self.subject, self.valid 146 ) 147 } 148 } 149 150 impl From<RustChainLink> for ChainLink { 151 fn from(link: RustChainLink) -> Self { 152 ChainLink { 153 issuer: link.issuer, 154 subject: link.subject, 155 valid: link.valid, 156 error: link.error, 157 } 158 } 159 } 160 161 #[pyclass] 162 #[derive(Clone)] 163 pub struct VerificationReport { 164 #[pyo3(get)] 165 pub status: VerificationStatus, 166 #[pyo3(get)] 167 pub chain: Vec<ChainLink>, 168 #[pyo3(get)] 169 pub warnings: Vec<String>, 170 } 171 172 #[pymethods] 173 impl VerificationReport { 174 fn __repr__(&self) -> String { 175 format!( 176 "VerificationReport(status={}, chain_length={})", 177 self.status.status_type, 178 self.chain.len() 179 ) 180 } 181 182 fn is_valid(&self) -> bool { 183 self.status.is_valid() 184 } 185 } 186 187 impl From<RustVerificationReport> for VerificationReport { 188 fn from(report: RustVerificationReport) -> Self { 189 VerificationReport { 190 status: report.status.into(), 191 chain: report.chain.into_iter().map(|l| l.into()).collect(), 192 warnings: report.warnings, 193 } 194 } 195 }