fkl_parser/resolve/
include_resolver.rs1use std::path::PathBuf;
2
3pub fn include_resolver(base_path: &str, include_path: &str) -> Option<PathBuf> {
4 let mut base_path = PathBuf::from(base_path);
5 base_path.pop();
6 base_path.push(include_path);
7
8 base_path = base_path.canonicalize().ok()?;
9 if base_path.exists() {
10 Some(base_path)
11 } else {
12 None
13 }
14}
15
16#[cfg(test)]
17mod tests {
18 use super::*;
19
20 #[test]
21 fn test_include_resolver() {
22 let base_path = "/home/username/project/src/main.rs";
23 let include_path = "../abc.rs";
24 let result = include_resolver(base_path, include_path);
25 assert_eq!(result, None);
26 }
27
28 #[test]
29 fn test_include_resolver2() {
30 let base_path: PathBuf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
31 let include_path = "fkl_parser/src/lib.rs";
32 let result = include_resolver(&format!("{}", base_path.display()), include_path);
33 assert!(result.is_some());
34 }
35}