tejar/
read.rs

1#[derive(Clone)]
2pub struct TejarRecord {
3    pub data_file_name: String,
4    pub file_name: String,
5    pub offset: u32,
6    pub file_size: u32,
7    pub content_type: String,
8    pub timestamp: u64,
9    pub checksum: String,
10}
11
12#[derive(Clone)]
13pub struct Reader {
14    pub list: Vec<TejarRecord>,
15}
16
17pub struct FileInfo {
18    pub data_file_path: String,
19    pub content_type: String,
20    pub offset: u32,
21    pub file_size: u32,
22    pub checksum: String,
23}
24
25impl Reader {
26    pub fn get_file_info(&self, path: &std::path::Path) -> Option<FileInfo> {
27        self.list
28            .iter()
29            .find(|file_record| std::path::Path::new(&file_record.file_name).eq(path))
30            .map(|file_record| FileInfo {
31                data_file_path: file_record.data_file_name.to_string(),
32                content_type: file_record.content_type.to_string(),
33                offset: file_record.offset,
34                file_size: file_record.file_size,
35                checksum: file_record.checksum.to_string(),
36            })
37    }
38}
39
40pub fn reader(list_content: &str) -> Result<Reader, crate::error::ReadError> {
41    let list = crate::create::List::parse_list(list_content)?;
42    Ok(Reader {
43        list: list
44            .records
45            .into_iter()
46            .map(|r| TejarRecord {
47                data_file_name: r.data_file_name,
48                file_name: r.file_name,
49                offset: r.start,
50                file_size: r.size,
51                content_type: r.content_type,
52                timestamp: r.timestamp,
53                checksum: r.checksum,
54            })
55            .collect(),
56    })
57}