Skip to content

Commit d6f1737

Browse files
committed
Merge branch 'main' into fix/sign-doc-err-report
Signed-off-by: bkioshn <[email protected]>
2 parents 7c3371b + 7f180cb commit d6f1737

File tree

2 files changed

+78
-33
lines changed

2 files changed

+78
-33
lines changed

rust/signed_doc/src/metadata/document_ref.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,25 @@ use super::{decode_cbor_uuid, encode_cbor_uuid, UuidV7};
55

66
/// Reference to a Document.
77
#[derive(Copy, Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
8-
#[serde(untagged)]
9-
pub enum DocumentRef {
10-
/// Reference to the latest document
11-
Latest {
12-
/// Document ID UUID
13-
id: UuidV7,
14-
},
15-
/// Reference to the specific document version
16-
WithVer {
17-
/// Document ID UUID,
18-
id: UuidV7,
19-
/// Document Ver UUID
20-
ver: UuidV7,
21-
},
8+
pub struct DocumentRef {
9+
/// Reference to the Document Id
10+
pub id: UuidV7,
11+
/// Reference to the Document Ver, if not specified the latest document is meant
12+
#[serde(skip_serializing_if = "Option::is_none")]
13+
pub ver: Option<UuidV7>,
2214
}
2315

2416
impl TryFrom<DocumentRef> for Value {
2517
type Error = anyhow::Error;
2618

2719
fn try_from(value: DocumentRef) -> Result<Self, Self::Error> {
28-
match value {
29-
DocumentRef::Latest { id } => encode_cbor_uuid(id),
30-
DocumentRef::WithVer { id, ver } => {
31-
Ok(Value::Array(vec![
32-
encode_cbor_uuid(id)?,
33-
encode_cbor_uuid(ver)?,
34-
]))
35-
},
20+
if let Some(ver) = value.ver {
21+
Ok(Value::Array(vec![
22+
encode_cbor_uuid(value.id)?,
23+
encode_cbor_uuid(ver)?,
24+
]))
25+
} else {
26+
encode_cbor_uuid(value.id)
3627
}
3728
}
3829
}
@@ -43,7 +34,7 @@ impl TryFrom<&Value> for DocumentRef {
4334
#[allow(clippy::indexing_slicing)]
4435
fn try_from(val: &Value) -> anyhow::Result<DocumentRef> {
4536
if let Ok(id) = decode_cbor_uuid(val.clone()) {
46-
Ok(DocumentRef::Latest { id })
37+
Ok(DocumentRef { id, ver: None })
4738
} else {
4839
let Some(array) = val.as_array() else {
4940
anyhow::bail!("Document Reference must be either a single UUID or an array of two");
@@ -58,7 +49,7 @@ impl TryFrom<&Value> for DocumentRef {
5849
ver >= id,
5950
"Document Reference Version can never be smaller than its ID"
6051
);
61-
Ok(DocumentRef::WithVer { id, ver })
52+
Ok(DocumentRef { id, ver: Some(ver) })
6253
}
6354
}
6455
}

rust/signed_doc/src/metadata/extra_fields.rs

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,88 @@ const CATEGORY_ID_KEY: &str = "category_id";
3232
pub struct ExtraFields {
3333
/// Reference to the latest document.
3434
#[serde(rename = "ref", skip_serializing_if = "Option::is_none")]
35-
pub(super) doc_ref: Option<DocumentRef>,
35+
doc_ref: Option<DocumentRef>,
3636
/// Reference to the document template.
3737
#[serde(skip_serializing_if = "Option::is_none")]
38-
pub(super) template: Option<DocumentRef>,
38+
template: Option<DocumentRef>,
3939
/// Reference to the document reply.
4040
#[serde(skip_serializing_if = "Option::is_none")]
41-
pub(super) reply: Option<DocumentRef>,
41+
reply: Option<DocumentRef>,
4242
/// Reference to the document section.
4343
#[serde(skip_serializing_if = "Option::is_none")]
44-
pub(super) section: Option<String>,
44+
section: Option<String>,
4545
/// Reference to the document collaborators. Collaborator type is TBD.
4646
#[serde(default = "Vec::new", skip_serializing_if = "Vec::is_empty")]
47-
pub(super) collabs: Vec<String>,
47+
collabs: Vec<String>,
4848
/// Unique identifier for the brand that is running the voting.
4949
#[serde(skip_serializing_if = "Option::is_none")]
50-
pub(super) brand_id: Option<UuidV4>,
50+
brand_id: Option<UuidV4>,
5151
/// Unique identifier for the campaign of voting.
5252
#[serde(skip_serializing_if = "Option::is_none")]
53-
pub(super) campaign_id: Option<UuidV4>,
53+
campaign_id: Option<UuidV4>,
5454
/// Unique identifier for the election.
5555
#[serde(skip_serializing_if = "Option::is_none")]
56-
pub(super) election_id: Option<UuidV4>,
56+
election_id: Option<UuidV4>,
5757
/// Unique identifier for the voting category as a collection of proposals.
5858
#[serde(skip_serializing_if = "Option::is_none")]
59-
pub(super) category_id: Option<UuidV4>,
59+
category_id: Option<UuidV4>,
6060
}
6161

6262
impl ExtraFields {
63+
/// Return `ref` field.
64+
#[must_use]
65+
pub fn doc_ref(&self) -> Option<DocumentRef> {
66+
self.doc_ref
67+
}
68+
69+
/// Return `template` field.
70+
#[must_use]
71+
pub fn template(&self) -> Option<DocumentRef> {
72+
self.template
73+
}
74+
75+
/// Return `reply` field.
76+
#[must_use]
77+
pub fn reply(&self) -> Option<DocumentRef> {
78+
self.reply
79+
}
80+
81+
/// Return `section` field.
82+
#[must_use]
83+
pub fn section(&self) -> Option<&String> {
84+
self.section.as_ref()
85+
}
86+
87+
/// Return `collabs` field.
88+
#[must_use]
89+
pub fn collabs(&self) -> &Vec<String> {
90+
&self.collabs
91+
}
92+
93+
/// Return `brand_id` field.
94+
#[must_use]
95+
pub fn brand_id(&self) -> Option<UuidV4> {
96+
self.brand_id
97+
}
98+
99+
/// Return `campaign_id` field.
100+
#[must_use]
101+
pub fn campaign_id(&self) -> Option<UuidV4> {
102+
self.campaign_id
103+
}
104+
105+
/// Return `election_id` field.
106+
#[must_use]
107+
pub fn election_id(&self) -> Option<UuidV4> {
108+
self.election_id
109+
}
110+
111+
/// Return `category_id` field.
112+
#[must_use]
113+
pub fn category_id(&self) -> Option<UuidV4> {
114+
self.category_id
115+
}
116+
63117
/// Fill the COSE header `ExtraFields` data into the header builder.
64118
pub(super) fn fill_cose_header_fields(
65119
&self, mut builder: coset::HeaderBuilder,

0 commit comments

Comments
 (0)