use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use super::page_schema::PageSchema;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ContractArtifact {
format: String,
pages: BTreeMap<String, PageSchema>,
}
impl ContractArtifact {
pub const FORMAT: &'static str = "arcature.page-contract.v1";
#[must_use]
pub fn new(pages: BTreeMap<String, PageSchema>) -> Self {
Self {
format: Self::FORMAT.to_owned(),
pages,
}
}
#[must_use]
pub fn format(&self) -> &str {
&self.format
}
#[must_use]
pub fn pages(&self) -> &BTreeMap<String, PageSchema> {
&self.pages
}
pub fn to_json(&self) -> Result<Vec<u8>, serde_json::Error> {
serde_json::to_vec_pretty(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::inertia::contracts::{ContractType, PropsSchema};
fn artifact() -> ContractArtifact {
let mut pages = BTreeMap::new();
pages.insert(
"Home".to_owned(),
PageSchema::new(PropsSchema::new().required("name", ContractType::string())),
);
ContractArtifact::new(pages)
}
#[test]
fn carries_the_stable_format_identifier() {
assert_eq!(artifact().format(), ContractArtifact::FORMAT);
}
#[test]
fn json_is_deterministic() {
assert_eq!(artifact().to_json().unwrap(), artifact().to_json().unwrap());
}
#[test]
fn round_trips_through_json() {
let json = artifact().to_json().unwrap();
let parsed: ContractArtifact = serde_json::from_slice(&json).unwrap();
assert_eq!(parsed, artifact());
}
}