nestrs-cli-rs 0.1.0

Rust port of the Nest CLI for the nestrs organization.
Documentation
//! Upstream source: `../nest-cli/lib/compiler/helpers/copy-path-resolve.ts`.

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

pub fn copy_path_resolve(file_path: &str, out_dir: &str, up: usize) -> Result<PathBuf, String> {
    Ok(Path::new(out_dir).join(deal_with(file_path, up)?))
}

pub fn deal_with(in_path: &str, up: usize) -> Result<PathBuf, String> {
    if up == 0 {
        return Ok(PathBuf::from(in_path));
    }

    if depth(in_path) < up.saturating_sub(1) {
        return Err("Path outside of project folder is not allowed".to_string());
    }

    Ok(Path::new(in_path).components().skip(up).collect())
}

pub fn depth(path: &str) -> usize {
    Path::new(path).components().count().saturating_sub(1)
}