Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions dsc_lib/src/configure/config_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ impl DscRepoSchema for Configuration {
}

fn validate_schema_uri(&self) -> Result<(), DscError> {
match Self::is_recognized_schema_uri(&self.schema) {
true => Ok(()),
false => Err(DscError::UnrecognizedSchemaUri(
if Self::is_recognized_schema_uri(&self.schema) {
Ok(())
} else {
Err(DscError::UnrecognizedSchemaUri(
self.schema.clone(),
Self::recognized_schema_uris(),
))
Expand Down
7 changes: 4 additions & 3 deletions dsc_lib/src/dscresources/resource_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,10 @@ impl DscRepoSchema for ResourceManifest {
}

fn validate_schema_uri(&self) -> Result<(), DscError> {
match Self::is_recognized_schema_uri(&self.schema_version) {
true => Ok(()),
false => Err(DscError::UnrecognizedSchemaUri(
if Self::is_recognized_schema_uri(&self.schema_version) {
Ok(())
} else {
Err(DscError::UnrecognizedSchemaUri(
self.schema_version.clone(),
Self::recognized_schema_uris(),
))
Expand Down
77 changes: 41 additions & 36 deletions dsc_lib/src/schemas/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ pub enum SchemaUriPrefix {
impl std::fmt::Display for SchemaUriPrefix {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::AkaDotMs => write!(f, "{}", "https://aka.ms/dsc/schemas"),
Self::Github => write!(f, "{}", "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas"),
Self::AkaDotMs => write!(f, "https://aka.ms/dsc/schemas"),
Self::Github => write!(f, "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas"),
}
}
}

impl SchemaUriPrefix {
/// Returns every known URI prefix for convenient iteration.
pub fn all() -> Vec<SchemaUriPrefix> {
#[must_use] pub fn all() -> Vec<SchemaUriPrefix> {
vec![
Self::AkaDotMs,
Self::Github,
Expand Down Expand Up @@ -66,11 +66,10 @@ impl SchemaForm {
/// The extension for [`Bundled`] and [`Canonical`] schemas is `.json`
///
/// The extension for [`VSCode`] schemas is `.vscode.json`
pub fn to_extension(&self) -> String {
#[must_use] pub fn to_extension(&self) -> String {
match self {
Self::Bundled => ".json".to_string(),
Self::Bundled | Self::Canonical => ".json".to_string(),
Self::VSCode => ".vscode.json".to_string(),
Self::Canonical => ".json".to_string(),
}
}

Expand All @@ -79,16 +78,15 @@ impl SchemaForm {
/// The [`Bundled`] and [`VSCode`] schemas are always published in the `bundled` folder
/// immediately beneath the version folder. The [`Canonical`] schemas use the folder path
/// as defined for that schema.
pub fn to_folder_prefix(&self) -> String {
#[must_use] pub fn to_folder_prefix(&self) -> String {
match self {
Self::Bundled => "bundled/".to_string(),
Self::VSCode => "bundled/".to_string(),
Self::Canonical => "".to_string(),
Self::Bundled | Self::VSCode => "bundled/".to_string(),
Self::Canonical => String::new(),
}
}

/// Returns every schema form for convenient iteration.
pub fn all() -> Vec<SchemaForm> {
#[must_use] pub fn all() -> Vec<SchemaForm> {
vec![
Self::Bundled,
Self::VSCode,
Expand Down Expand Up @@ -137,16 +135,16 @@ pub enum RecognizedSchemaVersion {
impl std::fmt::Display for RecognizedSchemaVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::V3 => write!(f, "{}", "v3"),
Self::V3_0 => write!(f, "{}", "v3.0"),
Self::V3_0_0 => write!(f, "{}", "v3.0.0"),
Self::V3 => write!(f, "v3"),
Self::V3_0 => write!(f, "v3.0"),
Self::V3_0_0 => write!(f, "v3.0.0"),
}
}
}

impl RecognizedSchemaVersion {
/// Returns every recognized schema version for convenient iteration.
pub fn all() -> Vec<RecognizedSchemaVersion> {
#[must_use] pub fn all() -> Vec<RecognizedSchemaVersion> {
vec![
Self::V3,
Self::V3_0,
Expand All @@ -155,17 +153,17 @@ impl RecognizedSchemaVersion {
}

//// Returns the latest version with major, minor, and patch segments, like `3.0.0`.
pub fn latest() -> RecognizedSchemaVersion {
#[must_use] pub fn latest() -> RecognizedSchemaVersion {
Self::V3_0_0
}

/// Returns the latest minor version for the latest major version, like `3.0`.
pub fn latest_minor() -> RecognizedSchemaVersion {
#[must_use] pub fn latest_minor() -> RecognizedSchemaVersion {
Self::V3_0
}

/// Returns the latest major version, like `3`
pub fn latest_major() -> RecognizedSchemaVersion {
#[must_use] pub fn latest_major() -> RecognizedSchemaVersion {
Self::V3
}
}
Expand Down Expand Up @@ -224,9 +222,10 @@ pub(crate) fn get_recognized_schema_uris(
should_bundle: bool
) -> Vec<String> {
let mut uris: Vec<String> = Vec::new();
let schema_forms = match should_bundle {
true => SchemaForm::all(),
false => vec![SchemaForm::Canonical],
let schema_forms = if should_bundle {
SchemaForm::all()
} else {
vec![SchemaForm::Canonical]
};
for uri_prefix in SchemaUriPrefix::all() {
for schema_form in schema_forms.iter().copied() {
Expand All @@ -244,15 +243,15 @@ pub(crate) fn get_recognized_schema_uris(
}
}

return uris.into()
uris
}

/// Returns the JSON Schema to validate that a `$schema` keyword for a DSC type is one of the
/// recognized URIs.
///
/// This is a convenience function used by the [`DscRepoSchema`] trait. It's not intended for
/// direct use.`
pub(crate) fn get_recognized_uris_subschema(
/// direct use.
#[must_use] pub(crate) fn get_recognized_uris_subschema(
metadata: Metadata,
schema_file_base_name: &str,
schema_folder_path: &str,
Expand Down Expand Up @@ -317,9 +316,10 @@ pub(crate) fn get_default_schema_uri(
/// If a schema is published in bundled form, the bundled form is the default. Otherwise, the
/// default form is canonical (non-bundled).
fn get_default_schema_form(should_bundle: bool) -> SchemaForm {
match should_bundle {
false => SchemaForm::Canonical,
true => SchemaForm::Bundled,
if should_bundle {
SchemaForm::Bundled
} else {
SchemaForm::Canonical
}
}

Expand Down Expand Up @@ -352,7 +352,7 @@ pub trait DscRepoSchema : JsonSchema {
/// default when creating an instance is the latest major version of the schema with the
/// `aka.ms` prefix. If the schema is published in the bundled form, the default is for the
/// bundled schema. Otherwise, the default is for the canonical (non-bundled) schema.
fn default_schema_id_uri() -> String {
#[must_use] fn default_schema_id_uri() -> String {
get_default_schema_uri(
Self::SCHEMA_FILE_BASE_NAME,
Self::SCHEMA_FOLDER_PATH,
Expand All @@ -361,7 +361,7 @@ pub trait DscRepoSchema : JsonSchema {
}

/// Returns the schema URI for a given version, form, and prefix.
fn get_schema_id_uri(
#[must_use] fn get_schema_id_uri(
schema_version: RecognizedSchemaVersion,
schema_form: SchemaForm,
uri_prefix: SchemaUriPrefix
Expand All @@ -379,7 +379,7 @@ pub trait DscRepoSchema : JsonSchema {
/// version.
///
/// If the type isn't published in bundled form, this function returns `None`.
fn get_enhanced_schema_id_uri(schema_version: RecognizedSchemaVersion) -> Option<String> {
#[must_use] fn get_enhanced_schema_id_uri(schema_version: RecognizedSchemaVersion) -> Option<String> {
if !Self::SCHEMA_SHOULD_BUNDLE {
return None;
}
Expand All @@ -395,7 +395,7 @@ pub trait DscRepoSchema : JsonSchema {

/// Returns the URI for the canonical (non-bundled) form of the schema with the default
/// prefix for a given version.
fn get_canonical_schema_id_uri(schema_version: RecognizedSchemaVersion) -> String {
#[must_use] fn get_canonical_schema_id_uri(schema_version: RecognizedSchemaVersion) -> String {
get_recognized_schema_uri(
Self::SCHEMA_FILE_BASE_NAME,
Self::SCHEMA_FOLDER_PATH,
Expand All @@ -407,7 +407,7 @@ pub trait DscRepoSchema : JsonSchema {

/// Returns the URI for the bundled form of the schema with the default prefix for a given
/// version.
fn get_bundled_schema_id_uri(schema_version: RecognizedSchemaVersion) -> Option<String> {
#[must_use] fn get_bundled_schema_id_uri(schema_version: RecognizedSchemaVersion) -> Option<String> {
if !Self::SCHEMA_SHOULD_BUNDLE {
return None;
}
Expand All @@ -426,7 +426,7 @@ pub trait DscRepoSchema : JsonSchema {
/// This convenience function generates a vector containing every recognized JSON Schema `$id`
/// URI for a specific schema. It handles returning the schemas for every recognized prefix,
/// version, and form.
fn recognized_schema_uris() -> Vec<String> {
#[must_use] fn recognized_schema_uris() -> Vec<String> {
get_recognized_schema_uris(
Self::SCHEMA_FILE_BASE_NAME,
Self::SCHEMA_FOLDER_PATH,
Expand All @@ -441,7 +441,7 @@ pub trait DscRepoSchema : JsonSchema {
/// recognized and validated. This method generates the appropriate subschema with every
/// valid URI for the schema's `$id` without needing to regularly update an enum for each
/// schema and release.
fn recognized_schema_uris_subschema(_: &mut schemars::gen::SchemaGenerator) -> Schema {
#[must_use] fn recognized_schema_uris_subschema(_: &mut schemars::gen::SchemaGenerator) -> Schema {
get_recognized_uris_subschema(
Self::schema_metadata(),
Self::SCHEMA_FILE_BASE_NAME,
Expand All @@ -451,7 +451,7 @@ pub trait DscRepoSchema : JsonSchema {
}

/// Indicates whether a given string is a recognized shema URI.
fn is_recognized_schema_uri(uri: &String) -> bool {
#[must_use] fn is_recognized_schema_uri(uri: &String) -> bool {
Self::recognized_schema_uris().contains(uri)
}

Expand All @@ -465,7 +465,12 @@ pub trait DscRepoSchema : JsonSchema {
/// types that don't define the `$schema` keyword in their serialized form.
///
/// Any DSC type that serializes with the `$schema` keyword **must** define this
/// method to actually validate the instance.
/// method to actually validate the instance.
///
/// # Errors
///
/// If the value for the schema field isn't a recognized schema, the method should raise the
/// [`DscError::UnrecognizedSchemaUri`] error.
fn validate_schema_uri(&self) -> Result<(), DscError> {
Ok(())
}
Expand Down