use crate::error::{Error, Result};
use crate::TapMessage;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, TapMessage)]
#[tap(message_type = "https://tap.rsvp/schema/1.0#Cancel")]
pub struct Cancel {
#[tap(thread_id)]
pub transaction_id: String,
pub by: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}
impl Cancel {
pub fn new(transaction_id: &str, by: &str) -> Self {
Self {
transaction_id: transaction_id.to_string(),
by: by.to_string(),
reason: None,
}
}
pub fn with_reason(transaction_id: &str, by: &str, reason: &str) -> Self {
Self {
transaction_id: transaction_id.to_string(),
by: by.to_string(),
reason: Some(reason.to_string()),
}
}
}
impl Cancel {
pub fn validate_cancel(&self) -> Result<()> {
if self.transaction_id.is_empty() {
return Err(Error::Validation(
"Cancel message must have a transaction_id".into(),
));
}
if self.by.is_empty() {
return Err(Error::Validation(
"Cancel message must specify 'by' field".into(),
));
}
Ok(())
}
}