Skip to content

Commit 201b858

Browse files
apskhemMr-Leshiy
andauthored
refactor(rust/signed-doc): Change collabs header to collaborators (#395)
* chore: rename * feat: cat-id decoder * fix: minor sync * chore: minor * fix: bstr * chore: b string --------- Co-authored-by: Alex Pozhylenkov <[email protected]>
1 parent af7fc4b commit 201b858

File tree

7 files changed

+56
-35
lines changed

7 files changed

+56
-35
lines changed

.config/dictionaries/project.dic

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ ciphertexts
4848
Coap
4949
codegen
5050
codepoints
51-
collabs
5251
coti
5352
coverallsapp
5453
cpus

rust/signed_doc/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
<!-- cspell: words collabs -->
2-
31
# Catalyst signed document
42

53
Catalyst signed document crate implementation based on this

rust/signed_doc/src/metadata/collaborators.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
//! Catalyst Signed Document `collabs` field type definition.
1+
//! Catalyst Signed Document `collaborators` field type definition.
22
3-
use std::ops::Deref;
3+
use std::{ops::Deref, str::FromStr};
44

5-
/// 'collabs' field type definition, which is a JSON path string
5+
use catalyst_types::catalyst_id::CatalystId;
6+
7+
/// 'collaborators' field type definition, which is a JSON path string
68
#[derive(Clone, Debug, PartialEq)]
7-
pub struct Collaborators(Vec<String>);
9+
pub struct Collaborators(Vec<CatalystId>);
810

911
impl Deref for Collaborators {
10-
type Target = Vec<String>;
12+
type Target = Vec<CatalystId>;
1113

1214
fn deref(&self) -> &Self::Target {
1315
&self.0
@@ -26,7 +28,7 @@ impl minicbor::Encode<()> for Collaborators {
2628
.map_err(minicbor::encode::Error::message)?,
2729
)?;
2830
for c in &self.0 {
29-
e.str(c)?;
31+
e.bytes(&c.to_string().into_bytes())?;
3032
}
3133
}
3234
Ok(())
@@ -42,16 +44,26 @@ impl minicbor::Decode<'_, ()> for Collaborators {
4244
"Must a definite size array",
4345
));
4446
};
45-
let collabs = (0..items)
46-
.map(|_| Ok(d.str()?.to_string()))
47-
.collect::<Result<_, _>>()?;
48-
Ok(Self(collabs))
47+
48+
(0..items)
49+
.map(|_| d.bytes())
50+
.collect::<Result<Vec<_>, _>>()?
51+
.into_iter()
52+
.map(CatalystId::try_from)
53+
.collect::<Result<_, _>>()
54+
.map(Self)
55+
.map_err(minicbor::decode::Error::custom)
4956
}
5057
}
5158

5259
impl<'de> serde::Deserialize<'de> for Collaborators {
5360
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
5461
where D: serde::Deserializer<'de> {
55-
Ok(Self(Vec::<String>::deserialize(deserializer)?))
62+
Vec::<String>::deserialize(deserializer)?
63+
.into_iter()
64+
.map(|id| CatalystId::from_str(&id))
65+
.collect::<Result<_, _>>()
66+
.map(Self)
67+
.map_err(serde::de::Error::custom)
5668
}
5769
}

rust/signed_doc/src/metadata/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod document_refs;
1212
mod section;
1313
mod supported_field;
1414

15-
use catalyst_types::{problem_report::ProblemReport, uuid::UuidV7};
15+
use catalyst_types::{catalyst_id::CatalystId, problem_report::ProblemReport, uuid::UuidV7};
1616
pub use content_encoding::ContentEncoding;
1717
pub use content_type::ContentType;
1818
pub use doc_type::DocType;
@@ -119,12 +119,12 @@ impl Metadata {
119119
.and_then(SupportedField::try_as_section_ref)
120120
}
121121

122-
/// Return `collabs` field.
122+
/// Return `collaborators` field.
123123
#[must_use]
124-
pub fn collabs(&self) -> &[String] {
124+
pub fn collaborators(&self) -> &[CatalystId] {
125125
self.0
126-
.get(&SupportedLabel::Collabs)
127-
.and_then(SupportedField::try_as_collabs_ref)
126+
.get(&SupportedLabel::Collaborators)
127+
.and_then(SupportedField::try_as_collaborators_ref)
128128
.map_or(&[], |v| &**v)
129129
}
130130

@@ -206,7 +206,7 @@ impl Display for Metadata {
206206
writeln!(f, " template: {:?},", self.template())?;
207207
writeln!(f, " reply: {:?},", self.reply())?;
208208
writeln!(f, " section: {:?},", self.section())?;
209-
writeln!(f, " collabs: {:?},", self.collabs())?;
209+
writeln!(f, " collaborators: {:?},", self.collaborators())?;
210210
writeln!(f, " parameters: {:?},", self.parameters())?;
211211
writeln!(f, " }},")?;
212212
writeln!(f, "}}")

rust/signed_doc/src/metadata/supported_field.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,16 @@ pub(crate) enum SupportedField {
9494
Type(DocType) = 4,
9595
/// `reply` field.
9696
Reply(DocumentRefs) = 5,
97-
/// `collabs` field.
98-
Collabs(Collaborators) = 7,
9997
/// `section` field.
100-
Section(Section) = 8,
98+
Section(Section) = 6,
10199
/// `template` field.
102-
Template(DocumentRefs) = 9,
100+
Template(DocumentRefs) = 7,
103101
/// `parameters` field.
104-
Parameters(DocumentRefs) = 10,
102+
Parameters(DocumentRefs) = 8,
103+
/// `collaborators` field.
104+
Collaborators(Collaborators) = 9,
105105
/// `Content-Encoding` field.
106-
ContentEncoding(ContentEncoding) = 11,
106+
ContentEncoding(ContentEncoding) = 10,
107107
}
108108

109109
impl SupportedLabel {
@@ -117,7 +117,7 @@ impl SupportedLabel {
117117
Label::Str("ver") => Some(Self::Ver),
118118
Label::Str("type") => Some(Self::Type),
119119
Label::Str("reply") => Some(Self::Reply),
120-
Label::Str("collabs") => Some(Self::Collabs),
120+
Label::Str("collaborators") => Some(Self::Collaborators),
121121
Label::Str("section") => Some(Self::Section),
122122
Label::Str("template") => Some(Self::Template),
123123
Label::Str("parameters" | "brand_id" | "campaign_id" | "category_id") => {
@@ -139,7 +139,7 @@ impl SupportedLabel {
139139
Self::Ver => Label::Str("ver"),
140140
Self::Type => Label::Str("type"),
141141
Self::Reply => Label::Str("reply"),
142-
Self::Collabs => Label::Str("collabs"),
142+
Self::Collaborators => Label::Str("collaborators"),
143143
Self::Section => Label::Str("section"),
144144
Self::Template => Label::Str("template"),
145145
Self::Parameters => Label::Str("parameters"),
@@ -167,7 +167,9 @@ impl<'de> serde::de::DeserializeSeed<'de> for SupportedLabel {
167167
SupportedLabel::Ver => Deserialize::deserialize(d).map(SupportedField::Ver),
168168
SupportedLabel::Type => Deserialize::deserialize(d).map(SupportedField::Type),
169169
SupportedLabel::Reply => Deserialize::deserialize(d).map(SupportedField::Reply),
170-
SupportedLabel::Collabs => Deserialize::deserialize(d).map(SupportedField::Collabs),
170+
SupportedLabel::Collaborators => {
171+
Deserialize::deserialize(d).map(SupportedField::Collaborators)
172+
},
171173
SupportedLabel::Section => Deserialize::deserialize(d).map(SupportedField::Section),
172174
SupportedLabel::Template => Deserialize::deserialize(d).map(SupportedField::Template),
173175
SupportedLabel::Parameters => {
@@ -218,7 +220,7 @@ impl minicbor::Decode<'_, crate::decode_context::DecodeContext> for Option<Suppo
218220
},
219221
SupportedLabel::Type => d.decode_with(ctx).map(SupportedField::Type),
220222
SupportedLabel::Reply => d.decode_with(ctx).map(SupportedField::Reply),
221-
SupportedLabel::Collabs => d.decode().map(SupportedField::Collabs),
223+
SupportedLabel::Collaborators => d.decode().map(SupportedField::Collaborators),
222224
SupportedLabel::Section => d.decode().map(SupportedField::Section),
223225
SupportedLabel::Template => d.decode_with(ctx).map(SupportedField::Template),
224226
SupportedLabel::Parameters => d.decode_with(ctx).map(SupportedField::Parameters),
@@ -255,7 +257,7 @@ impl minicbor::Encode<()> for SupportedField {
255257
| SupportedField::Template(document_ref)
256258
| SupportedField::Parameters(document_ref) => document_ref.encode(e, ctx),
257259
SupportedField::Type(doc_type) => doc_type.encode(e, ctx),
258-
SupportedField::Collabs(collabs) => collabs.encode(e, ctx),
260+
SupportedField::Collaborators(collaborators) => collaborators.encode(e, ctx),
259261
SupportedField::Section(section) => section.encode(e, ctx),
260262
SupportedField::ContentEncoding(content_encoding) => content_encoding.encode(e, ctx),
261263
}

rust/signed_doc/tests/decoding.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ fn signed_doc_with_random_header_field_case(field: &'static str) -> TestCase {
236236
anyhow::ensure!(doc.doc_meta().reply().is_none());
237237
anyhow::ensure!(doc.doc_meta().section().is_none());
238238
anyhow::ensure!(doc.doc_meta().parameters().is_none());
239-
anyhow::ensure!(doc.doc_meta().collabs().is_empty());
239+
anyhow::ensure!(doc.doc_meta().collaborators().is_empty());
240240

241241
if field == "content-type" {
242242
anyhow::ensure!(doc.doc_meta().content_type().is_err());
@@ -595,7 +595,12 @@ fn signed_doc_with_complete_metadata_fields_case() -> TestCase {
595595
.encode_with(doc_ref.clone(), &mut ())?;
596596
p_headers.str("section")?.encode("$")?;
597597

598-
p_headers.str("collabs")?.encode(["collaborator 1", "collaborator 2"])?;
598+
/* cspell:disable */
599+
p_headers.str("collaborators")?;
600+
p_headers.array(2)?;
601+
p_headers.bytes(b"cardano/FftxFnOrj2qmTuB2oZG2v0YEWJfKvQ9Gg8AgNAhDsKE")?;
602+
p_headers.bytes(b"id.catalyst://preprod.cardano/FftxFnOrj2qmTuB2oZG2v0YEWJfKvQ9Gg8AgNAhDsKE/7/3")?;
603+
/* cspell:enable */
599604
p_headers.str("parameters")?.encode_with(uuid_v7, &mut catalyst_types::uuid::CborContext::Tagged)?;
600605

601606
e.bytes(p_headers.into_writer().as_slice())?;
@@ -1196,7 +1201,7 @@ fn catalyst_signed_doc_decoding_test() {
11961201
signed_doc_with_random_header_field_case("template"),
11971202
signed_doc_with_random_header_field_case("reply"),
11981203
signed_doc_with_random_header_field_case("section"),
1199-
signed_doc_with_random_header_field_case("collabs"),
1204+
signed_doc_with_random_header_field_case("collaborators"),
12001205
signed_doc_with_random_header_field_case("parameters"),
12011206
signed_doc_with_random_header_field_case("content-encoding"),
12021207
signed_doc_with_parameters_and_aliases_case(&["parameters", "category_id"]),

rust/signed_doc/tests/signature.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ fn metadata() -> serde_json::Value {
2121
"reply": {"id": UuidV7::new(), "ver": UuidV7::new()},
2222
"template": {"id": UuidV7::new(), "ver": UuidV7::new()},
2323
"section": "$",
24-
"collabs": vec!["Alex1", "Alex2"],
24+
"collaborators": vec![
25+
/* cspell:disable */
26+
"cardano/FftxFnOrj2qmTuB2oZG2v0YEWJfKvQ9Gg8AgNAhDsKE",
27+
"id.catalyst://preprod.cardano/FftxFnOrj2qmTuB2oZG2v0YEWJfKvQ9Gg8AgNAhDsKE/7/3"
28+
/* cspell:enable */
29+
],
2530
"parameters": {"id": UuidV7::new(), "ver": UuidV7::new()},
2631
})
2732
}

0 commit comments

Comments
 (0)