use crate::{
error::{OciSpecError, Result},
from_file, from_reader, to_file, to_string, to_writer,
};
use derive_builder::Builder;
use getset::{Getters, Setters};
use serde::{Deserialize, Serialize};
use std::{
io::{Read, Write},
path::Path,
};
#[derive(Builder, Clone, Debug, Deserialize, Eq, Getters, Setters, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
#[builder(
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
pub struct OciLayout {
#[getset(get = "pub", set = "pub")]
image_layout_version: String,
}
impl OciLayout {
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<OciLayout> {
from_file(path)
}
pub fn from_reader<R: Read>(reader: R) -> Result<OciLayout> {
from_reader(reader)
}
pub fn to_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
to_file(&self, path, false)
}
pub fn to_file_pretty<P: AsRef<Path>>(&self, path: P) -> Result<()> {
to_file(&self, path, true)
}
pub fn to_writer<W: Write>(&self, writer: &mut W) -> Result<()> {
to_writer(&self, writer, false)
}
pub fn to_writer_pretty<W: Write>(&self, writer: &mut W) -> Result<()> {
to_writer(&self, writer, true)
}
pub fn to_string(&self) -> Result<String> {
to_string(&self, false)
}
pub fn to_string_pretty(&self) -> Result<String> {
to_string(&self, true)
}
}
#[cfg(test)]
mod tests {
use std::{fs, path::PathBuf};
use super::*;
fn create_oci_layout() -> OciLayout {
OciLayoutBuilder::default()
.image_layout_version("lorem ipsum")
.build()
.expect("build oci layout")
}
fn get_oci_layout_path() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test/data/oci-layout")
}
#[test]
fn load_oci_layout_from_file() {
let oci_layout_path = get_oci_layout_path();
let actual = OciLayout::from_file(oci_layout_path).expect("from file");
let expected = create_oci_layout();
assert_eq!(actual, expected);
}
#[test]
fn load_oci_layout_from_reader() {
let reader = fs::read(get_oci_layout_path()).expect("read oci-layout");
let actual = OciLayout::from_reader(&*reader).expect("from reader");
let expected = create_oci_layout();
assert_eq!(actual, expected);
}
#[test]
fn save_oci_layout_to_file() {
let tmp = std::env::temp_dir().join("save_oci_layout_to_file");
fs::create_dir_all(&tmp).expect("create test directory");
let oci_layout = create_oci_layout();
let oci_layout_path = tmp.join("oci-layout");
oci_layout
.to_file_pretty(&oci_layout_path)
.expect("write oci-layout to file");
let actual = fs::read_to_string(oci_layout_path).expect("read actual");
let expected = fs::read_to_string(get_oci_layout_path()).expect("read expected");
assert_eq!(actual, expected);
}
#[test]
fn save_oci_layout_to_writer() {
let mut actual = Vec::new();
let oci_layout = create_oci_layout();
oci_layout.to_writer_pretty(&mut actual).expect("to writer");
let expected = fs::read(get_oci_layout_path()).expect("read expected");
assert_eq!(actual, expected);
}
#[test]
fn save_oci_layout_to_string() {
let oci_layout = create_oci_layout();
let actual = oci_layout.to_string_pretty().expect("to string");
let expected = fs::read_to_string(get_oci_layout_path()).expect("read expected");
assert_eq!(actual, expected);
}
}