Skip to content

Commit

Permalink
Add 'Base' to import.
Browse files Browse the repository at this point in the history
This allows one to specify paths from the root of the workspace, and is
somewhat less hacky than embedding file: ... hardcoded into the preludes.
  • Loading branch information
flaviusb committed Dec 29, 2022
1 parent 8244400 commit 3739b50
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 8 deletions.
33 changes: 25 additions & 8 deletions do_with_in_base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1735,6 +1735,7 @@ use std::path::{Path, PathBuf};
use std::ffi::OsStr;
use std::fs::File;
pub fn importHandler<T: StartMarker + Clone>(c: Configuration<T>, v: Variables<T>, data: Option<TokenStream2>, t: TokenStream2) -> (Variables<T>, TokenStream2) {
let mut reset = false;
let base = match c.clone().file {
Some(x) => x,
None => file!().to_string(),
Expand All @@ -1751,15 +1752,31 @@ pub fn importHandler<T: StartMarker + Clone>(c: Configuration<T>, v: Variables<T
stream.next(); // Skip 'include'
let mut cont = true;
while cont {
if let Some(TokenTree2::Literal(segment)) = stream.next() {
match syn::parse_str::<syn::LitStr>(&segment.clone().to_string()) {
Ok(x) => path.push(x.value()),
_ => panic!("oqwiefhawlkfhLQIPLE"),
};
} else {
cont = false;
}
match stream.next() {
Some(TokenTree2::Literal(segment)) => {
match syn::parse_str::<syn::LitStr>(&segment.clone().to_string()) {
Ok(x) => {
if reset {
path = Path::new(&x.value()).to_path_buf();
reset = false;
} else {
path.push(x.value());
}
},
_ => panic!("oqwiefhawlkfhLQIPLE"),
};
},
Some(TokenTree2::Ident(id)) if id.to_string() == "Base" => {
reset = true;
},
_ => {
cont = false;
},
};
}
if reset {
return (v, quote!{compile_error!{ "Path finished with a 'Base'; we need an actual file reference." }});
};
let mut f = match File::open(path.clone()) {
Ok(s) => s,
Err(e) => {
Expand Down
35 changes: 35 additions & 0 deletions do_with_in_internal_macros/tests/do_with_in_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,41 @@ fn import_test2() {
assert_eq!(a, 1);
}

#[test]
fn import_test3() {
do_with_in!{
sigil: $
do
$(import Base "do_with_in_internal_macros" "tests" "import.$")

let $b = $a;
};
assert_eq!(c, 1);
}

#[test]
fn import_test4() {
do_with_in!{
sigil: $
do
$(import Base "import_base_test.$")

let $b = $a;
};
assert_eq!(c, 1);
}

#[test]
fn import_test5() {
do_with_in!{
sigil: $
do
$(import Base "import_base_test2.$")
let $z = $c;
};
assert_eq!(twelve, 12);
}

/*#[test]
fn for_test1() {
do_with_in!{
Expand Down
5 changes: 5 additions & 0 deletions do_with_in_internal_macros/tests/import_2.$
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$(let
a = {3}
b = {4}
z = {twelve}
)
4 changes: 4 additions & 0 deletions import_base_test.$
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$(let
a = {1}
b = {c}
)
4 changes: 4 additions & 0 deletions import_base_test2.$
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$(import "do_with_in_internal_macros" "tests" "import_2.$")

$(var c = {$(arithmetic u8 $a * $b)})

0 comments on commit 3739b50

Please sign in to comment.