1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use std::rc::Rc;

use crate::{Storage, IdPath, Id, IdPathBuf};

/// A context used during rendering that tracks the path to the current view and
/// holds a reference to internal storage.
pub struct Context {
    id_path: IdPathBuf,
    storage: Rc<Storage>,
}

impl Context {
    pub fn new(storage: Rc<Storage>) -> Self {
        Self {
            id_path: IdPathBuf::root(),
            storage,
        }
    }

    pub fn storage(&self) -> &Rc<Storage> {
        &self.storage
    }

    pub fn id_path(&self) -> &IdPath {
        &self.id_path
    }

    pub fn child(&self, id: impl Into<Id>) -> Self {
        Self {
            id_path: self.id_path.child(id),
            storage: self.storage.clone(),
        }
    }
}