notebook_types/v1/
notebook.rs

1use crate::v0;
2
3use super::cell::NotebookCell;
4
5use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7use tsify::Tsify;
8use uuid::Uuid;
9
10#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Tsify)]
11#[tsify(into_wasm_abi, from_wasm_abi, hashmap_as_object)]
12pub struct Notebook<T> {
13    #[serde(rename = "cellContents")]
14    pub cell_contents: HashMap<Uuid, NotebookCell<T>>,
15    #[serde(rename = "cellOrder")]
16    pub cell_order: Vec<Uuid>,
17}
18
19impl<T> Notebook<T> {
20    pub fn cells(&self) -> impl Iterator<Item = &NotebookCell<T>> {
21        self.cell_order.iter().filter_map(|id| self.cell_contents.get(id))
22    }
23
24    pub fn migrate_from_v0(old: v0::Notebook<T>) -> Self {
25        let mut cell_contents = HashMap::new();
26        let mut cell_order = Vec::new();
27
28        for old_cell in old.cells {
29            let id = match old_cell {
30                v0::NotebookCell::RichText { id, .. }
31                | v0::NotebookCell::Formal { id, .. }
32                | v0::NotebookCell::Stem { id } => id,
33            };
34
35            cell_order.push(id);
36            cell_contents.insert(id, old_cell);
37        }
38
39        Notebook {
40            cell_contents,
41            cell_order,
42        }
43    }
44}