• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

polyphony-chat / polyproto-rs / #8

23 Feb 2025 10:48AM UTC coverage: 68.198% (-8.2%) from 76.367%
#8

push

bitfl0wer
Use LLVM and no-dead-code as recommended by tarpaulin for linux

980 of 1437 relevant lines covered (68.2%)

30.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.86
/src/types/spki/algorithmidentifierowned.rs
1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4

5
use std::ops::{Deref, DerefMut};
6

7
use der::{Any, Decode, Encode};
8
use spki::ObjectIdentifier;
9

10
#[derive(Debug, PartialEq, Eq, Clone, PartialOrd, Ord)]
11
/// `AlgorithmIdentifier` reference which has `Any` parameters.
12
///
13
/// A wrapper around `spki::AlgorithmIdentifierOwned`, which provides `serde` support, if enabled by
14
/// the `serde` feature.
15
///
16
/// ## De-/Serialization expectations
17
///
18
/// This type expects a DER encoded AlgorithmIdentifier with optional der::Any parameters. The DER
19
/// encoded data has to be provided in the form of an array of bytes. Types that fulfill this
20
/// expectation are, for example, `&[u8]`, `Vec<u8>` and `&[u8; N]`.
21
pub struct AlgorithmIdentifierOwned(spki::AlgorithmIdentifierOwned);
22

23
impl AlgorithmIdentifierOwned {
24
    /// Create a new `AlgorithmIdentifierOwned`.
25
    pub fn new(oid: ObjectIdentifier, parameters: Option<Any>) -> Self {
21✔
26
        Self(spki::AlgorithmIdentifierOwned { oid, parameters })
21✔
27
    }
28

29
    /// Try to encode this type as DER.
30
    pub fn to_der(&self) -> Result<Vec<u8>, der::Error> {
5✔
31
        self.0.to_der()
5✔
32
    }
33

34
    /// Try to decode this type from DER.
35
    pub fn from_der(bytes: &[u8]) -> Result<AlgorithmIdentifierOwned, der::Error> {
3✔
36
        spki::AlgorithmIdentifierOwned::from_der(bytes).map(Self)
3✔
37
    }
38
}
39

40
impl Deref for AlgorithmIdentifierOwned {
41
    type Target = spki::AlgorithmIdentifierOwned;
42

43
    fn deref(&self) -> &Self::Target {
×
44
        &self.0
×
45
    }
46
}
47

48
impl DerefMut for AlgorithmIdentifierOwned {
49
    fn deref_mut(&mut self) -> &mut Self::Target {
×
50
        &mut self.0
×
51
    }
52
}
53

54
impl From<spki::AlgorithmIdentifierOwned> for AlgorithmIdentifierOwned {
55
    fn from(value: spki::AlgorithmIdentifierOwned) -> Self {
1✔
56
        Self(value)
1✔
57
    }
58
}
59

60
impl From<AlgorithmIdentifierOwned> for spki::AlgorithmIdentifierOwned {
61
    fn from(value: AlgorithmIdentifierOwned) -> Self {
18✔
62
        value.0
18✔
63
    }
64
}
65

66
#[cfg(feature = "serde")]
67
mod serde_support {
68
    use super::AlgorithmIdentifierOwned;
69
    use serde::de::Visitor;
70
    use serde::{Deserialize, Serialize};
71
    struct AlgorithmIdentifierVisitor;
72

73
    impl<'de> Visitor<'de> for AlgorithmIdentifierVisitor {
74
        type Value = AlgorithmIdentifierOwned;
75

76
        fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
×
77
            formatter
×
78
                .write_str("a valid DER encoded byte slice representing an AlgorithmIdentifier")
79
        }
80

81
        fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
×
82
        where
83
            E: serde::de::Error,
84
        {
85
            AlgorithmIdentifierOwned::from_der(v).map_err(serde::de::Error::custom)
×
86
        }
87

88
        fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
3✔
89
        where
90
            A: serde::de::SeqAccess<'de>,
91
        {
92
            let mut bytes: Vec<u8> = Vec::new(); // Create a new Vec to store the bytes
3✔
93
            while let Some(byte) = seq.next_element()? {
93✔
94
                // "Iterate" over the sequence, assuming each element is a byte
95
                bytes.push(byte) // Push the byte to the Vec
45✔
96
            }
97
            AlgorithmIdentifierOwned::from_der(&bytes).map_err(serde::de::Error::custom)
3✔
98
        }
99
    }
100

101
    impl<'de> Deserialize<'de> for AlgorithmIdentifierOwned {
102
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
3✔
103
        where
104
            D: serde::Deserializer<'de>,
105
        {
106
            deserializer.deserialize_bytes(AlgorithmIdentifierVisitor)
3✔
107
        }
108
    }
109

110
    impl Serialize for AlgorithmIdentifierOwned {
111
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
5✔
112
        where
113
            S: serde::Serializer,
114
        {
115
            let der = self.to_der().map_err(serde::ser::Error::custom)?;
10✔
116
            serializer.serialize_bytes(&der)
×
117
        }
118
    }
119
}
120

121
#[cfg(test)]
122
mod test {
123
    use std::str::FromStr;
124

125
    use der::asn1::BitString;
126
    use der::{Any, Decode, Encode};
127
    use log::trace;
128
    use serde_json::json;
129
    use spki::ObjectIdentifier;
130

131
    use crate::testing_utils::init_logger;
132

133
    use super::AlgorithmIdentifierOwned;
134

135
    #[test]
136
    fn de_serialize() {
137
        init_logger();
138
        let oid = ObjectIdentifier::from_str("1.1.1.4.5").unwrap();
139
        let alg = AlgorithmIdentifierOwned::new(oid, None);
140
        let json = json!(alg);
141
        let deserialized: AlgorithmIdentifierOwned = serde_json::from_value(json).unwrap();
142
        assert_eq!(alg, deserialized);
143
        trace!("deserialized: {:?}", deserialized);
144
        trace!("original: {:?}", alg);
145

146
        let bytes = [48, 6, 6, 3, 43, 6, 1, 5, 1, 4, 5, 5, 23, 2, 0, 0];
147
        let bitstring = BitString::from_bytes(&bytes).unwrap();
148
        let alg = AlgorithmIdentifierOwned::new(
149
            oid,
150
            Some(Any::from_der(&bitstring.to_der().unwrap()).unwrap()),
151
        );
152
        let json = json!(alg);
153
        let deserialized: AlgorithmIdentifierOwned = serde_json::from_value(json).unwrap();
154
        trace!("deserialized: {:?}", deserialized);
155
        trace!("original: {:?}", alg);
156
        assert_eq!(alg, deserialized);
157
    }
158
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc