Skip to content

Commit

Permalink
Add Serialize, Deserialize support to devcontainer structures
Browse files Browse the repository at this point in the history
  • Loading branch information
wangeguo committed Aug 16, 2023
1 parent 2f65e9b commit ed8d129
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/devcontainer/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NonComposeBase>,

#[serde(flatten)]
pub image_container: Option<ImageContainer>,

#[serde(flatten)]
pub dockerfile_container: Option<DockerfileContainer>,

// Docker Composer specific properties.
// ## Docker Composer specific properties.
//
#[serde(flatten)]
pub compose: Option<ComposeContainer>,
}
4 changes: 4 additions & 0 deletions src/devcontainer/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::{
lifecycle::LifecycleScripts, mount::StringOrMount, ports_attributes::PortAttributes,
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<String>,
Expand Down Expand Up @@ -66,6 +68,7 @@ pub struct DevContainerCommon {
pub remote_user: Option<String>,
/// 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<LifecycleScripts>,
/// User environment probe to run. The default is "loginInteractiveShell"
pub user_env_probe: Option<UserEnvProbe>,
Expand All @@ -75,6 +78,7 @@ pub struct DevContainerCommon {
pub additional_properties: Option<HashMap<String, Value>>,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum UserEnvProbe {
None,
LoginShell,
Expand Down
5 changes: 5 additions & 0 deletions src/devcontainer/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -33,6 +36,7 @@ pub struct NonComposeBase {
pub workspace_mount: Option<String>,
}

#[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,
Expand All @@ -52,6 +56,7 @@ pub struct ComposeContainer {
pub override_command: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum ShutdownAction {
None,
StopContainer,
Expand Down
6 changes: 6 additions & 0 deletions src/devcontainer/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -29,9 +33,11 @@ pub struct DockerfileContainer {
/// The path is relative to the folder containing the `devcontainer.json` file.
pub context: Option<String>,
/// Docker build-related options.
#[serde(flatten)]
pub options: Option<BuildOptions>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct BuildOptions {
/// Target stage in a multi-stage build.
pub target: Option<String>,
Expand Down
5 changes: 5 additions & 0 deletions src/devcontainer/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -54,13 +57,15 @@ pub struct LifecycleScripts {
pub wait_for: Option<WaitFor>,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum LifecycleScript {
String(String),
Array(Vec<String>),
Object(HashMap<String, String>),
}

#[allow(clippy::enum_variant_names)]
#[derive(Debug, Serialize, Deserialize)]
pub enum WaitFor {
InitializeCommand,
OnCreateCommand,
Expand Down
5 changes: 5 additions & 0 deletions src/devcontainer/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -28,6 +32,7 @@ pub struct Mount {
}

/// Mount type.
#[derive(Debug, Serialize, Deserialize)]
pub enum MountKind {
Bind,
Volume,
Expand Down
5 changes: 5 additions & 0 deletions src/devcontainer/ports_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OnAutoForward>,
Expand Down Expand Up @@ -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
Expand All @@ -62,6 +66,7 @@ pub enum OnAutoForward {
}

/// The Protocol to use when forwarding a port.
#[derive(Debug, Serialize, Deserialize)]
pub enum Protocol {
Http,
Https,
Expand Down
5 changes: 5 additions & 0 deletions src/devcontainer/requirements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>,
Expand All @@ -28,6 +31,7 @@ pub struct HostRequirements {
pub gpu: Option<GPUVar>,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum GPUVar {
/// Indicates whether a GPU is required.
Boolean(bool),
Expand All @@ -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<u32>,
Expand Down
5 changes: 5 additions & 0 deletions src/devcontainer/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>),
}

#[derive(Debug, Serialize, Deserialize)]
pub enum IntegerOrStringOrArray {
Integer(u32),
String(String),
Expand Down

0 comments on commit ed8d129

Please sign in to comment.