1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+
1
4
//! Contains helpers for JSON schemas and DSC
2
5
3
6
use schemars:: { schema:: { Metadata , Schema } , JsonSchema } ;
@@ -27,7 +30,8 @@ impl std::fmt::Display for SchemaUriPrefix {
27
30
28
31
impl SchemaUriPrefix {
29
32
/// Returns every known URI prefix for convenient iteration.
30
- #[ must_use] pub fn all ( ) -> Vec < SchemaUriPrefix > {
33
+ #[ must_use]
34
+ pub fn all ( ) -> Vec < SchemaUriPrefix > {
31
35
vec ! [
32
36
Self :: AkaDotMs ,
33
37
Self :: Github ,
@@ -66,7 +70,8 @@ impl SchemaForm {
66
70
/// The extension for [`Bundled`] and [`Canonical`] schemas is `.json`
67
71
///
68
72
/// The extension for [`VSCode`] schemas is `.vscode.json`
69
- #[ must_use] pub fn to_extension ( & self ) -> String {
73
+ #[ must_use]
74
+ pub fn to_extension ( & self ) -> String {
70
75
match self {
71
76
Self :: Bundled | Self :: Canonical => ".json" . to_string ( ) ,
72
77
Self :: VSCode => ".vscode.json" . to_string ( ) ,
@@ -78,15 +83,17 @@ impl SchemaForm {
78
83
/// The [`Bundled`] and [`VSCode`] schemas are always published in the `bundled` folder
79
84
/// immediately beneath the version folder. The [`Canonical`] schemas use the folder path
80
85
/// as defined for that schema.
81
- #[ must_use] pub fn to_folder_prefix ( & self ) -> String {
86
+ #[ must_use]
87
+ pub fn to_folder_prefix ( & self ) -> String {
82
88
match self {
83
89
Self :: Bundled | Self :: VSCode => "bundled/" . to_string ( ) ,
84
90
Self :: Canonical => String :: new ( ) ,
85
91
}
86
92
}
87
93
88
94
/// Returns every schema form for convenient iteration.
89
- #[ must_use] pub fn all ( ) -> Vec < SchemaForm > {
95
+ #[ must_use]
96
+ pub fn all ( ) -> Vec < SchemaForm > {
90
97
vec ! [
91
98
Self :: Bundled ,
92
99
Self :: VSCode ,
@@ -144,7 +151,8 @@ impl std::fmt::Display for RecognizedSchemaVersion {
144
151
145
152
impl RecognizedSchemaVersion {
146
153
/// Returns every recognized schema version for convenient iteration.
147
- #[ must_use] pub fn all ( ) -> Vec < RecognizedSchemaVersion > {
154
+ #[ must_use]
155
+ pub fn all ( ) -> Vec < RecognizedSchemaVersion > {
148
156
vec ! [
149
157
Self :: V3 ,
150
158
Self :: V3_0 ,
@@ -153,17 +161,20 @@ impl RecognizedSchemaVersion {
153
161
}
154
162
155
163
//// Returns the latest version with major, minor, and patch segments, like `3.0.0`.
156
- #[ must_use] pub fn latest ( ) -> RecognizedSchemaVersion {
164
+ #[ must_use]
165
+ pub fn latest ( ) -> RecognizedSchemaVersion {
157
166
Self :: V3_0_0
158
167
}
159
168
160
169
/// Returns the latest minor version for the latest major version, like `3.0`.
161
- #[ must_use] pub fn latest_minor ( ) -> RecognizedSchemaVersion {
170
+ #[ must_use]
171
+ pub fn latest_minor ( ) -> RecognizedSchemaVersion {
162
172
Self :: V3_0
163
173
}
164
174
165
175
/// Returns the latest major version, like `3`
166
- #[ must_use] pub fn latest_major ( ) -> RecognizedSchemaVersion {
176
+ #[ must_use]
177
+ pub fn latest_major ( ) -> RecognizedSchemaVersion {
167
178
Self :: V3
168
179
}
169
180
}
@@ -251,7 +262,8 @@ pub(crate) fn get_recognized_schema_uris(
251
262
///
252
263
/// This is a convenience function used by the [`DscRepoSchema`] trait. It's not intended for
253
264
/// direct use.
254
- #[ must_use] pub ( crate ) fn get_recognized_uris_subschema (
265
+ #[ must_use]
266
+ pub ( crate ) fn get_recognized_uris_subschema (
255
267
metadata : Metadata ,
256
268
schema_file_base_name : & str ,
257
269
schema_folder_path : & str ,
@@ -352,7 +364,8 @@ pub trait DscRepoSchema : JsonSchema {
352
364
/// default when creating an instance is the latest major version of the schema with the
353
365
/// `aka.ms` prefix. If the schema is published in the bundled form, the default is for the
354
366
/// bundled schema. Otherwise, the default is for the canonical (non-bundled) schema.
355
- #[ must_use] fn default_schema_id_uri ( ) -> String {
367
+ #[ must_use]
368
+ fn default_schema_id_uri ( ) -> String {
356
369
get_default_schema_uri (
357
370
Self :: SCHEMA_FILE_BASE_NAME ,
358
371
Self :: SCHEMA_FOLDER_PATH ,
@@ -361,7 +374,8 @@ pub trait DscRepoSchema : JsonSchema {
361
374
}
362
375
363
376
/// Returns the schema URI for a given version, form, and prefix.
364
- #[ must_use] fn get_schema_id_uri (
377
+ #[ must_use]
378
+ fn get_schema_id_uri (
365
379
schema_version : RecognizedSchemaVersion ,
366
380
schema_form : SchemaForm ,
367
381
uri_prefix : SchemaUriPrefix
@@ -379,7 +393,8 @@ pub trait DscRepoSchema : JsonSchema {
379
393
/// version.
380
394
///
381
395
/// If the type isn't published in bundled form, this function returns `None`.
382
- #[ must_use] fn get_enhanced_schema_id_uri ( schema_version : RecognizedSchemaVersion ) -> Option < String > {
396
+ #[ must_use]
397
+ fn get_enhanced_schema_id_uri ( schema_version : RecognizedSchemaVersion ) -> Option < String > {
383
398
if !Self :: SCHEMA_SHOULD_BUNDLE {
384
399
return None ;
385
400
}
@@ -395,7 +410,8 @@ pub trait DscRepoSchema : JsonSchema {
395
410
396
411
/// Returns the URI for the canonical (non-bundled) form of the schema with the default
397
412
/// prefix for a given version.
398
- #[ must_use] fn get_canonical_schema_id_uri ( schema_version : RecognizedSchemaVersion ) -> String {
413
+ #[ must_use]
414
+ fn get_canonical_schema_id_uri ( schema_version : RecognizedSchemaVersion ) -> String {
399
415
get_recognized_schema_uri (
400
416
Self :: SCHEMA_FILE_BASE_NAME ,
401
417
Self :: SCHEMA_FOLDER_PATH ,
@@ -407,7 +423,8 @@ pub trait DscRepoSchema : JsonSchema {
407
423
408
424
/// Returns the URI for the bundled form of the schema with the default prefix for a given
409
425
/// version.
410
- #[ must_use] fn get_bundled_schema_id_uri ( schema_version : RecognizedSchemaVersion ) -> Option < String > {
426
+ #[ must_use]
427
+ fn get_bundled_schema_id_uri ( schema_version : RecognizedSchemaVersion ) -> Option < String > {
411
428
if !Self :: SCHEMA_SHOULD_BUNDLE {
412
429
return None ;
413
430
}
@@ -426,7 +443,8 @@ pub trait DscRepoSchema : JsonSchema {
426
443
/// This convenience function generates a vector containing every recognized JSON Schema `$id`
427
444
/// URI for a specific schema. It handles returning the schemas for every recognized prefix,
428
445
/// version, and form.
429
- #[ must_use] fn recognized_schema_uris ( ) -> Vec < String > {
446
+ #[ must_use]
447
+ fn recognized_schema_uris ( ) -> Vec < String > {
430
448
get_recognized_schema_uris (
431
449
Self :: SCHEMA_FILE_BASE_NAME ,
432
450
Self :: SCHEMA_FOLDER_PATH ,
@@ -441,7 +459,8 @@ pub trait DscRepoSchema : JsonSchema {
441
459
/// recognized and validated. This method generates the appropriate subschema with every
442
460
/// valid URI for the schema's `$id` without needing to regularly update an enum for each
443
461
/// schema and release.
444
- #[ must_use] fn recognized_schema_uris_subschema ( _: & mut schemars:: gen:: SchemaGenerator ) -> Schema {
462
+ #[ must_use]
463
+ fn recognized_schema_uris_subschema ( _: & mut schemars:: gen:: SchemaGenerator ) -> Schema {
445
464
get_recognized_uris_subschema (
446
465
Self :: schema_metadata ( ) ,
447
466
Self :: SCHEMA_FILE_BASE_NAME ,
@@ -451,7 +470,8 @@ pub trait DscRepoSchema : JsonSchema {
451
470
}
452
471
453
472
/// Indicates whether a given string is a recognized shema URI.
454
- #[ must_use] fn is_recognized_schema_uri ( uri : & String ) -> bool {
473
+ #[ must_use]
474
+ fn is_recognized_schema_uri ( uri : & String ) -> bool {
455
475
Self :: recognized_schema_uris ( ) . contains ( uri)
456
476
}
457
477
@@ -476,7 +496,7 @@ pub trait DscRepoSchema : JsonSchema {
476
496
}
477
497
}
478
498
479
- #[ allow ( unused_imports ) ]
499
+ #[ cfg ( test ) ]
480
500
mod test {
481
501
use serde:: { Deserialize , Serialize } ;
482
502
0 commit comments