use super::project;
use super::resources::asset::Assets;
use super::resources::container::{Container, ContainerSettings};
use crate::common::{container_file_of, thot_dir_of};
use crate::result::{ContainerError, Error, Result};
use settings_manager::local_settings::{LocalSettings, LockSettingsFile};
use std::path::{Path, PathBuf};
use std::{fs, io};
use thot_core::types::ResourceId;
pub fn new(path: &Path) -> Result<ResourceId> {
if path.exists() {
return Err(io::Error::new(io::ErrorKind::IsADirectory, "path already exists").into());
}
fs::create_dir_all(path)?;
init(path)
}
pub fn init(path: &Path) -> Result<ResourceId> {
if !path.exists() {
return Err(io::Error::new(io::ErrorKind::NotADirectory, "path does not exist").into());
}
if project::path_is_resource(path) {
if path_is_container(path) {
let container = Container::load(path)?;
return Ok(container.properties.rid.clone());
}
} else {
let thot_dir = thot_dir_of(path);
fs::create_dir(&thot_dir)?;
}
let mut container = Container::new()?;
container.set_base_path(PathBuf::from(path))?;
container.acquire_lock()?;
container.save()?;
let mut settings = ContainerSettings::new();
settings.set_base_path(PathBuf::from(path))?;
settings.acquire_lock()?;
settings.save()?;
let mut assets = Assets::new();
assets.set_base_path(PathBuf::from(path))?;
assets.acquire_lock()?;
assets.save()?;
Ok(container.properties.rid.clone())
}
pub fn mv(rid: ResourceId, to: &Path) -> Result {
todo!();
}
pub fn remove(rid: ResourceId) -> Result {
todo!();
}
pub fn delete(rid: ResourceId) -> Result {
todo!();
}
pub fn update(container: Container) -> Result {
todo!();
}
pub fn path_is_container(path: &Path) -> bool {
let c_path = container_file_of(path);
c_path.exists()
}
pub fn init_child(child: &Path, container: Option<&Path>) -> Result<ResourceId> {
if !child.is_dir() {
return Err(io::Error::new(
io::ErrorKind::NotADirectory,
"child path is not a directory",
)
.into());
}
let parent = match child.parent() {
Some(p) => p,
None => {
return Err(io::Error::new(
io::ErrorKind::InvalidFilename,
"Invalid path for container",
)
.into())
}
};
let container = match container {
None => parent,
Some(p) => p,
};
if parent != container {
return Err(Error::ContainerError(ContainerError::InvalidChildPath(
PathBuf::from(child),
)));
}
if !path_is_container(container) {
return Err(Error::ContainerError(ContainerError::PathNotAContainer(
PathBuf::from(container),
)));
}
let rid = init(child)?;
let mut cont = Container::load(container)?;
cont.register_child(rid.clone());
cont.save()?;
Ok(rid)
}
pub fn new_child(child: &Path, container: Option<&Path>) -> Result<ResourceId> {
fs::create_dir(child)?;
init_child(child, container)
}
pub fn children_paths(path: &Path) -> Result<Vec<PathBuf>> {
if !path_is_container(path) {
return Err(Error::ContainerError(ContainerError::PathNotAContainer(
PathBuf::from(path),
)));
}
let container = Container::load(path)?;
container.children_paths()
}
#[cfg(test)]
#[path = "./container_test.rs"]
mod container_test;