use crate::cell::Cell;
use crate::metadata::NotebookMetadata;
#[derive(Debug, Clone, PartialEq)]
pub struct Notebook {
pub cells: Vec<Cell>,
pub metadata: NotebookMetadata,
pub nbformat: u8,
pub nbformat_minor: u8,
pub sort_keys: bool,
}
impl Notebook {
pub fn new() -> Self {
Notebook {
cells: Vec::new(),
metadata: NotebookMetadata::default(),
nbformat: 4,
nbformat_minor: 5,
sort_keys: false,
}
}
pub fn with_cells(cells: Vec<Cell>) -> Self {
Notebook {
cells,
metadata: NotebookMetadata::default(),
nbformat: 4,
nbformat_minor: 5,
sort_keys: false,
}
}
pub fn len(&self) -> usize {
self.cells.len()
}
pub fn is_empty(&self) -> bool {
self.cells.is_empty()
}
pub fn iter(&self) -> impl Iterator<Item = &Cell> {
self.cells.iter()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Cell> {
self.cells.iter_mut()
}
pub fn code_cells(&self) -> impl Iterator<Item = &Cell> {
self.cells.iter().filter(|c| c.is_code())
}
pub fn markdown_cells(&self) -> impl Iterator<Item = &Cell> {
self.cells.iter().filter(|c| c.is_markdown())
}
}
impl Default for Notebook {
fn default() -> Self {
Self::new()
}
}
impl IntoIterator for Notebook {
type Item = Cell;
type IntoIter = std::vec::IntoIter<Cell>;
fn into_iter(self) -> Self::IntoIter {
self.cells.into_iter()
}
}
impl<'a> IntoIterator for &'a Notebook {
type Item = &'a Cell;
type IntoIter = std::slice::Iter<'a, Cell>;
fn into_iter(self) -> Self::IntoIter {
self.cells.iter()
}
}