Skip to content

Commit 7fd6944

Browse files
committed
Prepare for auto-generation of extension.json
1 parent f0baed1 commit 7fd6944

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

docs/extension-manifest-schema.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@
176176
"minLength": 1
177177
},
178178
"values": {
179-
"$ref": "#/definitions/ArgNumberOfValues"
179+
"default": 1,
180+
"allOf": [
181+
{
182+
"$ref": "#/definitions/ArgNumberOfValues"
183+
}
184+
]
180185
}
181186
},
182187
"additionalProperties": false

src/dfx-core/src/extension/manifest/extension.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::error::extension::{
44
};
55
use crate::json::structure::VersionReqWithJsonSchema;
66
use schemars::JsonSchema;
7-
use serde::{Deserialize, Deserializer};
7+
use serde::{Deserialize, Deserializer, Serialize, Serializer};
88
use serde_json::Value;
99
use std::path::PathBuf;
1010
use std::{
@@ -17,7 +17,7 @@ pub static MANIFEST_FILE_NAME: &str = "extension.json";
1717
type SubcmdName = String;
1818
type ArgName = String;
1919

20-
#[derive(Debug, Deserialize, JsonSchema)]
20+
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
2121
#[serde(deny_unknown_fields)]
2222
pub struct ExtensionManifest {
2323
pub name: String,
@@ -33,7 +33,7 @@ pub struct ExtensionManifest {
3333
pub canister_type: Option<ExtensionCanisterType>,
3434
}
3535

36-
#[derive(Debug, Deserialize, JsonSchema)]
36+
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
3737
#[serde(untagged)]
3838
pub enum ExtensionDependency {
3939
/// A SemVer version requirement, for example ">=0.17.0".
@@ -71,7 +71,7 @@ impl ExtensionManifest {
7171
}
7272
}
7373

74-
#[derive(Debug, Deserialize, JsonSchema)]
74+
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
7575
pub struct ExtensionCanisterType {
7676
/// If one field depends on another and both specify a handlebars expression,
7777
/// list the fields in the order that they should be evaluated.
@@ -88,18 +88,18 @@ pub struct ExtensionCanisterType {
8888
pub defaults: BTreeMap<String, Value>,
8989
}
9090

91-
#[derive(Debug, Deserialize, Default, JsonSchema)]
92-
pub struct ExtensionSubcommandsOpts(BTreeMap<SubcmdName, ExtensionSubcommandOpts>);
91+
#[derive(Debug, Serialize, Deserialize, Default, JsonSchema)]
92+
pub struct ExtensionSubcommandsOpts(pub BTreeMap<SubcmdName, ExtensionSubcommandOpts>);
9393

94-
#[derive(Debug, Deserialize, JsonSchema)]
94+
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
9595
#[serde(deny_unknown_fields)]
9696
pub struct ExtensionSubcommandOpts {
9797
pub about: Option<String>,
9898
pub args: Option<BTreeMap<ArgName, ExtensionSubcommandArgOpts>>,
9999
pub subcommands: Option<ExtensionSubcommandsOpts>,
100100
}
101101

102-
#[derive(Debug, Deserialize, JsonSchema)]
102+
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
103103
#[serde(deny_unknown_fields)]
104104
pub struct ExtensionSubcommandArgOpts {
105105
pub about: Option<String>,
@@ -167,6 +167,22 @@ impl<'de> Deserialize<'de> for ArgNumberOfValues {
167167
}
168168
}
169169

170+
impl Serialize for ArgNumberOfValues {
171+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
172+
where
173+
S: Serializer,
174+
{
175+
match self {
176+
Self::Number(n) => serializer.serialize_u64(*n as u64),
177+
Self::Unlimited => serializer.serialize_str("unlimited"),
178+
Self::Range(range) => {
179+
let s = format!("{}..{}", range.start, range.end - 1);
180+
serializer.serialize_str(&s)
181+
}
182+
}
183+
}
184+
}
185+
170186
impl ExtensionSubcommandArgOpts {
171187
pub fn into_clap_arg(
172188
self,

src/dfx-core/src/json/structure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ where
5656
}
5757
}
5858

59-
#[derive(Debug, Deserialize, JsonSchema)]
59+
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
6060
#[serde(transparent)]
6161
pub struct VersionReqWithJsonSchema(#[schemars(with = "String")] pub VersionReq);
6262

0 commit comments

Comments
 (0)