#![allow(
unused_imports,
dead_code,
non_camel_case_types,
unused_variables,
unexpected_cfgs,
clippy::all
)]
use super::context::{LoadContext, SaveContext};
use super::diagnostic::Diagnostic;
use super::office_package_manifest::OfficePackageManifest;
use super::power_point_invariants::PowerPointInvariants;
use super::presentation_stats::PresentationStats;
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PowerPointRenderResult {
pub stats: PresentationStats,
pub manifest: OfficePackageManifest,
pub invariants: PowerPointInvariants,
pub warnings: Vec<Diagnostic>,
}
impl PowerPointRenderResult {
pub fn new() -> Self {
Self::default()
}
pub fn from_json(json: &str, ctx: &LoadContext) -> Result<Self, serde_json::Error> {
let value: serde_json::Value = serde_json::from_str(json)?;
Self::validate_input_at(&value, "")
.map_err(|message| <serde_json::Error as serde::de::Error>::custom(message))?;
Ok(Self::load_from_value(&value, ctx))
}
pub fn from_yaml(yaml: &str, ctx: &LoadContext) -> Result<Self, serde_yaml::Error> {
let value: serde_json::Value = serde_yaml::from_str(yaml)?;
Self::validate_input_at(&value, "")
.map_err(|message| <serde_yaml::Error as serde::de::Error>::custom(message))?;
Ok(Self::load_from_value(&value, ctx))
}
pub fn try_load_from_value(
value: &serde_json::Value,
ctx: &LoadContext,
) -> Result<Self, serde_json::Error> {
Self::validate_input_at(value, "")
.map_err(|message| <serde_json::Error as serde::de::Error>::custom(message))?;
Ok(Self::load_from_value(value, ctx))
}
pub fn load_from_value(value: &serde_json::Value, ctx: &LoadContext) -> Self {
let value = ctx.process_input(value.clone());
if let Err(message) = Self::validate_input_at(&value, "") {
panic!("{}", message);
}
Self {
stats: value
.get("stats")
.filter(|v| v.is_object() || v.is_array() || v.is_string())
.map(|v| PresentationStats::load_from_value(v, ctx))
.unwrap_or_default(),
manifest: value
.get("manifest")
.filter(|v| v.is_object() || v.is_array() || v.is_string())
.map(|v| OfficePackageManifest::load_from_value(v, ctx))
.unwrap_or_default(),
invariants: value
.get("invariants")
.filter(|v| v.is_object() || v.is_array() || v.is_string())
.map(|v| PowerPointInvariants::load_from_value(v, ctx))
.unwrap_or_default(),
warnings: value
.get("warnings")
.map(|v| Self::load_warnings(v, ctx))
.unwrap_or_default(),
}
}
pub(crate) fn validate_input_at(value: &serde_json::Value, path: &str) -> Result<(), String> {
let child_path = if path.is_empty() {
"stats".to_string()
} else {
format!("{}.stats", path)
};
let child = value
.get("stats")
.filter(|candidate| !candidate.is_null())
.ok_or_else(|| format!("{}: missing required field", child_path))?;
PresentationStats::validate_input_at(child, &child_path)?;
let child_path = if path.is_empty() {
"manifest".to_string()
} else {
format!("{}.manifest", path)
};
let child = value
.get("manifest")
.filter(|candidate| !candidate.is_null())
.ok_or_else(|| format!("{}: missing required field", child_path))?;
OfficePackageManifest::validate_input_at(child, &child_path)?;
let child_path = if path.is_empty() {
"invariants".to_string()
} else {
format!("{}.invariants", path)
};
let child = value
.get("invariants")
.filter(|candidate| !candidate.is_null())
.ok_or_else(|| format!("{}: missing required field", child_path))?;
PowerPointInvariants::validate_input_at(child, &child_path)?;
if let Some(entries) = value
.get("warnings")
.and_then(|candidate| candidate.as_array())
{
let collection_path = if path.is_empty() {
"warnings".to_string()
} else {
format!("{}.warnings", path)
};
for (index, entry) in entries.iter().enumerate() {
let entry_path = format!("{}[{}]", collection_path, index);
Diagnostic::validate_input_at(entry, &entry_path)?;
}
}
Ok(())
}
pub fn to_value(&self, ctx: &SaveContext) -> serde_json::Value {
let mut result = serde_json::Map::new();
{
let nested = self.stats.to_value(ctx);
result.insert("stats".to_string(), nested);
}
{
let nested = self.manifest.to_value(ctx);
result.insert("manifest".to_string(), nested);
}
{
let nested = self.invariants.to_value(ctx);
result.insert("invariants".to_string(), nested);
}
result.insert(
"warnings".to_string(),
Self::save_warnings(&self.warnings, ctx),
);
ctx.process_dict(serde_json::Value::Object(result))
}
pub fn to_json(&self, ctx: &SaveContext) -> Result<String, serde_json::Error> {
serde_json::to_string_pretty(&self.to_value(ctx))
}
pub fn to_yaml(&self, ctx: &SaveContext) -> Result<String, serde_yaml::Error> {
serde_yaml::to_string(&self.to_value(ctx))
}
fn load_warnings(data: &serde_json::Value, ctx: &LoadContext) -> Vec<Diagnostic> {
match data {
serde_json::Value::Array(arr) => arr
.iter()
.map(|v| Diagnostic::load_from_value(v, ctx))
.collect(),
_ => Vec::new(),
}
}
fn save_warnings(items: &[Diagnostic], ctx: &SaveContext) -> serde_json::Value {
serde_json::Value::Array(
items
.iter()
.map(|item| item.to_value(ctx))
.collect::<Vec<_>>(),
)
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for PowerPointRenderResult {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serde::Serialize::serialize(&self.to_value(&SaveContext::default()), serializer)
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for PowerPointRenderResult {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = <serde_json::Value as serde::Deserialize>::deserialize(deserializer)?;
Self::validate_input_at(&value, "").map_err(serde::de::Error::custom)?;
Ok(Self::load_from_value(&value, &LoadContext::default()))
}
}