use crate::composition_objects::TextContent;
use crate::validators::*;
use serde::Serialize;
use slack_messaging_derive::Builder;
#[derive(Debug, Clone, Serialize, PartialEq, Builder)]
#[serde(tag = "type", rename = "alert")]
pub struct Alert {
#[builder(validate("required"))]
pub(crate) text: Option<TextContent>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) level: Option<AlertLevel>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(validate("text::max_255"))]
pub(crate) block_id: Option<String>,
}
#[derive(Debug, Copy, Clone, Serialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum AlertLevel {
Default,
Info,
Warning,
Error,
Success,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::composition_objects::test_helpers::*;
use crate::errors::*;
#[test]
fn it_implements_builder() {
let expected = Alert {
text: Some(plain_text("The work is mysterious and important.").into()),
level: Some(AlertLevel::Info),
block_id: Some("alert_block_0".into()),
};
let val = Alert::builder()
.set_text(Some(plain_text("The work is mysterious and important.")))
.set_level(Some(AlertLevel::Info))
.set_block_id(Some("alert_block_0"))
.build()
.unwrap();
assert_eq!(val, expected);
let val = Alert::builder()
.text(plain_text("The work is mysterious and important."))
.level(AlertLevel::Info)
.block_id("alert_block_0")
.build()
.unwrap();
assert_eq!(val, expected);
}
#[test]
fn it_requires_text_field() {
let err = Alert::builder().build().unwrap_err();
assert_eq!(err.object(), "Alert");
let errors = err.field("text");
assert!(errors.includes(ValidationErrorKind::Required));
}
#[test]
fn it_requires_block_id_less_than_255_characters_long() {
let err = Alert::builder()
.text(plain_text("The work is mysterious and important."))
.block_id("a".repeat(256))
.build()
.unwrap_err();
assert_eq!(err.object(), "Alert");
let errors = err.field("block_id");
assert!(errors.includes(ValidationErrorKind::MaxTextLength(255)));
}
}