ll_rs/
entry.rs

1extern crate libc;
2
3use chrono::{DateTime, Local};
4//use libc::{S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR, S_IXGRP, S_IXOTH, S_IXUSR};
5use std::os::unix::fs::PermissionsExt;
6use std::{error::Error, path::PathBuf};
7
8#[derive(Debug)]
9pub struct Entry {
10    is_dir: bool,
11    mode: u32,
12    size: f64,
13    modified: DateTime<Local>,
14    path: PathBuf,
15}
16
17impl Entry {
18    pub fn new(path: PathBuf) -> Result<Self, Box<dyn Error>> {
19        if !path.exists() {
20            return Err(Box::from(format!(
21                "Path doesn't exists or syslink is broken. Path: {:?}",
22                path
23            )));
24        }
25        let metadata = path.metadata()?;
26        let is_dir = metadata.is_dir();
27        let mode = metadata.permissions().mode();
28        let size = metadata.len() as f64;
29        let modified: DateTime<Local> = DateTime::from(metadata.modified()?);
30
31        Ok(Self {
32            is_dir,
33            mode,
34            size,
35            modified,
36            path,
37        })
38    }
39}