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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*-
 * shm-rs - a scheme serialization lib
 * Copyright (C) 2021  Aleksandr Morozov
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 *  file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */


use std::path::Path;

use super::{config_cnode::CNode, config_loader::ConfigLoaderSource};

/// An implementation of loader from the [CNode] structure.
pub struct ConfigVirtualFileSystem
{
    cnode: CNode,
}


impl ConfigLoaderSource for ConfigVirtualFileSystem
{
    fn is_local(&self) -> bool 
    {
        return false;
    }

    fn read_file(&self, path: &Path, _dynenv_name: &str) -> Result<String, String> 
    {
        //traverse the cnodes to find required file
        return self.cnode.open(path);
    }

    fn read_raw_file(&self, path: &Path) -> Result<String, String> 
    {
        return self.cnode.open(path);
    }

    fn get_serializator(&self, serial_name: &str) -> Result<std::rc::Rc<crate::DynEnvironment>, String> 
    {
        return Err(format!("can not get serializator: '{}', ConfigVirtualFileSystem does not have this info", serial_name));
    }
}

impl ConfigVirtualFileSystem
{
    /// Creates ew instance from the serialized [CNode] data.
    pub 
    fn new<'t>(serialized: &String) -> Result<Self, String>
    {
        let cnode = 
            CNode::deserialize_from_string(serialized)?;

        return Ok(
            Self
            {
                cnode: cnode
            }
        );
    }
}