use iri_rfc3987::Iri;
use crate::{ObjectPropertyExpression, OperandSet, StructuralModelError};
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(try_from = "KeyPropertiesInput")]
pub struct KeyProperties {
object_properties: OperandSet<ObjectPropertyExpression>,
data_properties: OperandSet<Iri<String>>,
}
impl KeyProperties {
pub fn try_new(
object_properties: Vec<ObjectPropertyExpression>,
data_properties: Vec<Iri<String>>,
) -> Result<Self, StructuralModelError> {
if object_properties.is_empty() && data_properties.is_empty() {
return Err(StructuralModelError::InvalidFormat {
kind: "key properties",
value: "0".to_owned(),
message: "requires at least one key property",
});
}
Ok(Self {
object_properties: OperandSet::new(object_properties),
data_properties: OperandSet::new(data_properties),
})
}
#[must_use]
pub fn object_properties(&self) -> &[ObjectPropertyExpression] {
self.object_properties.as_slice()
}
#[must_use]
pub fn data_properties(&self) -> &[Iri<String>] {
self.data_properties.as_slice()
}
}
#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct KeyPropertiesInput {
object_properties: Vec<ObjectPropertyExpression>,
data_properties: Vec<Iri<String>>,
}
impl TryFrom<KeyPropertiesInput> for KeyProperties {
type Error = StructuralModelError;
fn try_from(input: KeyPropertiesInput) -> Result<Self, Self::Error> {
Self::try_new(input.object_properties, input.data_properties)
}
}