Skip to main content

simple_fs/common/
system.rs

1use crate::{Error, Result, SPath};
2use std::path::Path;
3
4/// Returns the home directory of the current user as an `SPath`.
5pub fn home_dir() -> Result<SPath> {
6	let path = std::env::home_dir().ok_or(Error::HomeDirNotFound)?;
7	SPath::from_std_path(path)
8}
9
10/// Returns the current directory as an `SPath`.
11pub fn current_dir() -> Result<SPath> {
12	let path = std::env::current_dir().map_err(|e| Error::FileCantRead((Path::new("."), e).into()))?;
13	SPath::from_std_path_buf(path)
14}
15
16// region:    --- Tests
17
18#[cfg(test)]
19mod tests {
20	type Result<T> = core::result::Result<T, Box<dyn std::error::Error>>;
21
22	use super::*;
23
24	#[test]
25	fn test_common_system_home_dir_simple() -> Result<()> {
26		// -- Exec
27		let home = home_dir()?;
28
29		// -- Check
30		assert!(home.exists(), "Home directory should exist");
31		assert!(home.is_absolute(), "Home directory should be an absolute path");
32
33		Ok(())
34	}
35
36	#[test]
37	fn test_common_system_current_dir_simple() -> Result<()> {
38		// -- Exec
39		let current = current_dir()?;
40
41		// -- Check
42		assert!(current.exists(), "Current directory should exist");
43		assert!(current.is_absolute(), "Current directory should be an absolute path");
44
45		Ok(())
46	}
47}
48
49// endregion: --- Tests