#![deny(missing_docs)]
#![deny(warnings)]
#![deny(missing_debug_implementations)]
extern crate indexmap;
extern crate serde;
#[macro_use] extern crate serde_derive;
extern crate serde_json;
mod error;
#[macro_use] mod codec;
mod types;
mod parts;
pub mod aws;
pub use error::{Error, ErrorKind};
pub use types::*;
pub use parts::*;
pub mod json {
pub use serde_json::{Value, Number};
}
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Template {
#[serde(rename = "Description", default, skip_serializing_if = "Option::is_none")]
description: Option<String>,
#[serde(rename = "Resources", default)]
resources: Resources,
#[serde(rename = "Outputs", default)]
outputs: Outputs
}
impl Template {
pub fn description(&self) -> &Option<String> {
&self.description
}
pub fn description_mut(&mut self) -> &mut Option<String> {
&mut self.description
}
pub fn resources(&self) -> &Resources {
&self.resources
}
pub fn resources_mut(&mut self) -> &mut Resources {
&mut self.resources
}
pub fn outputs(&self) -> &Outputs {
&self.outputs
}
pub fn outputs_mut(&mut self) -> &mut Outputs {
&mut self.outputs
}
}
impl Template {
pub fn from_json<T: AsRef<str>>(input: T) -> Result<Template, ::Error> {
serde_json::from_str(input.as_ref())
.map_err(|err| ::Error::new(::ErrorKind::Serialization, err))
}
pub fn to_json(&self) -> Result<String, ::Error> {
serde_json::to_string(self)
.map_err(|err| ::Error::new(::ErrorKind::Serialization, err))
}
}
pub trait Resource: Sized + private::Sealed {
const TYPE: &'static str;
type Properties: private::Properties<Self>;
fn properties(&self) -> &Self::Properties;
fn properties_mut(&mut self) -> &mut Self::Properties;
}
mod private {
pub trait Sealed {}
pub trait Properties<R>: Into<R> + ::serde::Serialize + ::serde::de::DeserializeOwned {}
impl<P, R> Properties<R> for P where P: Into<R> + ::serde::Serialize + ::serde::de::DeserializeOwned {}
}
#[cfg(test)]
mod tests {
use serde_json::to_value;
use super::Template;
#[test]
fn deserialize_empty_template() {
let tpl = Template::from_json("{}").unwrap();
assert!(tpl.description().is_none())
}
#[test]
fn serialize_empty_template() {
let tpl = Template::default();
let val = to_value(&tpl).unwrap();
let obj = val.as_object().unwrap();
assert_eq!(2, obj.len());
assert_eq!(false, obj.contains_key("Description"));
assert_eq!(true, obj.contains_key("Resources"));
assert_eq!(true, obj.contains_key("Outputs"));
}
}