use crate::cell::{Cell, CellId};
use crate::error::HexvaultError;
use crate::keys::PartitionKey;
use crate::stack::{Layer, TokenResolver};
use std::sync::Arc;
pub struct Partition {
id: String,
key: PartitionKey,
resolver: Arc<dyn TokenResolver>,
}
impl Partition {
pub(crate) fn new(id: String, key: PartitionKey, resolver: Arc<dyn TokenResolver>) -> Self {
Self { id, key, resolver }
}
pub fn id(&self) -> &str {
&self.id
}
pub(crate) fn key(&self) -> &PartitionKey {
&self.key
}
pub fn create_cell(&self, id: CellId) -> Cell {
Cell::new(id)
}
pub fn seal(
&self,
cell: &mut Cell,
key: &str,
plaintext: &[u8],
layer: Layer,
token: &str,
) -> Result<(), HexvaultError> {
let context = self.resolver.resolve(token)?;
cell.store(&self.key, key, plaintext, layer, &context)
}
pub fn open(&self, cell: &Cell, key: &str, token: &str) -> Result<Vec<u8>, HexvaultError> {
let context = self.resolver.resolve(token)?;
cell.retrieve(&self.key, key, &context)
}
}