get_relative_path

Function get_relative_path 

Source
pub fn get_relative_path<P: AsRef<Path>, B: AsRef<Path>>(
    file: P,
    relative_to: B,
) -> Option<String>
Expand description

Get the path of the file as a relative path to a base directory.

§Arguments

  • file - The file path to convert.
  • relative_to - The base path to make the file path relative to.

§Returns

The relative path as a PathBuf, or None if the path cannot be made relative.

§Example

use std::path::Path;
use agent_chain_core::api::get_relative_path;

let base = Path::new("/home/user/project");
let file = Path::new("/home/user/project/src/main.rs");
let relative = get_relative_path(file, base);
assert_eq!(relative, Some("src/main.rs".into()));