use super::{ElementMetadata, ModelElement, Property};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Operation {
pub metadata: ElementMetadata,
pub input: Vec<Property>,
pub output: Option<Property>,
}
impl Operation {
pub fn new(urn: String) -> Self {
Self {
metadata: ElementMetadata::new(urn),
input: Vec::new(),
output: None,
}
}
pub fn input(&self) -> &[Property] {
&self.input
}
pub fn add_input(&mut self, property: Property) {
self.input.push(property);
}
pub fn with_output(mut self, property: Property) -> Self {
self.output = Some(property);
self
}
pub fn output(&self) -> Option<&Property> {
self.output.as_ref()
}
}
impl ModelElement for Operation {
fn urn(&self) -> &str {
&self.metadata.urn
}
fn metadata(&self) -> &ElementMetadata {
&self.metadata
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Event {
pub metadata: ElementMetadata,
pub parameters: Vec<Property>,
}
impl Event {
pub fn new(urn: String) -> Self {
Self {
metadata: ElementMetadata::new(urn),
parameters: Vec::new(),
}
}
pub fn parameters(&self) -> &[Property] {
&self.parameters
}
pub fn add_parameter(&mut self, property: Property) {
self.parameters.push(property);
}
}
impl ModelElement for Event {
fn urn(&self) -> &str {
&self.metadata.urn
}
fn metadata(&self) -> &ElementMetadata {
&self.metadata
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_operation_creation() {
let operation = Operation::new("urn:samm:org.example:1.0.0#testOperation".to_string());
assert_eq!(operation.name(), "testOperation");
assert_eq!(operation.input().len(), 0);
assert!(operation.output().is_none());
}
#[test]
fn test_operation_with_output() {
let output = Property::new("urn:samm:org.example:1.0.0#output".to_string());
let operation =
Operation::new("urn:samm:org.example:1.0.0#testOp".to_string()).with_output(output);
assert!(operation.output().is_some());
}
#[test]
fn test_event_creation() {
let event = Event::new("urn:samm:org.example:1.0.0#testEvent".to_string());
assert_eq!(event.name(), "testEvent");
assert_eq!(event.parameters().len(), 0);
}
}