#![deny(missing_docs)]
use std::{fs::File, io::BufReader, path::Path};
use serde::{Deserialize, Serialize};
use serde_json::Number;
mod basic_members;
mod concept;
mod custom_properties;
mod define;
mod image;
mod property;
mod prototype;
mod types;
pub use basic_members::BasicMembers;
pub use concept::Concept;
pub use custom_properties::CustomProperties;
pub use define::{Define, DefineValue};
pub use image::Image;
pub use property::{Property, PropertyDefault};
pub use prototype::Prototype;
pub use types::{Literal, Type, TypeLiteral};
#[derive(Debug)]
pub enum LoadingError {
FileOpen(std::io::Error),
JsonParse(serde_json::Error),
}
impl From<std::io::Error> for LoadingError {
fn from(err: std::io::Error) -> Self {
LoadingError::FileOpen(err)
}
}
impl From<serde_json::Error> for LoadingError {
fn from(err: serde_json::Error) -> Self {
LoadingError::JsonParse(err)
}
}
impl core::fmt::Display for LoadingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LoadingError::FileOpen(err) => write!(f, "Failed to open file: {}", err),
LoadingError::JsonParse(err) => write!(f, "Failed to parse JSON: {}", err),
}
}
}
impl core::error::Error for LoadingError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
LoadingError::FileOpen(err) => Some(err),
LoadingError::JsonParse(err) => Some(err),
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct PrototypeApi {
pub application: String,
pub application_version: String,
pub api_version: Number,
pub stage: String,
pub prototypes: Vec<prototype::Prototype>,
pub types: Vec<concept::Concept>,
pub defines: Vec<define::Define>,
}
impl PrototypeApi {
pub fn try_from_file<P: AsRef<Path>>(path: P) -> Result<Self, LoadingError> {
let json_file = File::open(path)?;
let reader = BufReader::new(json_file);
let json = serde_json::from_reader(reader)?;
Ok(json)
}
}