rlist_drivers/
driver_index.rs

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use crate::onedrive::{OnedriveConfig, OnedriveDriver};
use crate::static_link::static_driver;
use rlist_driver_macro::rlist_driver_index;
use rlist_vfs::driver::{CloudDriver, GetVfs};

#[rlist_driver_index]
pub enum DriverIndex {
    #[rlist_driver(name = "onedrive")]
    OneDrive(OnedriveConfig),

    #[rlist_driver(name = "static")]
    Static(static_driver::StaticDir),
}

impl DriverIndex {
    pub async fn create_instance(self) -> Box<dyn GetVfs> {
        match self {
            DriverIndex::OneDrive(config) => {
                let driver = OnedriveDriver::load_config(config).await;
                Box::new(OnedriveDriver::new(driver).await)
            }
            DriverIndex::Static(config) => {
                let driver = static_driver::StaticDriver::load_config(config).await;
                Box::new(static_driver::StaticDriver::new(driver).await)
            }
        }
    }
}

#[cfg(test)]
mod test {
    use crate::driver_index::DriverIndex::Static;
    use rlist_vfs::VfsBasicMeta;

    #[test]
    fn test_des_static() {
        let static_dir_json = r#"
{
  "driver": "static",
  "config": {
    "name": "root",
    "size": 2048,
    "last_modified": "2024-04-22T12:00:00Z",
    "files": [
      {
        "name": "example.txt",
        "size": 1024,
        "last_modified": "2024-04-21T11:00:00Z",
        "links": [
          "https://example.com/example.txt"
        ]
      },
      {
        "name": "image.png",
        "size": 512,
        "last_modified": "2024-04-20T10:00:00Z",
        "links": [
          "https://example.com/image.png"
        ]
      }
    ],
    "subdirectories": [
      {
        "name": "docs",
        "size": 512,
        "last_modified": "2024-04-19T09:00:00Z",
        "files": [
          {
            "name": "readme.md",
            "size": 256,
            "last_modified": "2024-04-18T08:00:00Z",
            "links": [
              "https://example.com/docs/readme.md"
            ]
          }
        ],
        "subdirectories": []
      }
    ]
  }
}
        "#;
        let result = serde_json::from_str::<super::DriverIndex>(static_dir_json);
        assert!(result.is_ok());
        let result = result.unwrap();
        let result = match result {
            Static(config) => config,
            _ => panic!("Unexpected result"),
        };
        assert_eq!(result.name(), "root");
    }
}