Skip to content

Commit

Permalink
Update pathutil.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Feb 16, 2024
1 parent abc967e commit 0b618da
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions crates/erg_common/pathutil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,12 @@ pub fn remove_verbatim(path: &Path) -> String {
/// e.g.
/// ```txt
/// http.d/client.d.er -> http.client
/// .../torch/1.0.0/src/lib.d.er -> torch
/// .../torch/1.0.0/src/random.d.er -> torch/random
/// $ERG_PATH/pkgs/certified/torch/1.0.0/src/lib.d.er -> torch
/// $ERG_PATH/pkgs/certified/torch/1.0.0/src/random.d.er -> torch/random
/// /users/foo/torch/src/lib.d.er -> torch
/// math.d.er -> math
/// ```
/// FIXME: split by `.` instead of `/`
pub fn mod_name(path: &Path) -> Str {
let path = match path.strip_prefix(erg_pkgs_path()) {
Ok(path) => {
Expand All @@ -267,6 +269,32 @@ pub fn mod_name(path: &Path) -> Str {
.join("/");
return Str::rc(format!("{mod_root}/{sub}").trim_end_matches('/'));
}
// using local or git path
Err(_) if path.display().to_string().split("/src/").count() > 1 => {
// <mod_root>/src/<sub>
let path = path.display().to_string();
let mod_root = path
.split("/src/")
.next()
.unwrap()
.split('/')
.last()
.unwrap();
let sub = path
.split("/src/")
.nth(1)
.unwrap()
.split('/')
.map(|c| {
c.trim_end_matches("lib.d.er")
.trim_end_matches(".d.er")
.trim_end_matches(".d")
.to_string()
})
.collect::<Vec<_>>()
.join("/");
return Str::rc(format!("{mod_root}/{sub}").trim_end_matches('/'));
}
Err(_) => path,
};
let mut name = path
Expand Down

0 comments on commit 0b618da

Please sign in to comment.