oci_spec/
lib.rs

1#![deny(missing_docs, warnings)]
2#![doc = include_str!("../README.md")]
3#![allow(clippy::too_long_first_doc_paragraph)]
4
5#[cfg(feature = "distribution")]
6pub mod distribution;
7mod error;
8#[cfg(feature = "image")]
9pub mod image;
10#[cfg(feature = "runtime")]
11pub mod runtime;
12
13use std::{
14    fs::{self, OpenOptions},
15    io::{Read, Write},
16    path::Path,
17};
18
19use serde::{de::DeserializeOwned, Serialize};
20
21pub use error::*;
22
23fn from_file<P: AsRef<Path>, T: DeserializeOwned>(path: P) -> Result<T> {
24    let path = path.as_ref();
25    let manifest_file = std::io::BufReader::new(fs::File::open(path)?);
26    let manifest = serde_json::from_reader(manifest_file)?;
27    Ok(manifest)
28}
29
30fn from_reader<R: Read, T: DeserializeOwned>(reader: R) -> Result<T> {
31    let manifest = serde_json::from_reader(reader)?;
32    Ok(manifest)
33}
34
35fn to_file<P: AsRef<Path>, T: Serialize>(item: &T, path: P, pretty: bool) -> Result<()> {
36    let path = path.as_ref();
37    let file = OpenOptions::new()
38        .write(true)
39        .create(true)
40        .truncate(true)
41        .open(path)?;
42    let file = std::io::BufWriter::new(file);
43
44    match pretty {
45        true => serde_json::to_writer_pretty(file, item)?,
46        false => serde_json::to_writer(file, item)?,
47    }
48
49    Ok(())
50}
51
52fn to_writer<W: Write, T: Serialize>(item: &T, writer: &mut W, pretty: bool) -> Result<()> {
53    match pretty {
54        true => serde_json::to_writer_pretty(writer, item)?,
55        false => serde_json::to_writer(writer, item)?,
56    }
57
58    Ok(())
59}
60
61fn to_string<T: Serialize>(item: &T, pretty: bool) -> Result<String> {
62    Ok(match pretty {
63        true => serde_json::to_string_pretty(item)?,
64        false => serde_json::to_string(item)?,
65    })
66}