use std::fmt;
use serde::{Deserialize, Serialize};
use crate::convert;
pub mod actions;
#[doc(inline)]
pub use actions::Actions;
pub mod context;
#[doc(inline)]
pub use context::Context;
pub mod file;
#[doc(inline)]
pub use file::File;
pub mod image;
#[doc(inline)]
pub use image::Image;
pub mod input;
#[doc(inline)]
pub use input::Input;
pub mod section;
#[doc(inline)]
pub use section::Section;
pub mod header;
#[doc(inline)]
pub use header::Header;
#[derive(Hash, PartialEq, Serialize, Deserialize, Debug)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Block<'a> {
Section(Section<'a>),
Divider,
Image(Image<'a>),
Actions(Actions<'a>),
Context(Context<'a>),
Input(Input<'a>),
Header(Header<'a>),
File(File<'a>),
}
impl fmt::Display for Block<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let kind = match self {
| Block::Header { .. } => "Header",
| Block::Section { .. } => "Section",
| Block::Divider => "Divider",
| Block::Image { .. } => "Image",
| Block::Actions { .. } => "Actions",
| Block::Context { .. } => "Context",
| Block::Input { .. } => "Input",
| Block::File { .. } => "File",
};
write!(f, "{}", kind)
}
}
impl<'a> Block<'a> {
#[cfg(feature = "validation")]
#[cfg_attr(docsrs, doc(cfg(feature = "validation")))]
pub fn validate(&self) -> crate::val_helpr::ValidationResult {
use Block::*;
match self {
| Section(contents) => contents.validate(),
| Image(contents) => contents.validate(),
| Actions(contents) => contents.validate(),
| Context(contents) => contents.validate(),
| Input(contents) => contents.validate(),
| Header(contents) => contents.validate(),
| File(contents) => contents.validate(),
| 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));
#[derive(Clone, Debug, Deserialize, Hash, PartialEq, Serialize)]
pub struct UnsupportedElement<'a> {
context: String,
element: crate::elems::BlockElement<'a>,
}
impl<'a> std::fmt::Display for UnsupportedElement<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f,
"(In {}) Block element not supported: {:#?}",
self.context, self.element)
}
}
impl<'a> std::error::Error for UnsupportedElement<'a> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
#[cfg(feature = "validation")]
fn validate_block_id(id: &std::borrow::Cow<str>)
-> crate::val_helpr::ValidatorResult {
crate::val_helpr::below_len("block_id", 255, id)
}