From ed8d1299cb9488d4c4e35de11631f336caaaf03e Mon Sep 17 00:00:00 2001 From: Eguo Wang Date: Wed, 16 Aug 2023 21:26:18 +0800 Subject: [PATCH] Add Serialize, Deserialize support to devcontainer structures --- src/devcontainer/base.rs | 18 +++++++++++++++--- src/devcontainer/common.rs | 4 ++++ src/devcontainer/compose.rs | 5 +++++ src/devcontainer/container.rs | 6 ++++++ src/devcontainer/lifecycle.rs | 5 +++++ src/devcontainer/mount.rs | 5 +++++ src/devcontainer/ports_attributes.rs | 5 +++++ src/devcontainer/requirements.rs | 5 +++++ src/devcontainer/types.rs | 5 +++++ 9 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/devcontainer/base.rs b/src/devcontainer/base.rs index acb77bf..49e074c 100644 --- a/src/devcontainer/base.rs +++ b/src/devcontainer/base.rs @@ -12,21 +12,33 @@ // See the License for the specific language governing permissions and // limitations under the License. +use serde::{Deserialize, Serialize}; + use super::{ common::DevContainerCommon, compose::{ComposeContainer, NonComposeBase}, container::{DockerfileContainer, ImageContainer}, }; +#[derive(Debug, Serialize, Deserialize)] pub struct DevContainer { - // General properties, common to all. + // ## General properties, common to all. + #[serde(flatten)] pub common: DevContainerCommon, - // Image or Dockerfile specific properties. + // ## Image or Dockerfile specific properties. + // + #[serde(flatten)] pub non_compose_base: Option, + + #[serde(flatten)] pub image_container: Option, + + #[serde(flatten)] pub dockerfile_container: Option, - // Docker Composer specific properties. + // ## Docker Composer specific properties. + // + #[serde(flatten)] pub compose: Option, } diff --git a/src/devcontainer/common.rs b/src/devcontainer/common.rs index 6334431..a7223ad 100644 --- a/src/devcontainer/common.rs +++ b/src/devcontainer/common.rs @@ -17,6 +17,7 @@ use std::collections::HashMap; +use serde::{Deserialize, Serialize}; use serde_json::Value; use super::{ @@ -24,6 +25,7 @@ use super::{ requirements::HostRequirements, types::IntegerOrString, }; +#[derive(Debug, Serialize, Deserialize)] pub struct DevContainerCommon { /// The name for the dev container which can be displayed to the user. pub name: Option, @@ -66,6 +68,7 @@ pub struct DevContainerCommon { pub remote_user: Option, /// When creating or working with a dev container, you may need different /// commands to be run at different points in the container’s lifecycle. + #[serde(flatten)] pub lifecycle_scripts: Option, /// User environment probe to run. The default is "loginInteractiveShell" pub user_env_probe: Option, @@ -75,6 +78,7 @@ pub struct DevContainerCommon { pub additional_properties: Option>, } +#[derive(Debug, Serialize, Deserialize)] pub enum UserEnvProbe { None, LoginShell, diff --git a/src/devcontainer/compose.rs b/src/devcontainer/compose.rs index 844a34a..96b91ed 100644 --- a/src/devcontainer/compose.rs +++ b/src/devcontainer/compose.rs @@ -12,8 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. +use serde::{Deserialize, Serialize}; + use super::types::{IntegerOrStringOrArray, StringOrArray}; +#[derive(Debug, Serialize, Deserialize)] pub struct NonComposeBase { /// Application ports that are exposed by the container. /// This can be a single port or an array of ports. Each port can be a number or a string. @@ -33,6 +36,7 @@ pub struct NonComposeBase { pub workspace_mount: Option, } +#[derive(Debug, Serialize, Deserialize)] pub struct ComposeContainer { /// The name of the docker-compose file(s) used to start the services. pub docker_compose_file: StringOrArray, @@ -52,6 +56,7 @@ pub struct ComposeContainer { pub override_command: Option, } +#[derive(Debug, Serialize, Deserialize)] pub enum ShutdownAction { None, StopContainer, diff --git a/src/devcontainer/container.rs b/src/devcontainer/container.rs index 61d0605..7703417 100644 --- a/src/devcontainer/container.rs +++ b/src/devcontainer/container.rs @@ -14,13 +14,17 @@ use std::collections::HashMap; +use serde::{Deserialize, Serialize}; + use super::types::StringOrArray; +#[derive(Debug, Serialize, Deserialize)] pub struct ImageContainer { /// The docker image that will be used to create the container. pub image: String, } +#[derive(Debug, Serialize, Deserialize)] pub struct DockerfileContainer { /// The location of the Dockerfile that defines the contents of the container. /// The path is relative to the folder containing the `devcontainer.json` file. @@ -29,9 +33,11 @@ pub struct DockerfileContainer { /// The path is relative to the folder containing the `devcontainer.json` file. pub context: Option, /// Docker build-related options. + #[serde(flatten)] pub options: Option, } +#[derive(Debug, Serialize, Deserialize)] pub struct BuildOptions { /// Target stage in a multi-stage build. pub target: Option, diff --git a/src/devcontainer/lifecycle.rs b/src/devcontainer/lifecycle.rs index e251ed0..5f78882 100644 --- a/src/devcontainer/lifecycle.rs +++ b/src/devcontainer/lifecycle.rs @@ -14,8 +14,11 @@ use std::collections::HashMap; +use serde::{Deserialize, Serialize}; + /// When creating or working with a dev container, you may need different /// commands to be run at different points in the container’s lifecycle. +#[derive(Debug, Serialize, Deserialize)] pub struct LifecycleScripts { /// A command to run locally (i.e Your host machine, cloud VM) before anything else. /// This command is run before "onCreateCommand"". If this is a single string, it will be run in a shell. @@ -54,6 +57,7 @@ pub struct LifecycleScripts { pub wait_for: Option, } +#[derive(Debug, Serialize, Deserialize)] pub enum LifecycleScript { String(String), Array(Vec), @@ -61,6 +65,7 @@ pub enum LifecycleScript { } #[allow(clippy::enum_variant_names)] +#[derive(Debug, Serialize, Deserialize)] pub enum WaitFor { InitializeCommand, OnCreateCommand, diff --git a/src/devcontainer/mount.rs b/src/devcontainer/mount.rs index 506bf84..912fc13 100644 --- a/src/devcontainer/mount.rs +++ b/src/devcontainer/mount.rs @@ -12,12 +12,16 @@ // See the License for the specific language governing permissions and // limitations under the License. +use serde::{Deserialize, Serialize}; + /// A mount can be a bind mount or a volume mount. +#[derive(Debug, Serialize, Deserialize)] pub enum StringOrMount { String(String), Mount(Mount), } +#[derive(Debug, Serialize, Deserialize)] pub struct Mount { /// Mount type. pub kind: MountKind, @@ -28,6 +32,7 @@ pub struct Mount { } /// Mount type. +#[derive(Debug, Serialize, Deserialize)] pub enum MountKind { Bind, Volume, diff --git a/src/devcontainer/ports_attributes.rs b/src/devcontainer/ports_attributes.rs index 0148a67..d16083c 100644 --- a/src/devcontainer/ports_attributes.rs +++ b/src/devcontainer/ports_attributes.rs @@ -12,8 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. +use serde::{Deserialize, Serialize}; + /// The PortAttributes properties allow you to map default port options /// for one or more manually or automatically forwarded ports. +#[derive(Debug, Serialize, Deserialize)] pub struct PortAttributes { /// Defines the action that occurs when the port is discovered for automatic forwarding pub on_auto_forward: Option, @@ -42,6 +45,7 @@ impl Default for PortAttributes { } /// Defines the action that occurs when the port is discovered for automatic forwarding +#[derive(Debug, Serialize, Deserialize)] pub enum OnAutoForward { /// Shows a notification when a port is automatically forwarded. // TODO: default @@ -62,6 +66,7 @@ pub enum OnAutoForward { } /// The Protocol to use when forwarding a port. +#[derive(Debug, Serialize, Deserialize)] pub enum Protocol { Http, Https, diff --git a/src/devcontainer/requirements.rs b/src/devcontainer/requirements.rs index 9a05e81..af4e91d 100644 --- a/src/devcontainer/requirements.rs +++ b/src/devcontainer/requirements.rs @@ -12,7 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +use serde::{Deserialize, Serialize}; + /// Host hardware requirements. +#[derive(Debug, Serialize, Deserialize)] pub struct HostRequirements { /// Number of required CPUs. minimum: 1 pub cpus: Option, @@ -28,6 +31,7 @@ pub struct HostRequirements { pub gpu: Option, } +#[derive(Debug, Serialize, Deserialize)] pub enum GPUVar { /// Indicates whether a GPU is required. Boolean(bool), @@ -37,6 +41,7 @@ pub enum GPUVar { Config(GPUConfig), } +#[derive(Debug, Serialize, Deserialize)] pub struct GPUConfig { /// Number of required cores. minimum: 1 pub cores: Option, diff --git a/src/devcontainer/types.rs b/src/devcontainer/types.rs index 63b4ccd..8c76578 100644 --- a/src/devcontainer/types.rs +++ b/src/devcontainer/types.rs @@ -12,16 +12,21 @@ // See the License for the specific language governing permissions and // limitations under the License. +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] pub enum IntegerOrString { Integer(i32), String(String), } +#[derive(Debug, Serialize, Deserialize)] pub enum StringOrArray { String(String), Array(Vec), } +#[derive(Debug, Serialize, Deserialize)] pub enum IntegerOrStringOrArray { Integer(u32), String(String),