Skip to content

Commit 626ecc5

Browse files
committed
move EncodedCbor struct to separate mod
1 parent c822ff7 commit 626ecc5

File tree

6 files changed

+54
-51
lines changed

6 files changed

+54
-51
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//! An encoded CBOR (tag 24) struct
2+
3+
use minicbor::{data::Tag, Decode, Decoder, Encode};
4+
5+
use crate::Cbor;
6+
7+
/// encoded-cbor CBOR tag <https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml/>.
8+
const ENCODED_CBOR_TAG: u64 = 24;
9+
10+
/// An encoded CBOR struct, CBOR tag 24.
11+
#[derive(Debug, Clone, PartialEq)]
12+
pub struct EncodedCbor<T>(pub T)
13+
where T: for<'a> Cbor<'a>;
14+
15+
impl<T> Decode<'_, ()> for EncodedCbor<T>
16+
where T: for<'a> Cbor<'a>
17+
{
18+
fn decode(d: &mut Decoder<'_>, (): &mut ()) -> Result<Self, minicbor::decode::Error> {
19+
let tag = d.tag()?;
20+
if ENCODED_CBOR_TAG != tag.as_u64() {
21+
return Err(minicbor::decode::Error::message(format!(
22+
"tag value must be: {ENCODED_CBOR_TAG}, provided: {}",
23+
tag.as_u64(),
24+
)));
25+
}
26+
let cbor_bytes = d.bytes()?.to_vec();
27+
let cbor = T::from_bytes(&cbor_bytes).map_err(minicbor::decode::Error::message)?;
28+
Ok(Self(cbor))
29+
}
30+
}
31+
32+
impl<T> Encode<()> for EncodedCbor<T>
33+
where T: for<'a> Cbor<'a>
34+
{
35+
fn encode<W: minicbor::encode::Write>(
36+
&self, e: &mut minicbor::Encoder<W>, (): &mut (),
37+
) -> Result<(), minicbor::encode::Error<W::Error>> {
38+
e.tag(Tag::new(ENCODED_CBOR_TAG))?;
39+
let cbor_bytes = self
40+
.0
41+
.to_bytes()
42+
.map_err(minicbor::encode::Error::message)?;
43+
e.bytes(&cbor_bytes)?;
44+
Ok(())
45+
}
46+
}

rust/vote-tx-v2/src/gen_tx/builder.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
33
use anyhow::ensure;
44

5-
use super::{
6-
cose_protected_header, EncodedCbor, EventKey, EventMap, GeneralizedTx, TxBody, Vote, VoterData,
7-
};
8-
use crate::{uuid::Uuid, Cbor};
5+
use super::{cose_protected_header, EventKey, EventMap, GeneralizedTx, TxBody, Vote, VoterData};
6+
use crate::{encoded_cbor::EncodedCbor, uuid::Uuid, Cbor};
97

108
/// `GeneralizedTx` builder struct
119
#[allow(clippy::module_name_repetitions)]

rust/vote-tx-v2/src/gen_tx/mod.rs

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod vote;
1111
pub use builder::GeneralizedTxBuilder;
1212
use coset::CborSerializable;
1313
pub use event_map::{EventKey, EventMap};
14-
use minicbor::{data::Tag, Decode, Decoder, Encode, Encoder};
14+
use minicbor::{Decode, Decoder, Encode, Encoder};
1515
pub use tx_body::{TxBody, VoterData};
1616
pub use vote::{Choice, Proof, PropId, Vote};
1717

@@ -32,14 +32,6 @@ where
3232
signature: coset::CoseSign,
3333
}
3434

35-
/// An encoded CBOR struct, CBOR tag 24.
36-
#[derive(Debug, Clone, PartialEq)]
37-
pub struct EncodedCbor<T>(T)
38-
where T: for<'a> Cbor<'a>;
39-
40-
/// encoded-cbor CBOR tag <https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml/>.
41-
const ENCODED_CBOR_TAG: u64 = 24;
42-
4335
/// `GeneralizedTx` array struct length
4436
const GENERALIZED_TX_LEN: u64 = 2;
4537

@@ -109,39 +101,6 @@ where
109101
}
110102
}
111103

112-
impl<T> Decode<'_, ()> for EncodedCbor<T>
113-
where T: for<'a> Cbor<'a>
114-
{
115-
fn decode(d: &mut Decoder<'_>, (): &mut ()) -> Result<Self, minicbor::decode::Error> {
116-
let tag = d.tag()?;
117-
if ENCODED_CBOR_TAG != tag.as_u64() {
118-
return Err(minicbor::decode::Error::message(format!(
119-
"tag value must be: {ENCODED_CBOR_TAG}, provided: {}",
120-
tag.as_u64(),
121-
)));
122-
}
123-
let cbor_bytes = d.bytes()?.to_vec();
124-
let cbor = T::from_bytes(&cbor_bytes).map_err(minicbor::decode::Error::message)?;
125-
Ok(Self(cbor))
126-
}
127-
}
128-
129-
impl<T> Encode<()> for EncodedCbor<T>
130-
where T: for<'a> Cbor<'a>
131-
{
132-
fn encode<W: minicbor::encode::Write>(
133-
&self, e: &mut minicbor::Encoder<W>, (): &mut (),
134-
) -> Result<(), minicbor::encode::Error<W::Error>> {
135-
e.tag(Tag::new(ENCODED_CBOR_TAG))?;
136-
let cbor_bytes = self
137-
.0
138-
.to_bytes()
139-
.map_err(minicbor::encode::Error::message)?;
140-
e.bytes(&cbor_bytes)?;
141-
Ok(())
142-
}
143-
}
144-
145104
/// Reads CBOR bytes from the decoder and returns them as bytes.
146105
fn read_cbor_bytes(d: &mut Decoder<'_>) -> Result<Vec<u8>, minicbor::decode::Error> {
147106
let start = d.position();
@@ -169,7 +128,7 @@ mod tests {
169128
use test_strategy::proptest;
170129

171130
use super::*;
172-
use crate::uuid::Uuid;
131+
use crate::{encoded_cbor::EncodedCbor, uuid::Uuid};
173132

174133
type ChoiceT = Vec<u8>;
175134
type ProofT = Vec<u8>;

rust/vote-tx-v2/src/gen_tx/tx_body.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use minicbor::{Decode, Decoder, Encode, Encoder};
44

5-
use super::{EncodedCbor, EventMap, Vote};
6-
use crate::{uuid::Uuid, Cbor};
5+
use super::{EventMap, Vote};
6+
use crate::{encoded_cbor::EncodedCbor, uuid::Uuid, Cbor};
77

88
/// `TxBody` array struct length
99
const TX_BODY_LEN: u64 = 4;

rust/vote-tx-v2/src/gen_tx/vote.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
33
use minicbor::{Decode, Decoder, Encode};
44

5-
use super::EncodedCbor;
6-
use crate::Cbor;
5+
use crate::{encoded_cbor::EncodedCbor, Cbor};
76

87
/// `Vote` array struct length
98
const VOTE_LEN: u64 = 3;

rust/vote-tx-v2/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use anyhow::anyhow;
77
use minicbor::{Decode, Decoder, Encode, Encoder};
88

9+
pub mod encoded_cbor;
910
pub mod gen_tx;
1011
pub mod public_tx;
1112
pub mod uuid;

0 commit comments

Comments
 (0)