pub mod portable;
mod registry;
mod scope;
use crate::containers::element_field::{ElementField, SubElementField};
use crate::containers::evolution::{Evolution, SubEvolution};
use crate::containers::finite_element_space::{FiniteElementSpace, SubFiniteElementSpace};
use crate::containers::matrix::{Matrix, SubMatrix};
use crate::containers::mesh::{Mesh, SubMesh};
use crate::containers::model::{Model, SubModel};
use crate::containers::node_field::{NodeField, SubNodeField};
use crate::coords::Coords;
use crate::error::{PyrucastError, Result};
use crate::handle::Handle;
pub use portable::Portable;
use serde::{Deserialize, Serialize};
use std::any::Any;
use std::collections::BTreeMap;
use std::path::Path;
const MAGIC: &[u8; 8] = b"PYRUCAST";
const FORMAT: u32 = 5;
pub trait Archivable: Portable + Any + Send + Sync {
const TAG: &'static str;
fn on_load(&mut self) {}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub enum Value {
Bool(bool),
Int(i64),
Float(f64),
Text(String),
Bools(Vec<bool>),
Ints(Vec<i64>),
Floats(Vec<f64>),
Texts(Vec<String>),
}
pub enum Root {
Coords(Handle<Coords>),
Mesh(Mesh),
SubMesh(Handle<SubMesh>),
FiniteElementSpace(FiniteElementSpace),
SubFiniteElementSpace(Handle<SubFiniteElementSpace>),
NodeField(NodeField),
SubNodeField(Handle<SubNodeField>),
ElementField(ElementField),
SubElementField(Handle<SubElementField>),
Evolution(Evolution),
SubEvolution(Handle<SubEvolution>),
Model(Model),
SubModel(Handle<SubModel>),
Matrix(Matrix),
SubMatrix(Handle<SubMatrix>),
Value(Value),
}
impl Root {
pub fn type_name(&self) -> &'static str {
match self {
Root::Coords(_) => Coords::TAG,
Root::Mesh(_) => Mesh::TAG,
Root::SubMesh(_) => SubMesh::TAG,
Root::FiniteElementSpace(_) => FiniteElementSpace::TAG,
Root::SubFiniteElementSpace(_) => SubFiniteElementSpace::TAG,
Root::NodeField(_) => NodeField::TAG,
Root::SubNodeField(_) => SubNodeField::TAG,
Root::ElementField(_) => ElementField::TAG,
Root::SubElementField(_) => SubElementField::TAG,
Root::Evolution(_) => Evolution::TAG,
Root::SubEvolution(_) => SubEvolution::TAG,
Root::Model(_) => Model::TAG,
Root::SubModel(_) => SubModel::TAG,
Root::Matrix(_) => Matrix::TAG,
Root::SubMatrix(_) => SubMatrix::TAG,
Root::Value(v) => match v {
Value::Bool(_) => "bool",
Value::Int(_) => "int",
Value::Float(_) => "float",
Value::Text(_) => "str",
Value::Bools(_) => "list[bool]",
Value::Ints(_) => "list[int]",
Value::Floats(_) => "list[float]",
Value::Texts(_) => "list[str]",
},
}
}
}
impl std::fmt::Debug for Root {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Root::Coords(h) => write!(f, "{h}"),
Root::SubMesh(h) => write!(f, "{h}"),
Root::SubFiniteElementSpace(h) => write!(f, "{h}"),
Root::SubNodeField(h) => write!(f, "{h}"),
Root::SubElementField(h) => write!(f, "{h}"),
Root::SubEvolution(h) => write!(f, "{h}"),
Root::SubModel(h) => write!(f, "{h}"),
Root::SubMatrix(h) => write!(f, "{h}"),
Root::Value(v) => write!(f, "{v:?}"),
other => write!(f, "<{}>", other.type_name()),
}
}
}
#[derive(Default, Debug)]
pub struct Objects(BTreeMap<String, Root>);
impl std::ops::Deref for Objects {
type Target = BTreeMap<String, Root>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Objects {
pub fn into_inner(self) -> BTreeMap<String, Root> {
self.0
}
fn wrong(&self, key: &str, expected: &str) -> PyrucastError {
match self.0.get(key) {
Some(r) => PyrucastError::Message(format!(
"archive: \"{key}\" is a {}, not a {expected}",
r.type_name()
)),
None => PyrucastError::Message(format!(
"archive: no entry named \"{key}\" (available: {})",
self.0.keys().cloned().collect::<Vec<_>>().join(", ")
)),
}
}
}
macro_rules! accessors {
($($name:ident, $variant:ident, $ty:ty, $label:literal;)*) => {
impl Objects {
$(
#[doc = concat!("The `", $label, "` named `key`, or an error naming what was found instead.")]
pub fn $name(&self, key: &str) -> Result<$ty> {
match self.0.get(key) {
Some(Root::$variant(v)) => Ok(v.clone()),
_ => Err(self.wrong(key, $label)),
}
}
)*
}
};
}
accessors! {
coords, Coords, Handle<Coords>, "Coords";
submesh, SubMesh, Handle<SubMesh>, "SubMesh";
subfespace, SubFiniteElementSpace, Handle<SubFiniteElementSpace>, "SubFiniteElementSpace";
sub_node_field, SubNodeField, Handle<SubNodeField>, "SubNodeField";
sub_element_field, SubElementField, Handle<SubElementField>, "SubElementField";
sub_evolution, SubEvolution, Handle<SubEvolution>, "SubEvolution";
sub_model, SubModel, Handle<SubModel>, "SubModel";
sub_matrix, SubMatrix, Handle<SubMatrix>, "SubMatrix";
}
macro_rules! take_accessors {
($($name:ident, $variant:ident, $ty:ty, $label:literal;)*) => {
impl Objects {
$(
#[doc = concat!("The `", $label, "` named `key`, removed from the map (it is held by value).")]
pub fn $name(&mut self, key: &str) -> Result<$ty> {
match self.0.get(key) {
Some(Root::$variant(_)) => match self.0.remove(key) {
Some(Root::$variant(v)) => Ok(v),
_ => unreachable!("just matched"),
},
_ => Err(self.wrong(key, $label)),
}
}
)*
}
};
}
take_accessors! {
mesh, Mesh, Mesh, "Mesh";
fespace, FiniteElementSpace, FiniteElementSpace, "FiniteElementSpace";
node_field, NodeField, NodeField, "NodeField";
element_field, ElementField, ElementField, "ElementField";
evolution, Evolution, Evolution, "Evolution";
model, Model, Model, "Model";
matrix, Matrix, Matrix, "Matrix";
}
macro_rules! value_accessors {
($($name:ident, $variant:ident, $ty:ty, $label:literal;)*) => {
impl Objects {
$(
#[doc = concat!("The `", $label, "` named `key`.")]
pub fn $name(&self, key: &str) -> Result<$ty> {
match self.0.get(key) {
Some(Root::Value(Value::$variant(v))) => Ok(v.clone()),
_ => Err(self.wrong(key, $label)),
}
}
)*
}
};
}
value_accessors! {
bool, Bool, bool, "bool";
int, Int, i64, "int";
float, Float, f64, "float";
text, Text, String, "str";
bools, Bools, Vec<bool>, "list[bool]";
ints, Ints, Vec<i64>, "list[int]";
floats, Floats, Vec<f64>, "list[float]";
texts, Texts, Vec<String>, "list[str]";
}
#[doc(hidden)]
#[derive(Serialize, Deserialize)]
pub enum Entry {
Object(u32),
Inline { tag: String, payload: Vec<u8> },
Value(Value),
}
pub trait ArchiveRoot {
#[doc(hidden)]
fn to_entry(&self) -> Result<Entry>;
}
impl<T: Archivable> ArchiveRoot for Handle<T> {
fn to_entry(&self) -> Result<Entry> {
Ok(Entry::Object(scope::intern_handle(self)?))
}
}
macro_rules! inline_roots {
($($ty:ty),* $(,)?) => {
$(
impl ArchiveRoot for $ty {
fn to_entry(&self) -> Result<Entry> {
Ok(Entry::Inline {
tag: <$ty as Archivable>::TAG.to_string(),
payload: self.to_bytes()?,
})
}
}
)*
};
}
inline_roots!(
Mesh,
FiniteElementSpace,
NodeField,
ElementField,
Evolution,
Model,
Matrix,
);
macro_rules! value_roots {
($($ty:ty => $variant:ident),* $(,)?) => {
$(
impl ArchiveRoot for $ty {
fn to_entry(&self) -> Result<Entry> {
Ok(Entry::Value(Value::$variant(self.clone())))
}
}
)*
};
}
value_roots!(
bool => Bool,
i64 => Int,
f64 => Float,
String => Text,
Vec<bool> => Bools,
Vec<i64> => Ints,
Vec<f64> => Floats,
Vec<String> => Texts,
);
impl ArchiveRoot for str {
fn to_entry(&self) -> Result<Entry> {
Ok(Entry::Value(Value::Text(self.to_string())))
}
}
#[derive(Serialize, Deserialize)]
struct Body {
crate_version: String,
records: Vec<(u32, String, Vec<u8>)>,
roots: BTreeMap<String, Entry>,
}
pub fn save<P: AsRef<Path>>(path: P, roots: &[(&str, &dyn ArchiveRoot)]) -> Result<()> {
let (root_map, records) = scope::with_write(|| {
let mut sorted: Vec<_> = roots.iter().collect();
sorted.sort_by_key(|(name, _)| *name);
let mut map: BTreeMap<String, Entry> = BTreeMap::new();
for (name, root) in sorted {
if map.contains_key(*name) {
return Err(PyrucastError::Message(format!(
"archive: \"{name}\" is given twice"
)));
}
map.insert((*name).to_string(), root.to_entry()?);
}
Ok(map)
})?;
let body = Body {
crate_version: crate::VERSION.to_string(),
records: records
.into_iter()
.map(|r| (r.id, r.tag.to_string(), r.payload))
.collect(),
roots: root_map,
};
let mut out = Vec::with_capacity(4096);
out.extend_from_slice(MAGIC);
out.extend_from_slice(&FORMAT.to_le_bytes());
out.extend_from_slice(&body.to_bytes()?);
std::fs::write(path.as_ref(), &out)
.map_err(|e| PyrucastError::Io(format!("writing {}: {e}", path.as_ref().display())))
}
pub fn load<P: AsRef<Path>>(path: P) -> Result<Objects> {
let bytes = std::fs::read(path.as_ref())
.map_err(|e| PyrucastError::Io(format!("reading {}: {e}", path.as_ref().display())))?;
if bytes.len() < 12 || &bytes[..8] != MAGIC {
return Err(PyrucastError::Message(format!(
"{} is not a pyrucast archive (bad signature)",
path.as_ref().display()
)));
}
let format = u32::from_le_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]);
if format != FORMAT {
return Err(PyrucastError::Message(format!(
"{} is in archive format {format}, this build reads format {FORMAT}. \
Before version 1.0.0 no conversion is offered.",
path.as_ref().display()
)));
}
let body = Body::from_bytes(&bytes[12..])?;
scope::with_read(|| {
for (id, tag, payload) in &body.records {
let object = registry::decode_node(tag, payload)?;
scope::publish(*id, tag.clone(), object)?;
}
let mut out = BTreeMap::new();
for (name, entry) in body.roots {
let root = match entry {
Entry::Value(v) => Ok(Root::Value(v)),
Entry::Object(id) => {
let tag = scope::tag_of(id)?;
scope::with_object(id, |any| registry::wrap_root(&tag, any))?
}
Entry::Inline { tag, payload } => registry::decode_inline(&tag, &payload),
}?;
out.insert(name, root);
}
Ok(Objects(out))
})
}