1use quilt_cell::Cell;
4use quilt_zin::Zin;
5use quilt_zout::Zout;
6use std::path::PathBuf;
7
8#[derive(Debug, Clone)]
9pub struct FileCell {
10 pub path: PathBuf,
11 pub cell: Cell,
12}
13
14impl FileCell {
15 pub fn new(path: PathBuf) -> Self {
16 let mut z_in = Zin::new();
17 if let Ok(data) = std::fs::read_to_string(&path) {
18 z_in.put("content", data.len() as f64);
19 }
20 let cell = Cell::new(path.display().to_string().as_str())
21 .with_kind("file");
22 Self { path, cell }
23 }
24 pub fn size(&self) -> std::io::Result<u64> {
25 Ok(std::fs::metadata(&self.path)?.len())
26 }
27 pub fn read(&self) -> std::io::Result<String> {
28 std::fs::read_to_string(&self.path)
29 }
30 pub fn write(&self, content: &str) -> std::io::Result<()> {
31 std::fs::write(&self.path, content)
32 }
33}
34
35pub fn list_dir(path: &str) -> std::io::Result<Vec<FileCell>> {
36 let entries = std::fs::read_dir(path)?;
37 let mut cells = vec![];
38 for entry in entries {
39 let entry = entry?;
40 cells.push(FileCell::new(entry.path()));
41 }
42 Ok(cells)
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48 #[test]
49 fn test_file_cell() {
50 let fc = FileCell::new(PathBuf::from("."));
51 assert!(fc.size().is_ok());
52 }
53}