Skip to content

Commit

Permalink
fix rel_import
Browse files Browse the repository at this point in the history
  • Loading branch information
millergarym committed Apr 4, 2023
1 parent a9d4fc7 commit e46f7e1
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions rust/compiler/src/cli/tsgen/utils.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
pub fn rel_import(src: &String, dst: &String) -> String {
let src_v: Vec<&str> = src.split(['.']).collect();
// strip off the name, only leave the path
let src_v = &src_v[..src_v.len() - 1];
let dst_v: Vec<&str> = dst.split(['.']).collect();
let mut common = 0;
let mut src_i = src_v.iter();
let mut dst_i = dst_v.iter();
let last = dst_v.last().unwrap();
let dst_v = &dst_v[..dst_v.len() - 1];
let mut src_i = src_v.iter().peekable();
let mut dst_i = dst_v.iter().peekable();
let mut import = String::new();
import.push_str("./");
while let (Some(sel), Some(del)) = (&src_i.next(), &dst_i.next()) {
import.push_str(".");
while let (Some(sel), Some(del)) = (&src_i.peek(), &dst_i.peek()) {
if sel != del {
break;
}
common += 1;
src_i.next();
dst_i.next();
}
// dbg!(&src_v);
// dbg!(common);
// dbg!(&dst_v);
import.push_str("../".repeat(src_v.len() - common).as_str());
if common == dst_v.len() {
import.push_str("../");
import.push_str(dst_v.join("/").as_str());
} else {
import.push_str(dst_v[common..].join("/").as_str());
while let Some(_) = &src_i.next() {
import.push_str("/..");
}
while let Some(del) = &dst_i.next() {
import.push_str("/");
import.push_str(del);
}
import.push_str("/");
import.push_str(last);
import
}

Expand All @@ -50,7 +50,7 @@ mod tests {
),
("test 04", "common.adminui.api", "common", "./../../common"),
("test 05", "common.adminui.api", "common.db", "./../db"),
("test 06", "common.adminui.api", "common.adminui", "./../common/adminui"),
("test 06", "common.adminui.api", "common.adminui", "./../adminui"),
];

for t in tests {
Expand Down

0 comments on commit e46f7e1

Please sign in to comment.