intext/
state.rs

1use crate::nodes::{NodeAttributes, Nodes};
2use crate::plugins::Plugins;
3use std::cell::RefCell;
4use std::rc::Rc;
5use std::collections::HashMap;
6use std::fmt::Debug;
7use std::path::PathBuf;
8
9pub type Shared<T> = Rc<RefCell<T>>;
10pub type SharedState = Shared<State>;
11
12#[derive(Clone, Debug)]
13pub struct State {
14    pub nodes: Nodes,
15    pub paths: Vec<PathBuf>,
16    pub plugins: Plugins,
17    pub tags: HashMap<String, NodeAttributes>,
18}
19
20impl Default for State {
21    fn default() -> Self {
22        State {
23            nodes: Nodes::new(),
24            paths: vec![std::env::current_dir().unwrap()],
25            plugins: Plugins::new(),
26            tags: HashMap::new(),
27        }
28    }
29}
30
31impl State {
32    pub fn shareable() -> SharedState {
33        Shared::share(State::default())
34    }
35
36    pub fn current_path(&self) -> PathBuf {
37        self.paths.last().unwrap().into()
38    }
39}
40
41pub trait SharedContructor<T> {
42    fn share(item: T) -> Shared<T>;
43}
44
45impl<T> SharedContructor<T> for Rc<RefCell<T>> {
46    fn share(item: T) -> Shared<T> {
47        Rc::new(RefCell::new(item))
48    }
49}