thot-local 0.10.0-intermediate

Local functionality for Thot data management and analysis software.
Documentation
//! Functionality to handle Scripts at a system level.
use super::collections::scripts::Scripts;
use crate::result::{Error, Result};
use settings_manager::prelude::{ListSetting, SystemSettings};
use std::path::Path;
use std::{fs, io};
use thot_core::system::Script;
use uuid::Uuid;

// **************
// *** Script ***
// **************

/// Make the given file a Script.
pub fn make_script(file: &Path) -> Result {
    if !file.exists() {
        return Err(io::Error::new(io::ErrorKind::NotFound, "script file does not exist").into());
    }

    if !file.is_file() {
        return Err(
            io::Error::new(io::ErrorKind::IsADirectory, "script file is not a file").into(),
        );
    }

    let abs_path = match fs::canonicalize(file) {
        Ok(path) => path,
        Err(err) => return Err(err.into()),
    };

    let script = Script::new(abs_path);

    let mut scripts = match Scripts::load() {
        Ok(sets) => sets,
        Err(err) => return Err(Error::SettingsError(err)),
    };

    scripts.push(script);
    scripts.save()?;

    // success
    Ok(())
}

pub fn r#move(id: Uuid, path: &Path) -> Result {
    todo!();
}

/// Finds a script given its path.
pub fn script_by_path(path: &Path) -> Result<Script> {
    todo!();
}

#[cfg(test)]
#[path = "./scripts_test.rs"]
mod scripts_test;