Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 91 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ maintenance = { status = "actively-developed" }

[dependencies]
serde = { version = "^1", features = ["derive"] }
validator = { version = "0.10.0", optional = true }
validator_derive = { version = "0.10.0", optional = true }
validator = { version = "0.16.0", optional = true }
validator_derive = { version = "0.16.0", optional = true }
mox = { version = "0.12", optional = true }

[dev-dependencies]
Expand Down
3 changes: 1 addition & 2 deletions src/blocks/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
use std::borrow::Cow;

use serde::{Deserialize, Serialize};
#[cfg(feature = "validation")]
use validator::Validate;


use crate::text;
#[cfg(feature = "validation")]
Expand Down
24 changes: 17 additions & 7 deletions src/blocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ pub mod header;
#[doc(inline)]
pub use header::Header;

pub mod rich_text;
#[doc(inline)]
pub use rich_text::RichText;

/// # Layout Blocks
///
/// Blocks are a series of components that can be combined
Expand Down Expand Up @@ -88,6 +92,9 @@ pub enum Block<'a> {

/// # File Block
File(File<'a>),

/// # Rich Text Block
RichText(RichText<'a>),
}

impl fmt::Display for Block<'_> {
Expand All @@ -101,6 +108,7 @@ impl fmt::Display for Block<'_> {
| Block::Context { .. } => "Context",
| Block::Input { .. } => "Input",
| Block::File { .. } => "File",
| Block::RichText { .. } => "RichText",
};

write!(f, "{}", kind)
Expand Down Expand Up @@ -132,18 +140,20 @@ impl<'a> Block<'a> {
| Input(contents) => contents.validate(),
| Header(contents) => contents.validate(),
| File(contents) => contents.validate(),
| RichText(contents) => validator::Validate::validate(contents),
| Divider => Ok(()),
}
}
}

convert!(impl<'a> From<Actions<'a>> for Block<'a> => |a| Block::Actions(a));
convert!(impl<'a> From<Input<'a>> for Block<'a> => |a| Block::Input(a));
convert!(impl<'a> From<Section<'a>> for Block<'a> => |a| Block::Section(a));
convert!(impl<'a> From<Image<'a>> for Block<'a> => |a| Block::Image(a));
convert!(impl<'a> From<Context<'a>> for Block<'a> => |a| Block::Context(a));
convert!(impl<'a> From<File<'a>> for Block<'a> => |a| Block::File(a));
convert!(impl<'a> From<Header<'a>> for Block<'a> => |a| Block::Header(a));
convert!(impl<'a> From<RichText<'a>> for Block<'a> => Block::RichText);
convert!(impl<'a> From<Actions<'a>> for Block<'a> => Block::Actions);
convert!(impl<'a> From<Input<'a>> for Block<'a> => Block::Input);
convert!(impl<'a> From<Section<'a>> for Block<'a> => Block::Section);
convert!(impl<'a> From<Image<'a>> for Block<'a> => Block::Image);
convert!(impl<'a> From<Context<'a>> for Block<'a> => Block::Context);
convert!(impl<'a> From<File<'a>> for Block<'a> => Block::File);
convert!(impl<'a> From<Header<'a>> for Block<'a> => Block::Header);

/// Error yielded when `TryFrom` is called on an unsupported block element.
#[derive(Clone, Debug, Deserialize, Hash, PartialEq, Serialize)]
Expand Down
24 changes: 24 additions & 0 deletions src/blocks/rich_text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! Undocumented rich text blocks

#![allow(missing_docs)]

use std::borrow::Cow;

use crate::text;

#[derive(Clone, Debug, serde::Deserialize, Hash, PartialEq, serde::Serialize)]
#[cfg_attr(feature = "validation", derive(Validate))]
pub struct RichText<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "validation",
validate(custom = "super::validate_block_id"))]
pub block_id: Option<Cow<'a, str>>,
pub elements: Vec<RichTextSection>,
}

#[derive(Clone, Debug, serde::Deserialize, Hash, PartialEq, serde::Serialize)]
pub struct RichTextSection {
#[serde(rename = "type")]
pub ty: String,
pub elements: Vec<text::Text>,
}
3 changes: 1 addition & 2 deletions src/elems/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
use std::borrow::Cow;

use serde::{Deserialize as De, Serialize as Ser};
#[cfg(feature = "validation")]
use validator::Validate;


#[cfg(feature = "validation")]
use crate::val_helpr::ValidationResult;
Expand Down
32 changes: 32 additions & 0 deletions tests/json/blocks/rich_text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use pretty_assertions::assert_eq;
use serde_json::json;
use slack_blocks::{blocks::{self, RichText, rich_text::RichTextSection}, blox::*, text::ToSlackPlaintext};

/// <https://github.com/cakekindel/slack-blocks-rs/issues/170>
#[test]
pub fn issue_170() {
let button = blox! {<button action_id="fart">"Click me!!!!"</button>};
let block: blocks::Block = slack_blocks::Block::RichText(RichText {
block_id: Some("ommitted".into()),
elements: vec![RichTextSection { ty: "rich_text_section".into(), elements: vec!["foobar".plaintext()] }],
});

let actual = serde_json::to_value(block).expect("should serialize");
let expected = json!({
"type": "rich_text",
"block_id": "<ommitted>",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "foobar"
}
]
}
]
});

assert_eq!(actual, expected);
}
2 changes: 1 addition & 1 deletion tests/json/compose/option.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use pretty_assertions::assert_eq;
use serde_json::json;
use slack_blocks::{blocks, blox::*, text::ToSlackMarkdown};
use slack_blocks::{blox::*};

#[test]
pub fn option_docs_ex_1() {
Expand Down