Skip to content

Commit 92c9cdf

Browse files
committed
wip
1 parent f131bde commit 92c9cdf

File tree

6 files changed

+51
-24
lines changed

6 files changed

+51
-24
lines changed

rust/catalyst-signed-doc-spec/src/metadata/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! `metadata` field definition
22
33
pub mod doc_ref;
4+
pub mod parameters;
45
pub mod reply;
56
pub mod template;
67

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//! `signed_doc.json` "parameters" field JSON definition
2+
3+
use std::ops::Deref;
4+
5+
use crate::metadata::doc_ref::Ref;
6+
7+
/// `signed_doc.json` "parameters" field JSON object
8+
#[derive(serde::Deserialize)]
9+
pub struct Parameters(pub Ref);
10+
11+
impl Deref for Parameters {
12+
type Target = Ref;
13+
14+
fn deref(&self) -> &Self::Target {
15+
&self.0
16+
}
17+
}
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
//! `signed_doc.json` "template" field JSON definition
22
3-
use crate::{is_required::IsRequired, DocumentName};
3+
use std::ops::Deref;
4+
5+
use crate::metadata::doc_ref::Ref;
46

57
/// `signed_doc.json` "template" field JSON object
68
#[derive(serde::Deserialize)]
7-
#[allow(clippy::missing_docs_in_private_items, dead_code)]
8-
pub struct Template {
9-
pub required: IsRequired,
10-
#[serde(rename = "type")]
11-
pub doc_type: Option<DocumentName>,
12-
pub multiple: Option<bool>,
9+
pub struct Template(pub Ref);
10+
11+
impl Deref for Template {
12+
type Target = Ref;
13+
14+
fn deref(&self) -> &Self::Target {
15+
&self.0
16+
}
1317
}

rust/signed_doc/src/validator/rules/doc_ref.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,23 @@ impl RefRule {
2828
/// Generating `RefRule` from specs
2929
pub(crate) fn new(
3030
docs: &HashMap<catalyst_signed_doc_spec::DocumentName, catalyst_signed_doc_spec::DocSpec>,
31-
ref_spec: &catalyst_signed_doc_spec::metadata::doc_ref::Ref,
31+
spec: &catalyst_signed_doc_spec::metadata::doc_ref::Ref,
3232
) -> anyhow::Result<Self> {
33-
let optional = match ref_spec.required {
33+
let optional = match spec.required {
3434
catalyst_signed_doc_spec::is_required::IsRequired::Yes => false,
3535
catalyst_signed_doc_spec::is_required::IsRequired::Optional => true,
3636
catalyst_signed_doc_spec::is_required::IsRequired::Excluded => {
37+
anyhow::ensure!(
38+
spec.doc_type.is_empty() && spec.multiple.is_none(),
39+
"'type' and 'multiple' fields could not been specified when 'required' is 'excluded' for 'ref' metadata definition"
40+
);
3741
return Ok(Self::NotSpecified);
3842
},
3943
};
4044

41-
anyhow::ensure!(!ref_spec.doc_type.is_empty(), "'type' field should exists and has at least one entry for the required 'ref' metadata definition");
45+
anyhow::ensure!(!spec.doc_type.is_empty(), "'type' field should exists and has at least one entry for the required 'ref' metadata definition");
4246

43-
let exp_ref_types = ref_spec.doc_type.iter().try_fold(
47+
let exp_ref_types = spec.doc_type.iter().try_fold(
4448
Vec::new(),
4549
|mut res, doc_name| -> anyhow::Result<_> {
4650
let docs_spec = docs.get(doc_name).ok_or(anyhow::anyhow!(
@@ -51,7 +55,7 @@ impl RefRule {
5155
},
5256
)?;
5357

54-
let multiple = ref_spec.multiple.ok_or(anyhow::anyhow!(
58+
let multiple = spec.multiple.ok_or(anyhow::anyhow!(
5559
"'multiple' field should exists for the required 'ref' metadata definition"
5660
))?;
5761

rust/signed_doc/src/validator/rules/reply.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,21 @@ impl ReplyRule {
3535
IsRequired::Yes => false,
3636
IsRequired::Optional => true,
3737
IsRequired::Excluded => {
38+
anyhow::ensure!(
39+
spec.doc_type.is_empty() && spec.multiple.is_none(),
40+
"'type' and 'multiple' fields could not been specified when 'required' is 'excluded' for 'template' metadata definition"
41+
);
3842
return Ok(Self::NotSpecified);
3943
},
4044
};
4145

42-
anyhow::ensure!(spec.doc_type.len() == 1, "'type' field should exists and has only one entry for the required 'reply' metadata definition");
4346
anyhow::ensure!(
4447
spec.multiple.is_some_and(|v| !v),
4548
"'multiple' field should be only set to false for the required 'reply' metadata definition"
4649
);
4750

48-
let doc_name = spec.doc_type.first().ok_or(anyhow::anyhow!("'type' field should exists and has only one entry for the required 'reply' metadata definition"))?;
49-
let docs_spec = docs.get(doc_name).ok_or(anyhow::anyhow!(
51+
let doc_name = &<&[DocumentName; 1]>::try_from(spec.doc_type.as_slice()).map_err(|_| anyhow::anyhow!("'type' field should exists and has only one entry for the required 'reply' metadata definition"))?[0];
52+
let docs_spec = docs.get(&doc_name).ok_or(anyhow::anyhow!(
5053
"cannot find a document definition {doc_name}"
5154
))?;
5255
let allowed_type = docs_spec.doc_type.as_str().parse()?;

rust/signed_doc/src/validator/rules/template.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,23 @@ impl TemplateRule {
3535
) -> anyhow::Result<Self> {
3636
if let IsRequired::Excluded = spec.required {
3737
anyhow::ensure!(
38-
spec.doc_type.is_none() && spec.multiple.is_none(),
39-
"'type' and 'multiple' fields could not been specified when 'required' is 'excluded' for 'template' metadata definition"
40-
);
38+
spec.doc_type.is_empty() && spec.multiple.is_none(),
39+
"'type' and 'multiple' fields could not been specified when 'required' is 'excluded' for 'template' metadata definition"
40+
);
4141
return Ok(Self::NotSpecified);
4242
}
4343

4444
anyhow::ensure!(
4545
spec.multiple.is_some_and(|v| !v),
46-
"'multiple' must be `false` for 'template' metadata definition"
46+
"'multiple' field should be only set to false for the required 'reply' metadata definition"
4747
);
4848
anyhow::ensure!(
4949
spec.required != IsRequired::Optional,
50-
"'required' field cannot been 'optional' for 'template' metadata definition"
50+
"'required' field cannot been 'optional' for 'template' metadata definition"
5151
);
5252

53-
let doc_name = spec.doc_type.as_ref().ok_or(anyhow::anyhow!(
54-
"'type' field should exists for the required 'template' metadata definition"
55-
))?;
56-
let docs_spec = docs.get(doc_name).ok_or(anyhow::anyhow!(
53+
let doc_name = &<&[DocumentName; 1]>::try_from(spec.doc_type.as_slice()).map_err(|_| anyhow::anyhow!("'type' field should exists and has only one entry for the required 'reply' metadata definition"))?[0];
54+
let docs_spec = docs.get(&doc_name).ok_or(anyhow::anyhow!(
5755
"cannot find a document definition {doc_name}"
5856
))?;
5957
let allowed_type = docs_spec.doc_type.as_str().parse()?;

0 commit comments

Comments
 (0)