use crate::{
Block, ContainerBlock, ContainerBlockBuilder, ContainerWidth, ErrorCategory, PlainTextInput,
ValidationError,
};
#[derive(Clone, Debug, PartialEq)]
pub struct AccordionSection(ContainerBlock);
#[derive(Clone, Debug)]
pub struct AccordionSectionBuilder(ContainerBlockBuilder);
impl AccordionSection {
pub fn builder(title: impl Into<PlainTextInput>) -> AccordionSectionBuilder {
AccordionSectionBuilder(
ContainerBlock::builder()
.title(title)
.width(ContainerWidth::Standard)
.is_collapsible(true)
.default_collapsed(true)
.has_header_divider(false),
)
}
pub fn as_container(&self) -> &ContainerBlock {
&self.0
}
pub fn into_container(self) -> ContainerBlock {
self.0
}
}
impl AccordionSectionBuilder {
pub fn subtitle(mut self, subtitle: impl Into<crate::TextInput>) -> Self {
self.0 = self.0.subtitle(subtitle);
self
}
pub fn icon(mut self, icon: crate::ImageElement) -> Self {
self.0 = self.0.icon(icon);
self
}
pub fn width(mut self, width: ContainerWidth) -> Self {
self.0 = self.0.width(width);
self
}
pub fn has_header_divider(mut self, divider: bool) -> Self {
self.0 = self.0.has_header_divider(divider);
self
}
pub fn blocks<I, T>(mut self, blocks: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<Block>,
{
self.0 = self.0.child_blocks(blocks);
self
}
pub fn block(mut self, block: impl Into<Block>) -> Self {
self.0 = self.0.child_block(block);
self
}
pub fn expanded(mut self, expanded: bool) -> Self {
self.0 = self.0.default_collapsed(!expanded);
self
}
pub fn block_id(mut self, id: impl Into<String>) -> Self {
self.0 = self.0.block_id(id);
self
}
pub fn build(self) -> Result<AccordionSection, ValidationError> {
self.0.build().map(AccordionSection)
}
}
impl From<AccordionSection> for Block {
fn from(section: AccordionSection) -> Self {
section.0.into()
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Accordion {
sections: Vec<AccordionSection>,
}
impl Accordion {
pub fn new(
sections: impl IntoIterator<Item = AccordionSection>,
) -> Result<Self, ValidationError> {
let sections: Vec<_> = sections.into_iter().collect();
if sections.is_empty() {
return Err(ValidationError::new(
ErrorCategory::MissingRequired,
"Accordion.sections",
"expected at least one section",
));
}
Ok(Self { sections })
}
pub fn sections(&self) -> &[AccordionSection] {
&self.sections
}
pub fn render(&self) -> Vec<Block> {
self.sections.iter().cloned().map(Into::into).collect()
}
}
impl IntoIterator for Accordion {
type Item = Block;
type IntoIter = std::vec::IntoIter<Block>;
fn into_iter(self) -> Self::IntoIter {
self.sections
.into_iter()
.map(Into::into)
.collect::<Vec<_>>()
.into_iter()
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Paginator {
blocks: Vec<Block>,
page: usize,
page_count: usize,
}
#[derive(Clone, Debug)]
pub struct PaginatorBuilder {
blocks: Vec<Block>,
action_id_prefix: String,
page: usize,
page_size: usize,
previous_text: String,
next_text: String,
show_page_indicator: bool,
block_id: Option<String>,
}
impl Paginator {
pub fn builder<I, T>(action_id_prefix: impl Into<String>, blocks: I) -> PaginatorBuilder
where
I: IntoIterator<Item = T>,
T: Into<Block>,
{
PaginatorBuilder {
blocks: blocks.into_iter().map(Into::into).collect(),
action_id_prefix: action_id_prefix.into(),
page: 1,
page_size: 5,
previous_text: "Previous".into(),
next_text: "Next".into(),
show_page_indicator: true,
block_id: None,
}
}
pub const fn page(&self) -> usize {
self.page
}
pub const fn page_count(&self) -> usize {
self.page_count
}
pub fn blocks(&self) -> &[Block] {
&self.blocks
}
pub fn render(&self) -> Vec<Block> {
self.blocks.clone()
}
}
impl PaginatorBuilder {
pub fn page(mut self, page: usize) -> Self {
self.page = page;
self
}
pub fn page_size(mut self, size: usize) -> Self {
self.page_size = size;
self
}
pub fn previous_text(mut self, text: impl Into<String>) -> Self {
self.previous_text = text.into();
self
}
pub fn next_text(mut self, text: impl Into<String>) -> Self {
self.next_text = text.into();
self
}
pub fn show_page_indicator(mut self, show: bool) -> Self {
self.show_page_indicator = show;
self
}
pub fn block_id(mut self, id: impl Into<String>) -> Self {
self.block_id = Some(id.into());
self
}
pub fn build(self) -> Result<Paginator, ValidationError> {
use crate::{ActionsBlock, ButtonElement, ContextBlock, MarkdownText};
let fail = |category, path, message| ValidationError::new(category, path, message);
if self.blocks.is_empty() {
return Err(fail(
ErrorCategory::MissingRequired,
"Paginator.blocks",
"expected at least one block",
));
}
if self.action_id_prefix.is_empty() {
return Err(fail(
ErrorCategory::MissingRequired,
"Paginator.action_id_prefix",
"expected a nonempty prefix",
));
}
if self.page_size == 0 {
return Err(fail(
ErrorCategory::OutOfRange,
"Paginator.page_size",
"expected a positive page size",
));
}
let page_count = self.blocks.len().div_ceil(self.page_size);
if self.page == 0 || self.page > page_count {
return Err(fail(
ErrorCategory::OutOfRange,
"Paginator.page",
"page is outside the available pages",
));
}
let mut blocks: Vec<_> = self
.blocks
.into_iter()
.skip((self.page - 1) * self.page_size)
.take(self.page_size)
.collect();
if page_count > 1 {
let mut controls = Vec::new();
if self.page > 1 {
controls.push(
ButtonElement::builder()
.text(self.previous_text)
.action_id(format!("{}.previous", self.action_id_prefix))
.value((self.page - 1).to_string())
.build()?,
);
}
if self.page < page_count {
controls.push(
ButtonElement::builder()
.text(self.next_text)
.action_id(format!("{}.next", self.action_id_prefix))
.value((self.page + 1).to_string())
.build()?,
);
}
if self.show_page_indicator {
blocks.push(
ContextBlock::builder()
.element(MarkdownText::new(format!(
"Page {} of {page_count}",
self.page
))?)
.build()?
.into(),
);
}
let mut actions = ActionsBlock::builder().elements(controls);
if let Some(id) = self.block_id {
actions = actions.block_id(id);
}
blocks.push(actions.build()?.into());
}
Ok(Paginator {
blocks,
page: self.page,
page_count,
})
}
}
impl IntoIterator for Paginator {
type Item = Block;
type IntoIter = std::vec::IntoIter<Block>;
fn into_iter(self) -> Self::IntoIter {
self.blocks.into_iter()
}
}