use serde_json::Value;
use super::macros::tmf_struct;
use super::reference::Ref;
use super::value::TimePeriod;
tmf_struct! {
@name = "Characteristic";
pub struct Characteristic {
id: String,
name: String,
value_type: String,
value: Value,
characteristic_relationship: Vec<CharacteristicRelationship>,
}
}
impl Characteristic {
pub fn new(name: impl Into<String>, value: impl Into<Value>) -> Self {
Self::builder().name(name).build().with_value(value)
}
#[must_use]
pub fn with_value(mut self, value: impl Into<Value>) -> Self {
let value = value.into();
if let Some(class) = ValueKind::of_value(&value).characteristic_type() {
class.clone_into(&mut self.at_type);
}
self.value = Some(value);
self
}
#[must_use]
pub fn with_value_type(mut self, value_type: impl Into<String>) -> Self {
self.value_type = Some(value_type.into());
self
}
#[must_use]
pub fn value_kind(&self) -> ValueKind {
ValueKind::from_type_name(self.type_name())
}
}
tmf_struct! {
@name = "CharacteristicRelationship";
pub struct CharacteristicRelationship {
id: String,
relationship_type: String,
}
}
tmf_struct! {
@name = "CharacteristicSpecification";
pub struct CharacteristicSpecification {
id: String,
name: String,
value_type: String,
description: String,
configurable: bool,
is_unique: bool,
min_cardinality: i64,
max_cardinality: i64,
regex: String,
extensible: bool,
valid_for: TimePeriod,
char_spec_relationship: Vec<CharacteristicSpecificationRelationship>,
characteristic_value_specification: Vec<CharacteristicValueSpecification>,
@renamed {
"@valueSchemaLocation" at_value_schema_location: String,
}
}
}
tmf_struct! {
@name = "CharacteristicSpecificationRelationship";
pub struct CharacteristicSpecificationRelationship {
relationship_type: String,
name: String,
characteristic_specification_id: String,
parent_specification_id: String,
parent_specification_href: String,
valid_for: TimePeriod,
}
}
tmf_struct! {
@name = "CharacteristicValueSpecification";
pub struct CharacteristicValueSpecification {
value_type: String,
is_default: bool,
unit_of_measure: String,
valid_for: TimePeriod,
value_from: i64,
value_to: i64,
range_interval: String,
regex: String,
value: Value,
}
}
impl CharacteristicValueSpecification {
pub fn new(value: impl Into<Value>) -> Self {
Self::builder().build().with_value(value)
}
#[must_use]
pub fn with_value(mut self, value: impl Into<Value>) -> Self {
let value = value.into();
if let Some(class) = ValueKind::of_value(&value).value_specification_type() {
class.clone_into(&mut self.at_type);
}
self.value = Some(value);
self
}
#[must_use]
pub fn value_kind(&self) -> ValueKind {
ValueKind::from_type_name(self.type_name())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ValueKind {
String,
StringArray,
Integer,
IntegerArray,
Number,
NumberArray,
Boolean,
BooleanArray,
Float,
FloatArray,
Object,
ObjectArray,
Map,
MapArray,
Other,
}
impl ValueKind {
#[must_use]
pub fn all() -> &'static [Self] {
&[
Self::String,
Self::StringArray,
Self::Integer,
Self::IntegerArray,
Self::Number,
Self::NumberArray,
Self::Boolean,
Self::BooleanArray,
Self::Float,
Self::FloatArray,
Self::Object,
Self::ObjectArray,
Self::Map,
Self::MapArray,
]
}
#[must_use]
pub fn from_type_name(name: &str) -> Self {
let prefix = name
.strip_suffix("CharacteristicValueSpecification")
.or_else(|| name.strip_suffix("Characteristic"))
.unwrap_or("");
match prefix {
"String" => Self::String,
"StringArray" => Self::StringArray,
"Integer" => Self::Integer,
"IntegerArray" => Self::IntegerArray,
"Number" => Self::Number,
"NumberArray" => Self::NumberArray,
"Boolean" => Self::Boolean,
"BooleanArray" => Self::BooleanArray,
"Float" => Self::Float,
"FloatArray" => Self::FloatArray,
"Object" => Self::Object,
"ObjectArray" => Self::ObjectArray,
"Map" => Self::Map,
"MapArray" => Self::MapArray,
_ => Self::Other,
}
}
#[must_use]
pub fn of_value(value: &Value) -> Self {
match value {
Value::String(_) => Self::String,
Value::Bool(_) => Self::Boolean,
Value::Number(n) => {
if n.is_i64() || n.is_u64() {
Self::Integer
} else {
Self::Number
}
}
Value::Object(_) => Self::Object,
Value::Array(items) => match items.split_first() {
Some((first, rest)) => {
let kind = Self::of_value(first);
if rest.iter().all(|item| Self::of_value(item) == kind) {
kind.array_of()
} else {
Self::Other
}
}
None => Self::Other,
},
Value::Null => Self::Other,
}
}
#[must_use]
fn array_of(self) -> Self {
match self {
Self::String => Self::StringArray,
Self::Integer => Self::IntegerArray,
Self::Number => Self::NumberArray,
Self::Boolean => Self::BooleanArray,
Self::Float => Self::FloatArray,
Self::Object => Self::ObjectArray,
Self::Map => Self::MapArray,
_ => Self::Other,
}
}
#[must_use]
pub fn is_array(self) -> bool {
matches!(
self,
Self::StringArray
| Self::IntegerArray
| Self::NumberArray
| Self::BooleanArray
| Self::FloatArray
| Self::ObjectArray
| Self::MapArray
)
}
#[must_use]
pub fn characteristic_type(self) -> Option<&'static str> {
Some(match self {
Self::String => "StringCharacteristic",
Self::StringArray => "StringArrayCharacteristic",
Self::Integer => "IntegerCharacteristic",
Self::IntegerArray => "IntegerArrayCharacteristic",
Self::Number => "NumberCharacteristic",
Self::NumberArray => "NumberArrayCharacteristic",
Self::Boolean => "BooleanCharacteristic",
Self::BooleanArray => "BooleanArrayCharacteristic",
Self::Float => "FloatCharacteristic",
Self::FloatArray => "FloatArrayCharacteristic",
Self::Object => "ObjectCharacteristic",
Self::ObjectArray => "ObjectArrayCharacteristic",
Self::Map | Self::MapArray | Self::Other => return None,
})
}
#[must_use]
pub fn value_specification_type(self) -> Option<&'static str> {
Some(match self {
Self::String => "StringCharacteristicValueSpecification",
Self::StringArray => "StringArrayCharacteristicValueSpecification",
Self::Integer => "IntegerCharacteristicValueSpecification",
Self::IntegerArray => "IntegerArrayCharacteristicValueSpecification",
Self::Number => "NumberCharacteristicValueSpecification",
Self::NumberArray => "NumberArrayCharacteristicValueSpecification",
Self::Boolean => "BooleanCharacteristicValueSpecification",
Self::BooleanArray => "BooleanArrayCharacteristicValueSpecification",
Self::Float => "FloatCharacteristicValueSpecification",
Self::FloatArray => "FloatArrayCharacteristicValueSpecification",
Self::Object => "ObjectCharacteristicValueSpecification",
Self::ObjectArray => "ObjectArrayCharacteristicValueSpecification",
Self::Map => "MapCharacteristicValueSpecification",
Self::MapArray => "MapArrayCharacteristicValueSpecification",
Self::Other => return None,
})
}
}
tmf_struct! {
@name = "ProductSpecificationCharacteristicValueUse";
pub struct CharacteristicValueUse {
id: String,
name: String,
description: String,
value_type: String,
min_cardinality: i64,
max_cardinality: i64,
valid_for: TimePeriod,
product_spec_characteristic_value: Vec<CharacteristicValueSpecification>,
product_specification: Ref<SpecificationTarget>,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SpecificationTarget;
impl super::extensible::TmfType for SpecificationTarget {
const TYPE_NAME: &'static str = "ProductSpecification";
const REF_TYPE_NAME: &'static str = "ProductSpecificationRef";
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn unknown_value_subclass_parses_as_other() {
let json = r#"{"@type":"VendorSpecialCharacteristicValueSpecification","value":1}"#;
let v: CharacteristicValueSpecification = serde_json::from_str(json).unwrap();
assert_eq!(v.value_kind(), ValueKind::Other);
let before: serde_json::Value = serde_json::from_str(json).unwrap();
let after = serde_json::to_value(&v).unwrap();
assert_eq!(before, after);
}
#[test]
fn known_value_subclass_maps_to_kind() {
let json = r#"{"@type":"StringCharacteristicValueSpecification","value":"x"}"#;
let v: CharacteristicValueSpecification = serde_json::from_str(json).unwrap();
assert_eq!(v.value_kind(), ValueKind::String);
assert_eq!(
ValueKind::String.value_specification_type(),
Some(v.at_type.as_str())
);
}
#[test]
fn a_built_characteristic_declares_its_subclass_not_the_base() {
for (value, class) in [
(json!("gold"), "StringCharacteristic"),
(json!(100), "IntegerCharacteristic"),
(json!(1.5), "NumberCharacteristic"),
(json!(true), "BooleanCharacteristic"),
(json!({"a": 1}), "ObjectCharacteristic"),
(json!(["a", "b"]), "StringArrayCharacteristic"),
(json!([1, 2]), "IntegerArrayCharacteristic"),
(json!([{"a": 1}]), "ObjectArrayCharacteristic"),
] {
let c = Characteristic::new("x", value.clone());
assert_eq!(c.type_name(), class, "for {value}");
assert_eq!(c.value.as_ref(), Some(&value));
}
}
#[test]
fn a_shape_that_names_no_subclass_leaves_the_base_class() {
for value in [json!(null), json!([]), json!([1, "two"]), json!([[1]])] {
assert_eq!(
Characteristic::new("x", value.clone()).type_name(),
"Characteristic",
"for {value}"
);
}
}
#[test]
fn a_type_names_the_same_kind_in_both_families() {
for kind in ValueKind::all() {
if let Some(name) = kind.characteristic_type() {
assert_eq!(ValueKind::from_type_name(name), *kind, "{name}");
}
let name = kind
.value_specification_type()
.expect("every kind is a value specification");
assert_eq!(ValueKind::from_type_name(name), *kind, "{name}");
}
assert_eq!(ValueKind::Map.characteristic_type(), None, "no such class");
assert_eq!(ValueKind::Other.value_specification_type(), None);
assert_eq!(
ValueKind::from_type_name("Characteristic"),
ValueKind::Other
);
}
#[test]
fn what_the_sender_said_and_what_the_value_is_stay_separable() {
let odd: Characteristic =
serde_json::from_str(r#"{"@type":"IntegerCharacteristic","value":"12"}"#).unwrap();
assert_eq!(odd.value_kind(), ValueKind::Integer, "what it claims");
assert_eq!(
odd.value.as_ref().map(ValueKind::of_value),
Some(ValueKind::String),
"what it is"
);
}
#[test]
fn array_kinds_report_themselves_as_arrays() {
assert!(ValueKind::StringArray.is_array());
assert!(!ValueKind::String.is_array());
assert!(!ValueKind::Other.is_array());
}
#[test]
fn value_schema_location_is_distinct_from_schema_location() {
let json = r#"{"@type":"CharacteristicSpecification","@schemaLocation":"a","@valueSchemaLocation":"b"}"#;
let c: CharacteristicSpecification = serde_json::from_str(json).unwrap();
assert_eq!(c.at_schema_location.as_deref(), Some("a"));
assert_eq!(c.at_value_schema_location.as_deref(), Some("b"));
assert!(c.extensions.is_empty(), "both must be typed, not captured");
}
}