pub mod build;
pub mod partial;
pub mod clone;
pub mod meta;
use crate::{ Block, Layer, Layout };
use serde::{ Serialize, Deserialize, de::DeserializeOwned };
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(bound(
serialize = "B: Block + Serialize",
deserialize = "B: Block + DeserializeOwned"))]
pub struct Stack<B: Block> {
layouts: Vec<Layout>,
blocks: Vec<B>
}
impl<B: Block> Stack<B> {
pub fn new() -> Self {
Self::default()
}
pub fn layouts(&self) -> &Vec<Layout> {
&self.layouts
}
pub(crate) fn layouts_mut(&mut self) -> &mut Vec<Layout> {
&mut self.layouts
}
pub fn blocks(&self) -> &Vec<B> {
&self.blocks
}
pub(crate) fn blocks_mut(&mut self) -> &mut Vec<B> {
&mut self.blocks
}
}
impl<B: Block> Stack<B> {
pub fn map<C: Block, T: Fn(&B) -> C>(&self, t: T) -> Stack<C> {
let mapped_blocks: Vec<C> = self.blocks()
.iter()
.map(t)
.collect();
let mut mapped_stack = Stack::<C>::new();
mapped_stack.blocks = mapped_blocks;
mapped_stack.layouts = self.layouts.clone();
mapped_stack
}
}
#[cfg(test)] mod test {
use crate::Stack;
use crate::block::{ Block, test::TestBlock };
use crate::types::layer::{ Layer, test::test_layer };
pub(crate) fn test_stack() -> Stack<TestBlock> {
let mut stack = Stack::<TestBlock>::new();
stack.add_layers(vec![test_layer(); 3]);
stack
}
#[test] fn new_stack_test() {
let stack = test_stack();
assert_eq!(stack.layouts.len(), 3);
assert_eq!(stack.blocks.len(), 9);
}
}