Function first_path_from_str

Source
pub fn first_path_from_str(s: &str) -> Result<PathBuf, Box<dyn Error>>
Expand description

Parse a path from a string

§Example

use std::path::PathBuf;

let haystack = r#"multi line
test string with/path/file.txt is
valid"#;
let path = first_path_from_str(haystack).unwrap();
assert_eq!(path, PathBuf::from("with/path/file.txt"));

// No path in string is an error
let haystack = "Random string with no path";
assert!(first_path_from_str(haystack).is_err());

// Path with no leading '/' and no file extension is OK
let haystack = "foo app/3-_2/t/3 bar";
let path = first_path_from_str(haystack).unwrap();
assert_eq!(path, PathBuf::from("app/3-_2/t/3"));

// More realistic example
let haystack = r#" ERROR: Logfile of failure stored in: /app/yocto/build/tmp/work/x86_64-linux/sqlite3-native/3.43.2/temp/log.do_fetch.21616"#;
let path = first_path_from_str(haystack).unwrap();
assert_eq!(
  path,
 PathBuf::from("/app/yocto/build/tmp/work/x86_64-linux/sqlite3-native/3.43.2/temp/log.do_fetch.21616")
);

§Errors

This function returns an error if no valid path is found in the string