Skip to content

Commit d6f110c

Browse files
authored
feat: Improve data types for rich text blocks (#353)
1 parent e00f648 commit d6f110c

4 files changed

Lines changed: 76 additions & 4 deletions

File tree

examples/socket_mode.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,49 @@ impl SlackBlocksTemplate for SlackHomeTabBlocksTemplateExample {
155155
some_into(SlackSectionBlock::new().with_text(md!("Latest news:")))
156156
],
157157
new_blocks,
158+
slack_blocks![
159+
some_into(SlackDividerBlock::new()),
160+
some_into(SlackRichTextBlock::new(
161+
vec![
162+
SlackRichTextSection::new(
163+
vec![
164+
SlackRichTextInlineElement::Text(
165+
SlackRichTextText::new("Let's use some rich text: ".into())
166+
.with_style(SlackRichTextStyle::new().with_bold(true))
167+
),
168+
SlackRichTextInlineElement::Emoji(
169+
SlackRichTextEmoji::new("slightly_smiling_face".into()).into()
170+
),
171+
]
172+
.into()
173+
)
174+
.into(),
175+
SlackRichTextSection::new(
176+
vec![SlackRichTextInlineElement::Date(SlackRichTextDate::new(
177+
SlackDateTime::now(),
178+
SlackDateTimeFormats::DateLong.to_string()
179+
))]
180+
.into()
181+
)
182+
.into(),
183+
SlackRichTextQuote::new(
184+
vec![
185+
SlackRichTextInlineElement::Text(SlackRichTextText::new(
186+
"While there is life, there is a hope. ".into()
187+
)),
188+
SlackRichTextInlineElement::Link(SlackRichTextLink::new(
189+
Url::parse("https://slack-rust.abdolence.dev")
190+
.expect("A proper url")
191+
.into()
192+
))
193+
]
194+
.into()
195+
)
196+
.into(),
197+
]
198+
.into()
199+
))
200+
],
158201
]
159202
.concat()
160203
}

src/models/blocks/kit.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ pub enum SlackBlock {
3333
Video(SlackVideoBlock),
3434
#[serde(rename = "markdown")]
3535
Markdown(SlackMarkdownBlock),
36-
3736
#[serde(rename = "rich_text")]
3837
RichText(SlackRichTextBlock),
3938
#[serde(rename = "share_shortcut")]
@@ -1100,6 +1099,30 @@ pub enum SlackRichTextElement {
11001099
Quote(SlackRichTextQuote),
11011100
}
11021101

1102+
impl From<SlackRichTextSection> for SlackRichTextElement {
1103+
fn from(element: SlackRichTextSection) -> Self {
1104+
SlackRichTextElement::Section(element)
1105+
}
1106+
}
1107+
1108+
impl From<SlackRichTextList> for SlackRichTextElement {
1109+
fn from(list: SlackRichTextList) -> Self {
1110+
SlackRichTextElement::List(list)
1111+
}
1112+
}
1113+
1114+
impl From<SlackRichTextPreformatted> for SlackRichTextElement {
1115+
fn from(element: SlackRichTextPreformatted) -> Self {
1116+
SlackRichTextElement::Preformatted(element)
1117+
}
1118+
}
1119+
1120+
impl From<SlackRichTextQuote> for SlackRichTextElement {
1121+
fn from(element: SlackRichTextQuote) -> Self {
1122+
SlackRichTextElement::Quote(element)
1123+
}
1124+
}
1125+
11031126
#[skip_serializing_none]
11041127
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
11051128
pub struct SlackRichTextSection {
@@ -1184,7 +1207,7 @@ pub struct SlackRichTextText {
11841207
#[skip_serializing_none]
11851208
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
11861209
pub struct SlackRichTextLink {
1187-
pub url: String,
1210+
pub url: Url,
11881211
pub text: Option<String>,
11891212
#[serde(rename = "unsafe")]
11901213
pub unsafe_: Option<bool>,
@@ -1222,7 +1245,7 @@ pub struct SlackRichTextEmoji {
12221245
#[skip_serializing_none]
12231246
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
12241247
pub struct SlackRichTextDate {
1225-
pub timestamp: i64,
1248+
pub timestamp: SlackDateTime,
12261249
pub format: String,
12271250
pub fallback: Option<String>,
12281251
pub style: Option<SlackRichTextStyle>,

src/models/common/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ pub struct SlackBotId(pub String);
128128
#[derive(Debug, Eq, PartialEq, Hash, Clone, Serialize, Deserialize, ValueStruct)]
129129
pub struct SlackDateTime(#[serde(with = "ts_seconds")] pub DateTime<Utc>);
130130

131+
impl SlackDateTime {
132+
pub fn now() -> Self {
133+
Self(Utc::now())
134+
}
135+
}
136+
131137
#[derive(Debug, Eq, PartialEq, Hash, Clone, Serialize, Deserialize, ValueStruct)]
132138
pub struct SlackDate(pub String);
133139

src/models/events/push.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ pub enum SlackMessageEventType {
175175
/// Catch-all for unknown subtypes Slack may add in the future.
176176
/// Preserves the original subtype string.
177177
#[serde(untagged)]
178-
Unknown(String),
178+
Other(String),
179179
}
180180

181181
#[skip_serializing_none]

0 commit comments

Comments
 (0)