1use std::{fs, path::Path};
2
3use crate::{
4 chunks::ChunksDiskFormat,
5 errors::RustyChunkEncError,
6 index::{read_index_disk_format, IndexDiskFormat},
7};
8
9#[derive(Debug)]
11pub struct Folder {
12 #[allow(dead_code)]
13 path: String,
14 #[allow(dead_code)]
15 chunks_files: Vec<ChunksDiskFormat>,
16 #[allow(dead_code)]
17 index: IndexDiskFormat,
18}
19
20impl Folder {
21 pub fn parse_folder(folder_path: &str) -> Result<Self, RustyChunkEncError> {
23 let index_file_path = Path::new(folder_path).join("index");
25 let index_data = fs::read(index_file_path)?;
26
27 let (_, _index) = read_index_disk_format(&index_data)?;
28
29 let chunks_folder_path = Path::new(folder_path).join("chunks");
31 let mut chunk_files = fs::read_dir(chunks_folder_path)?
32 .map(|entry| {
33 let entry = entry?;
34 let file_name = entry
35 .file_name()
36 .into_string()
37 .map_err(|_| RustyChunkEncError::InvalidFileName())?;
38 let file_type = entry.file_type()?;
39 if file_type.is_file() && file_name.chars().all(|c| c.is_ascii_digit()) {
40 Ok(Some(file_name))
41 } else {
42 Ok(None)
43 }
44 })
45 .filter_map(|result| match result {
46 Ok(None) => None,
47 Ok(Some(path)) => Some(Ok(path)),
48 Err(err) => Some(Err(err)),
49 })
50 .collect::<Result<Vec<String>, RustyChunkEncError>>()?;
51
52 chunk_files.sort();
54
55 println!("chunk_files: {:?}", chunk_files);
56
57 Ok(Folder {
58 path: folder_path.to_string(),
59 chunks_files: Vec::new(),
60 index: IndexDiskFormat::new(Vec::new()),
61 })
62 }
63}