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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use failure::Error;
use nom::*;
use crate::core::{checked_byte_count, decompress, Context, Source, TKeyHeader};
use crate::tree_reader::{ttree, Tree};
#[derive(Debug)]
pub struct FileItem {
source: Source,
tkey_hdr: TKeyHeader,
}
impl FileItem {
pub(crate) fn new(tkey_hdr: &TKeyHeader, source: Source) -> FileItem {
FileItem {
source,
tkey_hdr: tkey_hdr.to_owned(),
}
}
pub async fn as_tree(&self) -> Result<Tree, Error> {
self.parse_with(ttree).await
}
pub fn verbose_info(&self) -> String {
format!("{:#?}", self.tkey_hdr)
}
pub fn name(&self) -> String {
format!(
"`{}` of type `{}`",
self.tkey_hdr.obj_name, self.tkey_hdr.class_name
)
}
pub(crate) async fn parse_with<O, F>(&self, parser: F) -> Result<O, Error>
where
F: for<'s> Fn(&'s [u8], &'s Context<'s>) -> IResult<&'s [u8], O>,
{
let start = self.tkey_hdr.seek_key + self.tkey_hdr.key_len as u64;
let len = self.tkey_hdr.total_size - self.tkey_hdr.key_len as u32;
let comp_buf = self.source.fetch(start, len as u64).await?;
let buf = {
if self.tkey_hdr.total_size < self.tkey_hdr.uncomp_len {
let (_, buf) = decompress(comp_buf.as_slice()).unwrap();
buf
} else {
comp_buf
}
};
let s = buf.as_slice();
let k_map_offset = 2;
let context = Context {
source: self.source.clone(),
offset: (self.tkey_hdr.key_len + k_map_offset) as u64,
s,
};
let res = length_value!(s, checked_byte_count, call!(&parser, &context));
match res {
Ok((_, obj)) => Ok(obj),
_ => Err(format_err!("Supplied parser failed!")),
}
}
}
#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
use crate::core::RootFile;
use std::path::Path;
#[tokio::test]
async fn open_simple() {
let path = Path::new("./src/test_data/simple.root");
let f = RootFile::new(path)
.await
.expect("Failed to open file");
assert_eq!(f.items().len(), 1);
assert_eq!(f.items()[0].tkey_hdr.obj_name, "tree");
assert_eq!(f.streamers().await.unwrap().len(), 18);
}
#[tokio::test]
#[cfg(all(not(target_os = "macos"), not(target_arch = "wasm32")))]
async fn open_esd() {
use alice_open_data;
let path = alice_open_data::test_file().unwrap();
let f = RootFile::new(path.as_path())
.await
.expect("Failed to open file");
assert_eq!(f.items().len(), 2);
assert_eq!(f.items()[0].tkey_hdr.obj_name, "esdTree");
assert_eq!(f.items()[1].tkey_hdr.obj_name, "HLTesdTree");
assert_eq!(f.streamers().await.unwrap().len(), 87);
}
}