use serde::{Deserialize, Serialize};
use super::basic_members::BasicMembers;
use super::types::{Type, TypeLiteral};
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(untagged)]
pub enum PropertyDefault {
Description(String),
Literal(TypeLiteral),
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct Property {
#[serde(flatten)]
pub basic: BasicMembers,
pub visibility: Option<Vec<String>>,
pub alt_name: Option<String>,
pub r#override: bool,
pub r#type: Type,
pub optional: bool,
pub default: Option<PropertyDefault>,
}
#[cfg(test)]
mod tests {
use crate::{image::Image, types::Literal};
use super::*;
#[test]
fn default_field_type_description() {
let json = r#""foo""#;
let expected = PropertyDefault::Description("foo".into());
let actual = serde_json::from_str::<PropertyDefault>(json).unwrap();
assert_eq!(expected, actual);
}
#[test]
fn default_field_type_literal() {
let json = r#"
{
"complex_type": "literal",
"value": "foo"
}"#;
let expected = PropertyDefault::Literal(TypeLiteral {
value: Literal::String("foo".into()),
description: None,
});
let actual = serde_json::from_str::<PropertyDefault>(json).unwrap();
assert_eq!(expected, actual);
}
#[test]
fn default_field_type_non_literal() {
let json = r#"
{
"complex_type": "struct"
}"#;
let actual = serde_json::from_str::<PropertyDefault>(json);
assert!(actual.is_err());
}
#[test]
fn all_fields() {
let json = r#"
{
"name": "f1",
"order": 2,
"description": "f3",
"lists": [
"f4"
],
"examples": [
"f5"
],
"images": [
{ "filename": "f6", "caption": "f7" }
],
"visibility": [
"f8"
],
"alt_name": "f9",
"override": true,
"type": "f10",
"optional": true,
"default": {
"complex_type": "literal",
"value": 11
}
}"#;
let expected = Property {
basic: BasicMembers {
name: "f1".into(),
order: 2.into(),
description: "f3".into(),
lists: Some(vec!["f4".into()]),
examples: Some(vec!["f5".into()]),
images: Some(vec![Image {
filename: "f6".into(),
caption: Some("f7".into()),
}]),
},
visibility: Some(vec!["f8".into()]),
alt_name: Some("f9".into()),
r#override: true,
r#type: Type::Simple("f10".into()),
optional: true,
default: Some(PropertyDefault::Literal(TypeLiteral {
value: Literal::Number(11.into()),
description: None,
})),
};
let actual = serde_json::from_str::<Property>(json).unwrap();
assert_eq!(expected, actual);
}
#[test]
fn without_optionals() {
let json = r#"
{
"name": "f1",
"order": 2,
"description": "f3",
"override": true,
"type": "f4",
"optional": true
}"#;
let expected = Property {
basic: BasicMembers {
name: "f1".into(),
order: 2.into(),
description: "f3".into(),
lists: None,
examples: None,
images: None,
},
visibility: None,
alt_name: None,
r#override: true,
r#type: Type::Simple("f4".into()),
optional: true,
default: None,
};
let actual = serde_json::from_str::<Property>(json).unwrap();
assert_eq!(expected, actual);
}
}