thot-local 0.10.0-intermediate

Local functionality for Thot data management and analysis software.
Documentation
/// High level functionality for Assets and Buckets.
use super::container::path_is_container;
use super::resources::asset::{Assets, LocalAsset};
use crate::result::{AssetError, Error, Result};
use settings_manager::local_settings::LocalSettings;
use std::io;
use std::path::{Path, PathBuf};
use thot_core::types::{ResourceId, ResourcePath};

// *****************
// *** Functions ***
// *****************

/// Initialize a file as an Asset.
/// This **does not** register it with its parent Container.
///
/// `path` may point to a non-existant file.
/// If `container` is None, assumes the parent folder is the desired Container.
///
/// # Errors
/// + If the file is already an Asset.
///
pub fn init(path: &Path, container: Option<&Path>) -> Result<ResourceId> {
    // validate container
    let container = match container {
        Some(p) => p,
        None => match path.parent() {
            Some(p) => p,
            None => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidFilename,
                    "path is not a valid filename",
                )
                .into())
            }
        },
    };

    if !path_is_container(container) {
        return Err(Error::AssetError(AssetError::PathNotAContainer(
            PathBuf::from(path),
        )));
    }

    // check if asset is already registered
    let mut assets = Assets::load(container)?;
    let a_path = ResourcePath::new(PathBuf::from(path))?;
    let a_path = Some(a_path);
    for a in &assets.assets {
        if a.path == a_path {
            return Err(Error::AssetError(AssetError::FileAlreadyAsset(
                PathBuf::from(path),
            )));
        }
    }

    // file is valid, initialize as asset
    let asset_path = ResourcePath::new(PathBuf::from(path))?;
    let asset = LocalAsset::new(asset_path)?;
    let rid = asset.properties.rid.clone();
    assets.assets.push(asset);
    assets.save()?;
    Ok(rid)
}

pub fn mv() -> Result {
    todo!();
}

pub fn delete() -> Result {
    todo!();
}

pub fn update() -> Result {
    todo!();
}

/// Make a new bucket directory.
pub fn add_bucket(name: &Path) -> Result {
    todo!();
}

/// Make an existing directory a bucket.
pub fn make_bucket(path: &Path) -> Result {
    todo!();
}

/// Returns whether the path is an Asset registered with the given Container.
pub fn path_is_asset(path: &Path, container: &Path) -> bool {
    todo!();
}

/// Moves up the path until the first Container is reached.
pub fn container_from_path_ancestor(path: &Path) -> Result<PathBuf> {
    match path.ancestors().find(|p| path_is_container(p)) {
        None => {
            return Err(Error::AssetError(AssetError::ContainerNotFound(
                path.to_path_buf(),
            )))
        }
        Some(p) => Ok(p.to_path_buf()),
    }
}

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