Skip to content

Commit b2d22c8

Browse files
committed
fix(rust/c509-certificate): format + err handling
1 parent 069640b commit b2d22c8

File tree

8 files changed

+38
-40
lines changed

8 files changed

+38
-40
lines changed

rust/c509-certificate/examples/cli/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ fn generate(
163163
let now_timestamp: u64 = Utc::now()
164164
.timestamp()
165165
.try_into()
166-
.expect("Timestamp cannot be converted to u64");
166+
.map_err(|_| anyhow::anyhow!("Current timestamp is invalid"))?;
167167

168168
let not_before = parse_or_default_date(c509_json.validity_not_before, now_timestamp)?;
169169
// Default as expire date for not_after

rust/c509-certificate/src/attributes/attribute.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ struct Helper {
7272

7373
impl<'de> Deserialize<'de> for Attribute {
7474
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
75-
where
76-
D: Deserializer<'de>,
77-
{
75+
where D: Deserializer<'de> {
7876
let helper = Helper::deserialize(deserializer)?;
7977
let oid =
8078
Oid::from_str(&helper.oid).map_err(|e| serde::de::Error::custom(format!("{e:?}")))?;
@@ -88,9 +86,7 @@ impl<'de> Deserialize<'de> for Attribute {
8886

8987
impl Serialize for Attribute {
9088
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
91-
where
92-
S: serde::Serializer,
93-
{
89+
where S: serde::Serializer {
9490
let helper = Helper {
9591
oid: self.registered_oid.get_c509_oid().get_oid().to_string(),
9692
value: self.value.clone(),
@@ -201,9 +197,11 @@ impl Decode<'_, ()> for AttributeValue {
201197
match d.datatype()? {
202198
minicbor::data::Type::String => Ok(AttributeValue::Text(d.str()?.to_string())),
203199
minicbor::data::Type::Bytes => Ok(AttributeValue::Bytes(d.bytes()?.to_vec())),
204-
_ => Err(minicbor::decode::Error::message(
205-
"Invalid AttributeValue, value should be either String or Bytes",
206-
)),
200+
_ => {
201+
Err(minicbor::decode::Error::message(
202+
"Invalid AttributeValue, value should be either String or Bytes",
203+
))
204+
},
207205
}
208206
}
209207
}

rust/c509-certificate/src/extensions/extension/mod.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ struct Helper {
6767

6868
impl<'de> Deserialize<'de> for Extension {
6969
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
70-
where
71-
D: Deserializer<'de>,
72-
{
70+
where D: Deserializer<'de> {
7371
let helper = Helper::deserialize(deserializer)?;
7472
let oid =
7573
Oid::from_str(&helper.oid).map_err(|e| serde::de::Error::custom(format!("{e:?}")))?;
@@ -80,9 +78,7 @@ impl<'de> Deserialize<'de> for Extension {
8078

8179
impl Serialize for Extension {
8280
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
83-
where
84-
S: serde::Serializer,
85-
{
81+
where S: serde::Serializer {
8682
let helper = Helper {
8783
oid: self.registered_oid.get_c509_oid().get_oid().to_string(),
8884
value: self.value.clone(),
@@ -231,8 +227,7 @@ impl Encode<()> for ExtensionValue {
231227
}
232228

233229
impl<C> Decode<'_, C> for ExtensionValue
234-
where
235-
C: ExtensionValueTypeTrait + Debug,
230+
where C: ExtensionValueTypeTrait + Debug
236231
{
237232
fn decode(d: &mut Decoder<'_>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
238233
match ctx.get_type() {
@@ -248,9 +243,11 @@ where
248243
let value = AlternativeName::decode(d, &mut ())?;
249244
Ok(ExtensionValue::AlternativeName(value))
250245
},
251-
ExtensionValueType::Unsupported => Err(minicbor::decode::Error::message(
252-
"Cannot decode Unsupported extension value",
253-
)),
246+
ExtensionValueType::Unsupported => {
247+
Err(minicbor::decode::Error::message(
248+
"Cannot decode Unsupported extension value",
249+
))
250+
},
254251
}
255252
}
256253
}

rust/c509-certificate/src/extensions/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! C509 Extension as a part of `TBSCertificate` used in C509 Certificate.
22
//!
33
//! Extension fallback of C509 OID extension
4-
//! Given OID if not found in the registered OID table, it will be encoded as an unwrapped OID.
4+
//! Given OID if not found in the registered OID table, it will be encoded as an unwrapped
5+
//! OID.
56
//!
67
//! ```cddl
78
//! Extensions and Extension can be encoded as the following:

rust/c509-certificate/src/name/mod.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,11 @@ impl Decode<'_, ()> for NameValue {
131131
// If Name is a text string, the attribute is a CommonName
132132
minicbor::data::Type::String => Ok(create_attributes_with_cn(d.str()?.to_string())),
133133
minicbor::data::Type::Bytes => decode_bytes(d),
134-
_ => Err(minicbor::decode::Error::message(
135-
"Name must be an array, text or bytes",
136-
)),
134+
_ => {
135+
Err(minicbor::decode::Error::message(
136+
"Name must be an array, text or bytes",
137+
))
138+
},
137139
}
138140
}
139141
}
@@ -256,9 +258,11 @@ fn decode_eui_cn_bytes(bytes: &[u8]) -> Result<NameValue, minicbor::decode::Erro
256258
)?);
257259
Ok(create_attributes_with_cn(text))
258260
},
259-
_ => Err(minicbor::decode::Error::message(
260-
"EUI-64 or MAC address must be 7 or 9 bytes",
261-
)),
261+
_ => {
262+
Err(minicbor::decode::Error::message(
263+
"EUI-64 or MAC address must be 7 or 9 bytes",
264+
))
265+
},
262266
}
263267
}
264268

@@ -466,6 +470,7 @@ pub(crate) mod test_name {
466470
// Test data from https://datatracker.ietf.org/doc/draft-ietf-cose-cbor-encoded-cert/11/
467471
// A.2. Example IEEE 802.1AR profiled X.509 Certificate
468472
// Issuer: C=US, ST=CA, O=Example Inc, OU=certification, CN=802.1AR CA
473+
#[allow(clippy::similar_names)]
469474
pub(crate) fn names() -> (Name, String) {
470475
let mut attr1 = Attribute::new(oid!(2.5.4 .6));
471476
attr1.add_value(AttributeValue::Text("US".to_string()));

rust/c509-certificate/src/oid.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ struct Helper {
5858

5959
impl<'de> Deserialize<'de> for C509oid {
6060
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
61-
where
62-
D: Deserializer<'de>,
63-
{
61+
where D: Deserializer<'de> {
6462
let helper = Helper::deserialize(deserializer)?;
6563
let oid =
6664
Oid::from_str(&helper.oid).map_err(|e| serde::de::Error::custom(format!("{e:?}")))?;
@@ -70,9 +68,7 @@ impl<'de> Deserialize<'de> for C509oid {
7068

7169
impl Serialize for C509oid {
7270
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
73-
where
74-
S: serde::Serializer,
75-
{
71+
where S: serde::Serializer {
7672
let helper = Helper {
7773
oid: self.0.to_string(),
7874
};
@@ -132,9 +128,10 @@ impl Decode<'_, ()> for C509oid {
132128
#[cfg(test)]
133129
mod test_c509_oid {
134130

135-
use super::*;
136131
use asn1_rs::oid;
137132

133+
use super::*;
134+
138135
// Test reference 3.1. Encoding of the SHA-256 OID
139136
// https://datatracker.ietf.org/doc/rfc9090/
140137
#[test]

rust/c509-certificate/src/tbs_cert.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ pub(crate) mod test_tbs_cert {
371371
// 83 2A 4D 33 6A 08 AD 67 DF 20 F1 50 64 21 18 8A 0A DE 6D 34 92 36
372372

373373
#[test]
374+
#[allow(clippy::similar_names)]
374375
fn tbs_cert2() {
375376
// ---------helper----------
376377
// C=US, ST=CA, L=LA, O=example Inc, OU=IoT/serialNumber=Wt1234
@@ -423,10 +424,9 @@ pub(crate) mod test_tbs_cert {
423424
true,
424425
));
425426
let mut gns = GeneralNames::new();
426-
let hw = OtherNameHardwareModuleName::new(
427-
oid!(1.3.6 .1 .4 .1 .6175 .10 .1),
428-
vec![0x01, 0x02, 0x03, 0x04],
429-
);
427+
let hw = OtherNameHardwareModuleName::new(oid!(1.3.6 .1 .4 .1 .6175 .10 .1), vec![
428+
0x01, 0x02, 0x03, 0x04,
429+
]);
430430
gns.add_gn(GeneralName::new(
431431
GeneralNameTypeRegistry::OtherNameHardwareModuleName,
432432
GeneralNameValue::OtherNameHWModuleName(hw),

rust/c509-certificate/src/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use minicbor::{encode::Write, Decode, Decoder, Encode, Encoder};
44
use serde::{Deserialize, Serialize};
55

66
/// A struct representing a time where it accept seconds since the Unix epoch.
7-
/// Doesn't support dates before the Unix epoch (January 1, 1970, 00:00:00 UTC)
7+
/// Doesn't support dates before the Unix epoch (January 1, 1970, 00:00:00 UTC)
88
/// so unsigned integer is used.
99
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
1010
pub struct Time(u64);

0 commit comments

Comments
 (0)