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

Implement ToString for ImageIndex, ImageManifest, and ImageConfiguration #109

Merged
merged 2 commits into from
Jul 28, 2022
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
56 changes: 55 additions & 1 deletion src/image/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{Arch, Os};
use crate::{
error::{OciSpecError, Result},
from_file, from_reader, to_file, to_writer,
from_file, from_reader, to_file, to_string, to_writer,
};
use derive_builder::Builder;
use getset::{CopyGetters, Getters, MutGetters, Setters};
Expand Down Expand Up @@ -189,6 +189,47 @@ impl ImageConfiguration {
pub fn to_writer_pretty<W: Write>(&self, writer: &mut W) -> Result<()> {
to_writer(&self, writer, true)
}

/// Attempts to write an image configuration to a string as JSON.
/// # Errors
/// This function will return an [OciSpecError::SerDe](crate::OciSpecError::SerDe) if
/// the image configuration cannot be serialized.
/// # Example
/// ``` no_run
/// use oci_spec::image::ImageConfiguration;
///
/// let image_configuration = ImageConfiguration::from_file("config.json").unwrap();
/// let json_str = image_configuration.to_string().unwrap();
/// ```
pub fn to_string(&self) -> Result<String> {
to_string(&self, false)
}

/// Attempts to write an image configuration to a string as pretty printed JSON.
/// # Errors
/// This function will return an [OciSpecError::SerDe](crate::OciSpecError::SerDe) if
/// the image configuration cannot be serialized.
/// # Example
/// ``` no_run
/// use oci_spec::image::ImageConfiguration;
///
/// let image_configuration = ImageConfiguration::from_file("config.json").unwrap();
/// let json_str = image_configuration.to_string_pretty().unwrap();
/// ```
pub fn to_string_pretty(&self) -> Result<String> {
to_string(&self, true)
}
}

/// 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 {
// Serde seralization never fails since this is
// a combination of String and enums.
self.to_string_pretty()
.expect("ImageConfiguration JSON convertion failed")
}
}

#[derive(
Expand Down Expand Up @@ -541,4 +582,17 @@ mod tests {
let expected = fs::read(get_config_path()).expect("read expected");
assert_eq!(actual, expected);
}

#[test]
fn save_config_to_string() {
// arrange
let config = create_config();

// act
let actual = config.to_string_pretty().expect("to string");

// assert
let expected = fs::read_to_string(get_config_path()).expect("read expected");
assert_eq!(actual, expected);
}
}
56 changes: 55 additions & 1 deletion src/image/index.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{Descriptor, MediaType};
use crate::{
error::{OciSpecError, Result},
from_file, from_reader, to_file, to_writer,
from_file, from_reader, to_file, to_string, to_writer,
};
use derive_builder::Builder;
use getset::{CopyGetters, Getters, Setters};
Expand Down Expand Up @@ -152,6 +152,36 @@ impl ImageIndex {
pub fn to_writer_pretty<W: Write>(&self, writer: &mut W) -> Result<()> {
to_writer(&self, writer, true)
}

/// Attempts to write an image index to a string as JSON.
/// # Errors
/// This function will return an [OciSpecError::SerDe](crate::OciSpecError::SerDe) if
/// the image configuration cannot be serialized.
/// # Example
/// ``` no_run
/// use oci_spec::image::ImageIndex;
///
/// let image_index = ImageIndex::from_file("index.json").unwrap();
/// let json_str = image_index.to_string().unwrap();
/// ```
pub fn to_string(&self) -> Result<String> {
to_string(&self, false)
}

/// Attempts to write an image index to a string as pretty printed JSON.
/// # Errors
/// This function will return an [OciSpecError::SerDe](crate::OciSpecError::SerDe) if
/// the image configuration cannot be serialized.
/// # Example
/// ``` no_run
/// use oci_spec::image::ImageIndex;
///
/// let image_index = ImageIndex::from_file("index.json").unwrap();
/// let json_str = image_index.to_string_pretty().unwrap();
/// ```
pub fn to_string_pretty(&self) -> Result<String> {
to_string(&self, true)
}
}

impl Default for ImageIndex {
Expand All @@ -165,6 +195,17 @@ 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 {
// Serde seralization never fails since this is
// a combination of String and enums.
self.to_string_pretty()
.expect("ImageIndex to JSON convertion failed")
}
}

#[cfg(test)]
mod tests {
use std::{fs, path::PathBuf};
Expand Down Expand Up @@ -273,4 +314,17 @@ mod tests {
let expected = fs::read(get_index_path()).expect("read expected");
assert_eq!(actual, expected);
}

#[test]
fn save_index_to_string() {
// arrange
let index = create_index();

// act
let actual = index.to_string_pretty().expect("to string");

// assert
let expected = fs::read_to_string(get_index_path()).expect("read expected");
assert_eq!(actual, expected);
}
}
56 changes: 55 additions & 1 deletion src/image/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{Descriptor, MediaType};
use crate::{
error::{OciSpecError, Result},
from_file, from_reader, to_file, to_writer,
from_file, from_reader, to_file, to_string, to_writer,
};
use derive_builder::Builder;
use getset::{CopyGetters, Getters, MutGetters, Setters};
Expand Down Expand Up @@ -174,6 +174,47 @@ impl ImageManifest {
pub fn to_writer_pretty<W: Write>(&self, writer: &mut W) -> Result<()> {
to_writer(&self, writer, true)
}

/// Attempts to write an image manifest to a string as JSON.
/// # Errors
/// This function will return an [OciSpecError::SerDe](crate::OciSpecError::SerDe) if
/// the image configuration cannot be serialized.
/// # Example
/// ``` no_run
/// use oci_spec::image::ImageManifest;
///
/// let image_manifest = ImageManifest::from_file("manifest.json").unwrap();
/// let json_str = image_manifest.to_string().unwrap();
/// ```
pub fn to_string(&self) -> Result<String> {
to_string(&self, false)
}

/// Attempts to write an image manifest to a string as pretty printed JSON.
/// # Errors
/// This function will return an [OciSpecError::SerDe](crate::OciSpecError::SerDe) if
/// the image configuration cannot be serialized.
/// # Example
/// ``` no_run
/// use oci_spec::image::ImageManifest;
///
/// let image_manifest = ImageManifest::from_file("manifest.json").unwrap();
/// let json_str = image_manifest.to_string_pretty().unwrap();
/// ```
pub fn to_string_pretty(&self) -> Result<String> {
to_string(&self, true)
}
}

/// 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 {
// Serde seralization never fails since this is
// a combination of String and enums.
self.to_string_pretty()
.expect("ImageManifest to JSON convertion failed")
}
}

#[cfg(test)]
Expand Down Expand Up @@ -299,4 +340,17 @@ mod tests {
let expected = fs::read(get_manifest_path()).expect("read expected");
assert_eq!(actual, expected);
}

#[test]
fn save_manifest_to_string() {
// arrange
let manifest = create_manifest();

// act
let actual = manifest.to_string_pretty().expect("to string");

// assert
let expected = fs::read_to_string(get_manifest_path()).expect("read expected");
assert_eq!(actual, expected);
}
}
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,10 @@ fn to_writer<W: Write, T: Serialize>(item: &T, writer: &mut W, pretty: bool) ->

Ok(())
}

fn to_string<T: Serialize>(item: &T, pretty: bool) -> Result<String> {
Ok(match pretty {
true => serde_json::to_string_pretty(item)?,
false => serde_json::to_string(item)?,
})
}