use super::{Characteristic, ElementMetadata, ModelElement};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Property {
pub metadata: ElementMetadata,
pub characteristic: Option<Characteristic>,
pub example_values: Vec<String>,
pub optional: bool,
pub is_collection: bool,
pub payload_name: Option<String>,
pub is_abstract: bool,
pub extends: Option<String>,
}
impl Property {
pub fn new(urn: String) -> Self {
Self {
metadata: ElementMetadata::new(urn),
characteristic: None,
example_values: Vec::new(),
optional: false,
is_collection: false,
payload_name: None,
is_abstract: false,
extends: None,
}
}
pub fn with_characteristic(mut self, characteristic: Characteristic) -> Self {
self.characteristic = Some(characteristic);
self
}
pub fn as_optional(mut self) -> Self {
self.optional = true;
self
}
pub fn as_collection(mut self) -> Self {
self.is_collection = true;
self
}
pub fn with_payload_name(mut self, name: String) -> Self {
self.payload_name = Some(name);
self
}
pub fn as_abstract(mut self) -> Self {
self.is_abstract = true;
self
}
pub fn extends(mut self, property_urn: String) -> Self {
self.extends = Some(property_urn);
self
}
pub fn effective_name(&self) -> String {
self.payload_name.clone().unwrap_or_else(|| self.name())
}
}
impl ModelElement for Property {
fn urn(&self) -> &str {
&self.metadata.urn
}
fn metadata(&self) -> &ElementMetadata {
&self.metadata
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_property_creation() {
let property = Property::new("urn:samm:org.example:1.0.0#testProperty".to_string())
.as_optional()
.with_payload_name("test_property".to_string());
assert_eq!(property.name(), "testProperty");
assert_eq!(property.effective_name(), "test_property");
assert!(property.optional);
assert!(!property.is_abstract);
}
}