ocipkg 0.4.0

OCI registry for package distribution
Documentation
use anyhow::Result;
use oci_spec::image::Digest;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, path::PathBuf};

/// The contents of `application/vnd.ocipkg.v1.config+json` media type.
///
/// This is a map from the layer digest to the list of relative paths of the files in the layer.
///
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Config {
    layers: HashMap<Digest, Vec<PathBuf>>,
}

impl Config {
    pub fn to_json(&self) -> Result<String> {
        Ok(serde_json::to_string(self)?)
    }

    pub fn from_slice(slice: &[u8]) -> Result<Self> {
        Ok(serde_json::from_slice(slice)?)
    }

    pub fn add_layer(&mut self, digest: Digest, paths: Vec<PathBuf>) {
        self.layers.insert(digest, paths);
    }

    pub fn layers(&self) -> &HashMap<Digest, Vec<PathBuf>> {
        &self.layers
    }
}