Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_mut for fields of spec/runtime #166

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ reorder_imports = true
reorder_modules = true
remove_nested_parens = true
match_arm_leading_pipes = "Never"
fn_args_layout = "Tall"
fn_params_layout = "Tall"
edition = "2018"
merge_derives = true
use_try_shorthand = false
Expand Down
18 changes: 12 additions & 6 deletions src/image/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer}
use std::collections::BTreeMap;
use std::{
collections::HashMap,
fmt::Display,
io::{Read, Write},
path::Path,
};
Expand Down Expand Up @@ -250,14 +251,19 @@ impl ImageConfiguration {
}
}

/// Implement `ToString` directly since we cannot avoid twice memory allocation
/// when using auto-implementaion through `Display`.
impl ToString for ImageConfiguration {
fn to_string(&self) -> String {
/// This ToString trait is automatically implemented for any type which implements the Display trait.
/// As such, ToString shouldn’t be implemented directly: Display should be implemented instead,
/// and you get the ToString implementation for free.
impl Display for ImageConfiguration {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Serde seralization never fails since this is
// a combination of String and enums.
self.to_string_pretty()
.expect("ImageConfiguration JSON convertion failed")
write!(
f,
"{}",
self.to_string_pretty()
.expect("ImageConfiguration JSON convertion failed")
)
}
}

Expand Down
18 changes: 12 additions & 6 deletions src/image/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use getset::{CopyGetters, Getters, Setters};
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
fmt::Display,
io::{Read, Write},
path::Path,
};
Expand Down Expand Up @@ -210,14 +211,19 @@ impl Default for ImageIndex {
}
}

/// Implement `ToString` directly since we cannot avoid twice memory allocation
/// when using auto-implementaion through `Display`.
impl ToString for ImageIndex {
fn to_string(&self) -> String {
/// This ToString trait is automatically implemented for any type which implements the Display trait.
/// As such, ToString shouldn’t be implemented directly: Display should be implemented instead,
/// and you get the ToString implementation for free.
impl Display for ImageIndex {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Serde seralization never fails since this is
// a combination of String and enums.
self.to_string_pretty()
.expect("ImageIndex to JSON convertion failed")
write!(
f,
"{}",
self.to_string_pretty()
.expect("ImageIndex to JSON convertion failed")
)
}
}

Expand Down
18 changes: 12 additions & 6 deletions src/image/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use getset::{CopyGetters, Getters, MutGetters, Setters};
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
fmt::Display,
io::{Read, Write},
path::Path,
};
Expand Down Expand Up @@ -221,14 +222,19 @@ impl ImageManifest {
}
}

/// Implement `ToString` directly since we cannot avoid twice memory allocation
/// when using auto-implementaion through `Display`.
impl ToString for ImageManifest {
fn to_string(&self) -> String {
/// This ToString trait is automatically implemented for any type which implements the Display trait.
/// As such, ToString shouldn’t be implemented directly: Display should be implemented instead,
/// and you get the ToString implementation for free.
impl Display for ImageManifest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Serde seralization never fails since this is
// a combination of String and enums.
self.to_string_pretty()
.expect("ImageManifest to JSON convertion failed")
write!(
f,
"{}",
self.to_string_pretty()
.expect("ImageManifest to JSON convertion failed")
)
}
}

Expand Down
25 changes: 18 additions & 7 deletions src/runtime/hooks.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
use crate::error::OciSpecError;
use derive_builder::Builder;
use getset::{CopyGetters, Getters, Setters};
use getset::{CopyGetters, Getters, MutGetters, Setters};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(
Builder, Clone, Debug, Default, Deserialize, Eq, Getters, Setters, PartialEq, Serialize,
Builder,
Clone,
Debug,
Default,
Deserialize,
Eq,
MutGetters,
Getters,
Setters,
PartialEq,
Serialize,
)]
#[serde(rename_all = "camelCase")]
#[builder(
Expand All @@ -14,7 +24,7 @@ use std::path::PathBuf;
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
#[getset(get = "pub", set = "pub")]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// Hooks specifies a command that is run in the container at a particular
/// event in the lifecycle (setup and teardown) of a container.
pub struct Hooks {
Expand Down Expand Up @@ -75,6 +85,7 @@ pub struct Hooks {
Deserialize,
Eq,
Getters,
MutGetters,
Setters,
PartialEq,
Serialize,
Expand All @@ -88,28 +99,28 @@ pub struct Hooks {
/// Hook specifies a command that is run at a particular event in the
/// lifecycle of a container.
pub struct Hook {
#[getset(get = "pub", set = "pub")]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// Path to the binary to be executed. Following similar semantics to
/// [IEEE Std 1003.1-2008 `execv`'s path](https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html). This
/// specification extends the IEEE standard in that path MUST be
/// absolute.
path: PathBuf,

#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get = "pub", set = "pub")]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// Arguments used for the binary, including the binary name itself.
/// Following the same semantics as [IEEE Std 1003.1-2008
/// `execv`'s argv](https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html).
args: Option<Vec<String>>,

#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get = "pub", set = "pub")]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// Additional `key=value` environment variables. Following the same
/// semantics as [IEEE Std 1003.1-2008's `environ`](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_01).
env: Option<Vec<String>>,

#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
#[getset(get_mut = "pub", get_copy = "pub", set = "pub")]
/// Timeout is the number of seconds before aborting the hook. If set,
/// timeout MUST be greater than zero.
timeout: Option<i64>,
Expand Down
Loading
Loading