cocomo-core 0.0.1

COmpare, COpy & MOve directories and files [core library].
Documentation
// ---------------------------------------------------------------------------
// Copyright:   (c) 2022 ff. Michael Amrhein (michael@adrhinum.de)
// License:     This program is part of a larger application. For license
//              details please read the file LICENSE.TXT provided together
//              with the application.
// ---------------------------------------------------------------------------
// $Source: cocomo-core/src/fs_item.rs $
// $Revision: 2022-06-01T08:44:48+02:00 $

use std::{
    fs, io,
    path::{Path, PathBuf},
};

#[derive(Debug)]
pub enum ItemType {
    Directory,
    File,
}

impl Default for ItemType {
    fn default() -> Self {
        Self::Directory
    }
}

#[derive(Debug)]
pub struct FSItem {
    pub path: PathBuf,
    pub item_type: ItemType,
    pub metadata: fs::Metadata,
}

impl TryFrom<&String> for FSItem {
    type Error = io::Error;

    fn try_from(s: &String) -> Result<Self, Self::Error> {
        let path = Path::new(&s).canonicalize()?;
        let metadata = fs::metadata(&path)?;
        Ok(Self {
            path,
            item_type: if metadata.is_dir() {
                ItemType::Directory
            } else {
                ItemType::File
            },
            metadata,
        })
    }
}