Skip to content

Commit 2bd90b9

Browse files
(GH-538) Update exit_codes field to use ExitCodesMap.
This change updates the `exit_codes` fields for the `ResourceManifest` and `ExtensionManifest` structs to use the new `ExitCodesMap` type instead of `Option<HashMap<String, String>>`. It also updates the `run_process_async()` and `invoke_command()` functions to work with references to `ExitCodesMap` and removes the now-extraneous `fn convert_hashmap_string_keys_to_i32()` function.
1 parent 5ba6843 commit 2bd90b9

File tree

3 files changed

+12
-33
lines changed

3 files changed

+12
-33
lines changed

lib/dsc-lib/src/dscresources/command_resource.rs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rust_i18n::t;
77
use serde::Deserialize;
88
use serde_json::{Map, Value};
99
use std::{collections::HashMap, env, path::{Path, PathBuf}, process::Stdio};
10-
use crate::{configure::{config_doc::ExecutionKind, config_result::{ResourceGetResult, ResourceTestResult}}, types::FullyQualifiedTypeName, util::canonicalize_which};
10+
use crate::{configure::{config_doc::ExecutionKind, config_result::{ResourceGetResult, ResourceTestResult}}, types::{ExitCodesMap, FullyQualifiedTypeName}, util::canonicalize_which};
1111
use crate::dscerror::DscError;
1212
use super::{dscresource::{get_diff, redact, DscResource}, invoke_result::{ExportResult, GetResult, ResolveResult, SetResult, TestResult, ValidateResult, ResourceGetResponse, ResourceSetResponse, ResourceTestResponse, get_in_desired_state}, resource_manifest::{GetArgKind, SetDeleteArgKind, InputKind, Kind, ReturnKind, SchemaKind}};
1313
use tracing::{error, warn, info, debug, trace};
@@ -733,7 +733,7 @@ pub fn invoke_resolve(resource: &DscResource, input: &str) -> Result<ResolveResu
733733
///
734734
/// Error is returned if the command fails to execute or stdin/stdout/stderr cannot be opened.
735735
///
736-
async fn run_process_async(executable: &str, args: Option<Vec<String>>, input: Option<&str>, cwd: Option<&Path>, env: Option<HashMap<String, String>>, exit_codes: Option<&HashMap<i32, String>>) -> Result<(i32, String, String), DscError> {
736+
async fn run_process_async(executable: &str, args: Option<Vec<String>>, input: Option<&str>, cwd: Option<&Path>, env: Option<HashMap<String, String>>, exit_codes: &ExitCodesMap) -> Result<(i32, String, String), DscError> {
737737

738738
// use somewhat large initial buffer to avoid early string reallocations;
739739
// the value is based on list result of largest of built-in adapters - WMI adapter ~500KB
@@ -823,10 +823,8 @@ async fn run_process_async(executable: &str, args: Option<Vec<String>>, input: O
823823
debug!("{}", t!("dscresources.commandResource.processChildExit", executable = executable, id = child_id, code = code));
824824

825825
if code != 0 {
826-
if let Some(exit_codes) = exit_codes {
827-
if let Some(error_message) = exit_codes.get(&code) {
828-
return Err(DscError::CommandExitFromManifest(executable.to_string(), code, error_message.to_string()));
829-
}
826+
if let Some(error_message) = exit_codes.get_code(code) {
827+
return Err(DscError::CommandExitFromManifest(executable.to_string(), code, error_message.clone()));
830828
}
831829
return Err(DscError::Command(executable.to_string(), code, stderr_result));
832830
}
@@ -838,22 +836,6 @@ async fn run_process_async(executable: &str, args: Option<Vec<String>>, input: O
838836
}
839837
}
840838

841-
fn convert_hashmap_string_keys_to_i32(input: Option<&HashMap<String, String>>) -> Result<Option<HashMap<i32, String>>, DscError> {
842-
if input.is_none() {
843-
return Ok(None);
844-
}
845-
846-
let mut output: HashMap<i32, String> = HashMap::new();
847-
for (key, value) in input.unwrap() {
848-
if let Ok(key_int) = key.parse::<i32>() {
849-
output.insert(key_int, value.clone());
850-
} else {
851-
return Err(DscError::NotSupported(t!("util.invalidExitCodeKey", key = key).to_string()));
852-
}
853-
}
854-
Ok(Some(output))
855-
}
856-
857839
/// Invoke a command and return the exit code, stdout, and stderr.
858840
///
859841
/// # Arguments
@@ -874,8 +856,7 @@ fn convert_hashmap_string_keys_to_i32(input: Option<&HashMap<String, String>>) -
874856
/// Will panic if tokio runtime can't be created.
875857
///
876858
#[allow(clippy::implicit_hasher)]
877-
pub fn invoke_command(executable: &str, args: Option<Vec<String>>, input: Option<&str>, cwd: Option<&Path>, env: Option<HashMap<String, String>>, exit_codes: Option<&HashMap<String, String>>) -> Result<(i32, String, String), DscError> {
878-
let exit_codes = convert_hashmap_string_keys_to_i32(exit_codes)?;
859+
pub fn invoke_command(executable: &str, args: Option<Vec<String>>, input: Option<&str>, cwd: Option<&Path>, env: Option<HashMap<String, String>>, exit_codes: &ExitCodesMap) -> Result<(i32, String, String), DscError> {
879860
let executable = canonicalize_which(executable, cwd)?;
880861

881862
let run_async = async {
@@ -884,7 +865,7 @@ pub fn invoke_command(executable: &str, args: Option<Vec<String>>, input: Option
884865
trace!("{}", t!("dscresources.commandResource.commandCwd", cwd = cwd.display()));
885866
}
886867

887-
match run_process_async(&executable, args, input, cwd, env, exit_codes.as_ref()).await {
868+
match run_process_async(&executable, args, input, cwd, env, exit_codes).await {
888869
Ok((code, stdout, stderr)) => {
889870
Ok((code, stdout, stderr))
890871
},

lib/dsc-lib/src/dscresources/resource_manifest.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ use schemars::JsonSchema;
66
use semver::Version;
77
use serde::{Deserialize, Serialize};
88
use serde_json::{Map, Value};
9-
use std::collections::HashMap;
109

1110
use crate::{
1211
schemas::{dsc_repo::DscRepoSchema, transforms::idiomaticize_string_enum},
13-
types::FullyQualifiedTypeName,
12+
types::{ExitCodesMap, FullyQualifiedTypeName},
1413
};
1514

1615
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, DscRepoSchema)]
@@ -84,8 +83,8 @@ pub struct ResourceManifest {
8483
#[serde(skip_serializing_if = "Option::is_none")]
8584
pub adapter: Option<Adapter>,
8685
/// Mapping of exit codes to descriptions. Zero is always success and non-zero is always failure.
87-
#[serde(rename = "exitCodes", skip_serializing_if = "Option::is_none")]
88-
pub exit_codes: Option<HashMap<String, String>>, // we have to make this a string key instead of i32 due to https://github.com/serde-rs/json/issues/560
86+
#[serde(rename = "exitCodes", skip_serializing_if = "ExitCodesMap::is_empty_or_default", default)]
87+
pub exit_codes: ExitCodesMap,
8988
/// Details how to get the schema of the resource.
9089
#[serde(skip_serializing_if = "Option::is_none")]
9190
pub schema: Option<SchemaKind>,

lib/dsc-lib/src/extensions/extension_manifest.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ use schemars::JsonSchema;
66
use semver::Version;
77
use serde::{Deserialize, Serialize};
88
use serde_json::{Map, Value};
9-
use std::collections::HashMap;
109

1110
use crate::dscerror::DscError;
1211
use crate::extensions::{discover::DiscoverMethod, import::ImportMethod, secret::SecretMethod};
1312
use crate::schemas::dsc_repo::DscRepoSchema;
14-
use crate::types::FullyQualifiedTypeName;
13+
use crate::types::{ExitCodesMap, FullyQualifiedTypeName};
1514

1615
#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize, JsonSchema, DscRepoSchema)]
1716
#[serde(deny_unknown_fields)]
@@ -52,8 +51,8 @@ pub struct ExtensionManifest {
5251
/// Details how to call the Secret method of the extension.
5352
pub secret: Option<SecretMethod>,
5453
/// Mapping of exit codes to descriptions. Zero is always success and non-zero is always failure.
55-
#[serde(rename = "exitCodes", skip_serializing_if = "Option::is_none")]
56-
pub exit_codes: Option<HashMap<String, String>>,
54+
#[serde(rename = "exitCodes", skip_serializing_if = "ExitCodesMap::is_empty_or_default", default)]
55+
pub exit_codes: ExitCodesMap,
5756
#[serde(skip_serializing_if = "Option::is_none")]
5857
pub metadata: Option<Map<String, Value>>,
5958
}

0 commit comments

Comments
 (0)