worm_hole 1.6.2

CLI tool to easily jump between directories
// Copyright (C) 2025 Rignchen
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use std::fs::canonicalize;
use std::str::FromStr;

#[derive(Debug, Clone)]
pub struct DirPath(String);
impl FromStr for DirPath {
	type Err = String;

	/// Parse a string into a DirPath.
	/// The string must be a valid path
	/// And the path must be a directory.
	fn from_str(s: &str) -> Result<Self, Self::Err> {
		let path = canonicalize(s).map_err(|_| format!("Path {} does not exist", s))?;
		if !path.is_dir() {
			return Err(format!("{} is not a directory", s));
		}
		Ok(Self(path.to_string_lossy().to_string()))
	}
}

impl DirPath {
	/// Get the string representation of the path.
	/// The path given is the absolute path.
	pub fn str(&self) -> &str {
		&self.0
	}
}

#[derive(Debug, Clone)]
pub struct FilePath(String);
impl FromStr for FilePath {
	type Err = String;

	/// Parse a string into a FilePath.
	/// The string must be a valid path
	/// And the path must be a file.
	fn from_str(s: &str) -> Result<Self, Self::Err> {
		let path = canonicalize(s).map_err(|_| format!("Path {} does not exist", s))?;
		if !path.is_file() {
			return Err(format!("{} is not a file", s));
		}
		Ok(Self(path.to_string_lossy().to_string()))
	}
}

impl FilePath {
	/// Get the string representation of the path.
	/// The path given is the absolute path.
	pub fn str(&self) -> &str {
		&self.0
	}
}

#[cfg(test)]
mod test {
	use super::*;

	mod dir_path {
		use super::*;

		#[test]
		fn path_not_exist() {
			let path = "not_a_path".parse::<DirPath>().unwrap_err();
			assert_eq!(path, "Path not_a_path does not exist");
		}

		#[test]
		fn path_is_not_dir() {
			let error = "Cargo.toml".parse::<DirPath>().unwrap_err();
			assert_eq!(error, "Cargo.toml is not a directory");
		}

		#[test]
		fn path_is_dir() {
			let path = "/home".parse::<DirPath>().unwrap();
			assert_eq!(path.str(), "/home");
		}
	}

	mod file_path {
		use super::*;

		#[test]
		fn path_not_exist() {
			let path = "not_a_path".parse::<FilePath>().unwrap_err();
			assert_eq!(path, "Path not_a_path does not exist");
		}

		#[test]
		fn path_is_not_file() {
			let error = "/usr/bin".parse::<FilePath>().unwrap_err();
			assert_eq!(error, "/usr/bin is not a file");
		}

		#[test]
		fn path_is_file() {
			let path = "/init".parse::<FilePath>().unwrap();
			assert_eq!(path.str(), "/init");
		}
	}
}