(GH-538) Define newtypes for exit_codes fields#1383
Open
michaeltlombardi wants to merge 2 commits intoPowerShell:mainfrom
Open
(GH-538) Define newtypes for exit_codes fields#1383michaeltlombardi wants to merge 2 commits intoPowerShell:mainfrom
exit_codes fields#1383michaeltlombardi wants to merge 2 commits intoPowerShell:mainfrom
Conversation
2bd90b9 to
a6f4a16
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request introduces strongly-typed newtypes for exit codes to improve type safety, provide canonical JSON Schema generation, and eliminate manual type conversion. The PR defines ExitCode (a wrapper around i32) and ExitCodesMap (a wrapper around HashMap<ExitCode, String>), replacing the previous Option<HashMap<String, String>> approach used in resource and extension manifests.
Changes:
- Introduces
ExitCodeandExitCodesMapnewtypes with custom serialization, deserialization, and JSON Schema implementations - Updates
ResourceManifestandExtensionManifestto useExitCodesMapinstead ofOption<HashMap<String, String>> - Removes the
convert_hashmap_string_keys_to_i32()helper function, as conversion now happens automatically during deserialization - Adds comprehensive integration tests for both new types
- Adds localization strings and schema documentation for the exit codes definition
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
lib/dsc-lib/src/types/exit_code.rs |
Defines ExitCode newtype with string-based serialization and i32-based internal representation |
lib/dsc-lib/src/types/exit_codes_map.rs |
Defines ExitCodesMap newtype with custom JSON Schema and helper methods for lookup |
lib/dsc-lib/src/types/mod.rs |
Exports the new types |
lib/dsc-lib/tests/integration/types/exit_code.rs |
Comprehensive integration tests for ExitCode covering methods, serde, and traits |
lib/dsc-lib/tests/integration/types/exit_code_map.rs |
Comprehensive integration tests for ExitCodesMap covering methods, schema, serde, and traits |
lib/dsc-lib/tests/integration/types/mod.rs |
Adds test module declarations |
lib/dsc-lib/src/dscresources/resource_manifest.rs |
Updates exit_codes field to use ExitCodesMap |
lib/dsc-lib/src/extensions/extension_manifest.rs |
Updates exit_codes field to use ExitCodesMap |
lib/dsc-lib/src/dscresources/command_resource.rs |
Updates invoke_command and run_process_async signatures, removes conversion function |
lib/dsc-lib/src/dscerror.rs |
Adds InvalidExitCode error variant |
lib/dsc-lib/locales/schemas.definitions.yaml |
Adds schema documentation for exit codes |
lib/dsc-lib/locales/en-us.toml |
Adds localization strings for exit codes and removes obsolete key |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
a6f4a16 to
f008bdd
Compare
1a481a0 to
ce45413
Compare
ce45413 to
975efde
Compare
Prior to this change, the `exit_codes` fields for the resource and extension manifests defined the map of exit codes to their semantic meanings as `Option<HashMap<String, String>>`. As part of schema canonicalization, we need to provide a canonical JSON Schema for that object. Unfortunately, because we don't own either the `JsonSchema` trait or `HashMap<T>` type, we need to use a newtype wrapper. This change defines two new types: - `ExitCode` as a lightweight wrapper around `i32` where we can control serialization and deserialization with strings as the serialized format instead of an integer. - `ExitCodeMap` as a lightweight wrapper for `HashMap<ExitCode, String>`. It implements a helper function for determining whether the map is empty or default and defines the default map for exit `0` (success) and `1` (error). The `ExitCodeMap` includes a hand-implementation of `JsonSchema` to match the canonical JSON Schema, which `schemars` won't generate from the struct definition. This change not only enables us to provide the canonical JSON Schema for exit code fields in a reusable way, it also removes the need to maintain a converter function for transforming the exit codes map from `HashMap<String, String>` to `HashMap<i32, String>`.
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.
975efde to
d0ff52f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Summary
This change defines two new types:
ExitCodeas a lightweight wrapper aroundi32where we can control serialization and deserialization with strings as the serialized format instead of an integer.ExitCodesMapas a lightweight wrapper forHashMap<ExitCode, String>. It implements a helper function for determining whether the map is empty or default and defines the default map for exit0(success) and1(error).The
ExitCodesMapincludes a hand-implementation ofJsonSchemato match the canonical JSON Schema, whichschemarswon't generate from the struct definition.This change not only enables us to provide the canonical JSON Schema for exit code fields in a reusable way, it also removes the need to maintain a converter function for transforming the exit codes map from
HashMap<String, String>toHashMap<i32, String>.This change also:
ResourceManifestandExtensionManifeststructs to use theExitCodesMapfor theexit_codesfield.run_process_async()andinvoke_command()to use the new type.convert_hashmap_string_keys_to_i32()function, since the exit codes are correctly typed from deserialization/instance creation.PR Context
Prior to this change, the
exit_codesfields for the resource and extension manifests defined the map of exit codes to their semantic meanings asOption<HashMap<String, String>>.As part of schema canonicalization, we need to provide a canonical JSON Schema for that object. Unfortunately, because we don't own either the
JsonSchematrait orHashMap<T>type, we need to use a newtype wrapper.