extern crate serde;
use serde::{Serialize, Serializer};
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
pub struct Message<'a> {
#[serde(rename = "@type")]
typ: &'a str,
#[serde(rename = "@context")]
context: &'a str,
#[serde(skip_serializing_if = "String::is_empty")]
summary: String,
#[serde(rename = "themeColor", skip_serializing_if = "String::is_empty")]
theme_color: String,
#[serde(rename = "correlationId", skip_serializing_if = "usize_is_zero")]
correlation_id: usize,
#[serde(rename = "expectedActors", skip_serializing_if = "Vec::is_empty")]
expected_actors: Vec<String>,
#[serde(skip_serializing_if = "String::is_empty")]
originator: String,
#[serde(rename = "hideOriginalBody", skip_serializing_if = "Option::is_none", serialize_with = "optional_bool")]
hide_original_body: Option<bool>,
#[serde(skip_serializing_if = "String::is_empty")]
title: String,
#[serde(skip_serializing_if = "String::is_empty")]
text: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
sections: Vec<Section>,
}
impl<'a> Message<'a> {
pub fn new() -> Message<'a> {
Message {
typ: "MessageCard",
context: "https://schema.org/extensions",
..Default::default()
}
}
pub fn summary(mut self, s: impl ToString) -> Self {
self.summary = s.to_string();
self
}
pub fn theme_color(mut self, c: impl ToString) -> Self {
self.theme_color = c.to_string();
self
}
pub fn correlation_id(mut self, id: usize) -> Self {
self.correlation_id = id;
self
}
pub fn expected_actors<I>(mut self, v: I) -> Self
where
I: IntoIterator,
I::Item: ToString,
{
self.expected_actors = v.into_iter()
.map(|s| s.to_string())
.collect();
self
}
pub fn originator(mut self, s: impl ToString) -> Self {
self.originator = s.to_string();
self
}
pub fn hide_original_body(mut self, b: bool) -> Self {
self.hide_original_body = Some(b);
self
}
pub fn title(mut self, s: impl ToString) -> Self {
self.title = s.to_string();
self
}
pub fn text(mut self, s: impl ToString) -> Self {
self.text = s.to_string();
self
}
pub fn sections(mut self, v: Vec<Section>) -> Self {
self.sections = v;
self
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
pub struct Section {
#[serde(skip_serializing_if = "String::is_empty")]
title: String,
#[serde(rename = "startGroup", skip_serializing_if = "Option::is_none", serialize_with = "optional_bool")]
start_group: Option<bool>,
#[serde(rename = "activityImage", skip_serializing_if = "String::is_empty")]
activity_image: String,
#[serde(rename = "activityTitle", skip_serializing_if = "String::is_empty")]
activity_title: String,
#[serde(rename = "activitySubtitle", skip_serializing_if = "String::is_empty")]
activity_subtitle: String,
#[serde(rename = "activityText", skip_serializing_if = "String::is_empty")]
activity_text: String,
#[serde(rename = "heroImage", skip_serializing_if = "Option::is_none", serialize_with = "optional_image")]
hero_image: Option<Image>,
#[serde(skip_serializing_if = "String::is_empty")]
text: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
facts: Vec<Fact>,
#[serde(skip_serializing_if = "Vec::is_empty")]
images: Vec<Image>,
}
impl Section {
pub fn new() -> Self {
Section { ..Default::default() }
}
pub fn title(mut self, s: impl ToString) -> Self {
self.title = s.to_string();
self
}
pub fn start_group(mut self, b: bool) -> Self {
self.start_group = Some(b);
self
}
pub fn activity_image(mut self, url: impl ToString) -> Self {
self.activity_image = url.to_string();
self
}
pub fn activity_title(mut self, s: impl ToString) -> Self {
self.activity_title = s.to_string();
self
}
pub fn activity_subtitle(mut self, s: impl ToString) -> Self {
self.activity_subtitle = s.to_string();
self
}
pub fn activity_text(mut self, s: impl ToString) -> Self {
self.activity_text = s.to_string();
self
}
pub fn hero_image(mut self, i: Image) -> Self {
self.hero_image = Some(i);
self
}
pub fn text(mut self, s: impl ToString) -> Self {
self.text = s.to_string();
self
}
pub fn facts(mut self, f: Vec<Fact>) -> Self {
self.facts = f;
self
}
pub fn images(mut self, i: Vec<Image>) -> Self {
self.images = i;
self
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
pub struct Fact {
name: String,
value: String,
}
impl Fact {
pub fn new(name: impl ToString, value: impl ToString) -> Self {
Fact { name: name.to_string(), value: value.to_string() }
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
pub struct Image {
image: String,
title: String,
}
impl Image {
pub fn new(title: impl ToString, url: impl ToString) -> Self {
Image { image: url.to_string(), title: title.to_string() }
}
}
fn usize_is_zero(x: &usize) -> bool {
*x == 0
}
fn optional_bool<S>(val: &Option<bool>, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer
{
let v = match *val {
Some(b) => b,
None => false,
};
s.serialize_bool(v)
}
fn optional_image<S>(val: &Option<Image>, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer
{
match val {
Some(v) => v.serialize(s),
None => s.serialize_none(),
}
}