-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathroot.rs
68 lines (53 loc) · 1.45 KB
/
root.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
use std;
use std::sync::Arc;
#[derive(Debug)]
pub struct Root {
items: std::collections::HashMap<String,Box<dyn crate::Object>>
}
impl Root {
pub fn new() -> Root {
Root {
items: std::collections::HashMap::new(),
}
}
pub fn is_empty(&self) -> bool { self.items.is_empty() }
pub fn add<T: 'static + crate::Object>(&mut self, object: T) {
self.add_boxed(Box::new(object))
}
pub fn add_boxed(&mut self, object: Box<dyn crate::Object>) {
let name = object.id().to_string();
debug_assert!(name != "0");
debug_assert!(name != "-1");
self.items.insert(name, object);
}
}
impl crate::Object for Arc<Root> {
fn id(&self) -> &str { "0" }
fn parent_id(&self) -> &str { "-1" }
fn file_type(&self) -> crate::Type { crate::Type::Directory }
fn title(&self) -> String {
"Rusty Media".to_string()
}
fn is_dir(&self) -> bool { true }
fn lookup(&self, id: &str) -> crate::Result<Box<dyn crate::Object>> {
debug_assert!(id != "-1");
if id == "0" {
return Ok(Box::new(self.clone()))
}
let (first, suffix) = match id.find('/') {
Some(i) => (&id[..i], &id[i+1..]),
None => (id, ""),
};
match self.items.get(first) {
Some(obj) => obj.lookup(suffix),
None => return Err(
crate::ErrorKind::NotFound(format!(
"{:?} not found looking for {:?}", first, id)).into())
}
}
fn children(&self) -> crate::Result<Vec<Box<dyn crate::Object>>> {
self.items.values()
.map(|v| v.lookup(""))
.collect()
}
}