Skip to content

Commit 7f180cb

Browse files
authored
feat(rust/signed-doc): Improve catalyst-signed-doc crate API. (#177)
* Cleanup signed-doc API, make some needed for cat-gatewat getters * fix * wip
1 parent eaf071e commit 7f180cb

File tree

4 files changed

+80
-35
lines changed

4 files changed

+80
-35
lines changed

rust/signed_doc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ license.workspace = true
1111
workspace = true
1212

1313
[dependencies]
14-
catalyst-types = { version = "0.0.1", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "r20250122-00" }
14+
catalyst-types = { version = "0.0.1", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "r20250124-00" }
1515
anyhow = "1.0.95"
1616
serde = { version = "1.0.217", features = ["derive"] }
1717
serde_json = "1.0.134"

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
@@ -31,34 +31,88 @@ const CATEGORY_ID_KEY: &str = "category_id";
3131
pub struct ExtraFields {
3232
/// Reference to the latest document.
3333
#[serde(rename = "ref", skip_serializing_if = "Option::is_none")]
34-
pub(super) doc_ref: Option<DocumentRef>,
34+
doc_ref: Option<DocumentRef>,
3535
/// Reference to the document template.
3636
#[serde(skip_serializing_if = "Option::is_none")]
37-
pub(super) template: Option<DocumentRef>,
37+
template: Option<DocumentRef>,
3838
/// Reference to the document reply.
3939
#[serde(skip_serializing_if = "Option::is_none")]
40-
pub(super) reply: Option<DocumentRef>,
40+
reply: Option<DocumentRef>,
4141
/// Reference to the document section.
4242
#[serde(skip_serializing_if = "Option::is_none")]
43-
pub(super) section: Option<String>,
43+
section: Option<String>,
4444
/// Reference to the document collaborators. Collaborator type is TBD.
4545
#[serde(default = "Vec::new", skip_serializing_if = "Vec::is_empty")]
46-
pub(super) collabs: Vec<String>,
46+
collabs: Vec<String>,
4747
/// Unique identifier for the brand that is running the voting.
4848
#[serde(skip_serializing_if = "Option::is_none")]
49-
pub(super) brand_id: Option<UuidV4>,
49+
brand_id: Option<UuidV4>,
5050
/// Unique identifier for the campaign of voting.
5151
#[serde(skip_serializing_if = "Option::is_none")]
52-
pub(super) campaign_id: Option<UuidV4>,
52+
campaign_id: Option<UuidV4>,
5353
/// Unique identifier for the election.
5454
#[serde(skip_serializing_if = "Option::is_none")]
55-
pub(super) election_id: Option<UuidV4>,
55+
election_id: Option<UuidV4>,
5656
/// Unique identifier for the voting category as a collection of proposals.
5757
#[serde(skip_serializing_if = "Option::is_none")]
58-
pub(super) category_id: Option<UuidV4>,
58+
category_id: Option<UuidV4>,
5959
}
6060

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

rust/signed_doc/src/metadata/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod extra_fields;
1212

1313
use algorithm::Algorithm;
1414
use anyhow::anyhow;
15-
pub use catalyst_types::uuid::{CborContext, V4 as UuidV4, V7 as UuidV7};
15+
pub use catalyst_types::uuid::{CborContext, UuidV4, UuidV7};
1616
pub use content_encoding::ContentEncoding;
1717
pub use content_type::ContentType;
1818
use coset::{iana::CoapContentFormat, CborSerializable};

0 commit comments

Comments
 (0)