Repository: typedb/typeql (language: python)

Issue:
Sanitize rust macros to avoid forcing user's environment
## Problem to Solve

Currently the builder macros rely on a particular lexical environment.
For example 
```rust
typeql_lang::typeql_define()
// will expand to
TypeQLDefine::new(vec![])
```
requiring the user to have `TypeQLDefine` in the lexical environment.

This is a problem because it lets users do this sort of thing:

```rust
struct TypeQLDefine;
impl TypeQLDefine {
  fn new(Vec<typeql_lang::pattern::Definable>) -> Self { Self }
}

// somewhere where that TypeQLDefine is in scope...
typeql_define!(my_type, my_rule) // <-- this line doesn't error
```

This is a minimal example, but you can see where major problems can arise.

## Proposed Solution

Using proper rust macro hygiene fixes this issue, by expanding to fully qualified identifiers.

To fix the define query, for example, is as simple as replacing it with
```rust
#[macro_export]
macro_rules! typeql_define {
    ($($pattern:expr),* $(,)?) => {{
        $crate::query::TypeQLDefine::new(vec![$($pattern.into()),*])
    }}
}
```
With this we have
```rust
typeql_define!()
// will expand to
typeql_lang::query::TypeQLDefine::new(vec![])
```

Read more on macro hygiene at: https://veykril.github.io/tlborm/decl-macros/minutiae/hygiene.html
Sanitize rust macros to avoid forcing user's environment
## Problem to Solve

Currently the builder macros rely on a particular lexical environment.
For example 
```rust
typeql_lang::typeql_define()
// will expand to
TypeQLDefine::new(vec![])
```
requiring the user to have `TypeQLDefine` in the lexical environment.

This is a problem because it lets users do this sort of thing:

```rust
struct TypeQLDefine;
impl TypeQLDefine {
  fn new(Vec<typeql_lang::pattern::Definable>) -> Self { Self }
}

// somewhere where that TypeQLDefine is in scope...
typeql_define!(my_type, my_rule) // <-- this line doesn't error
```

This is a minimal example, but you can see where major problems can arise.

## Proposed Solution

Using proper rust macro hygiene fixes this issue, by expanding to fully qualified identifiers.

To fix the define query, for example, is as simple as replacing it with
```rust
#[macro_export]
macro_rules! typeql_define {
    ($($pattern:expr),* $(,)?) => {{
        $crate::query::TypeQLDefine::new(vec![$($pattern.into()),*])
    }}
}
```
With this we have
```rust
typeql_define!()
// will expand to
typeql_lang::query::TypeQLDefine::new(vec![])
```

Read more on macro hygiene at: https://veykril.github.io/tlborm/decl-macros/minutiae/hygiene.html


Discussion / hints:

Fix patch:
diff --git a/rust/builder/mod.rs b/rust/builder/mod.rs
index 38956ff24..341148ccb 100644
--- a/rust/builder/mod.rs
+++ b/rust/builder/mod.rs
@@ -32,35 +32,35 @@ use crate::{
 #[macro_export]
 macro_rules! typeql_match {
     ($($pattern:expr),* $(,)?) => {{
-        TypeQLMatch::from_patterns(vec![$($pattern.into()),*])
+        $crate::query::TypeQLMatch::from_patterns(vec![$($pattern.into()),*])
     }}
 }
 
 #[macro_export]
 macro_rules! typeql_insert {
     ($($thing_variable:expr),* $(,)?) => {{
-        TypeQLInsert::new(vec![$($thing_variable),*])
+        $crate::query::TypeQLInsert::new(vec![$($thing_variable),*])
     }}
 }
 
 #[macro_export]
 macro_rules! typeql_define {
     ($($pattern:expr),* $(,)?) => {{
-        TypeQLDefine::new(vec![$($pattern.into()),*])
+        $crate::query::TypeQLDefine::new(vec![$($pattern.into()),*])
     }}
 }
 
 #[macro_export]
 macro_rules! typeql_undefine {
     ($($pattern:expr),* $(,)?) => {{
-        TypeQLUndefine::new(vec![$($pattern.into()),*])
+        $crate::query::TypeQLUndefine::new(vec![$($pattern.into()),*])
     }}
 }
 
 #[macro_export]
 macro_rules! and {
     ($($pattern:expr),* $(,)?) => {{
-        Conjunction::new(vec![$($pattern.into()),*])
+        $crate::pattern::Conjunction::new(vec![$($pattern.into()),*])
     }}
 }
 
@@ -70,7 +70,7 @@ macro_rules! or {
         let mut patterns = vec![$($pattern.into()),*];
         match patterns.len() {
             1 => patterns.pop().unwrap(),
-            _ => Disjunction::new(patterns).into(),
+            _ => $crate::pattern::Disjunction::new(patterns).into(),
         }
     }}
 }
diff --git a/rust/common/error/macros.rs b/rust/common/error/macros.rs
index 3da5ecb56..11c37f4d4 100644
--- a/rust/common/error/macros.rs
+++ b/rust/common/error/macros.rs
@@ -166,9 +166,9 @@ macro_rules! error_messages {
     };
 }
 
+#[allow(dead_code)]
 #[cfg(test)]
 mod tests {
-    use std::fmt::Debug;
     error_messages! { TestError
         code: "TST", type: "Test Error",
         BasicError() =
diff --git a/rust/pattern/mod.rs b/rust/pattern/mod.rs
index 718176ebe..4a6befe85 100644
--- a/rust/pattern/mod.rs
+++ b/rust/pattern/mod.rs
@@ -51,7 +51,7 @@ pub use variable::{
 };
 
 use crate::{
-    common::{error::TypeQLError, validatable::Validatable, Result},
+    common::{validatable::Validatable, Result},
     enum_getter, enum_wrapper,
 };
 
diff --git a/rust/query/mod.rs b/rust/query/mod.rs
index d61cdb8b1..fff9eb95b 100644
--- a/rust/query/mod.rs
+++ b/rust/query/mod.rs
@@ -43,7 +43,7 @@ pub use typeql_update::TypeQLUpdate;
 pub use writable::Writable;
 
 use crate::{
-    common::{error::TypeQLError, validatable::Validatable, Result},
+    common::{validatable::Validatable, Result},
     enum_getter, enum_wrapper,
 };
 
diff --git a/rust/util/mod.rs b/rust/util/mod.rs
index 8894e82dc..2ccd2a218 100644
--- a/rust/util/mod.rs
+++ b/rust/util/mod.rs
@@ -36,7 +36,7 @@ macro_rules! enum_getter {
             pub fn $fn_name(self) -> $classname {
                 match self {
                     Self::$enum_variant(x) => x,
-                    _ => panic!("{}", TypeQLError::InvalidCasting(
+                    _ => panic!("{}", $crate::common::error::TypeQLError::InvalidCasting(
                         stringify!($enum_name),
                         self.enum_getter_get_name(),
                         stringify!($enum_variant),


Test patch:
diff --git a/rust/parser/test/mod.rs b/rust/parser/test/mod.rs
index 739454762..d9963b3f3 100644
--- a/rust/parser/test/mod.rs
+++ b/rust/parser/test/mod.rs
@@ -34,10 +34,10 @@ use crate::{
     gte, lt, lte, not, or, parse_definables, parse_label, parse_pattern, parse_patterns, parse_queries, parse_query,
     parse_variable,
     pattern::{
-        Annotation::Key, ConceptVariableBuilder, Conjunction, Disjunction, Label, RelationVariableBuilder,
-        ThingVariableBuilder, TypeVariableBuilder, Variable,
+        Annotation::Key, ConceptVariableBuilder, Label, RelationVariableBuilder, ThingVariableBuilder,
+        TypeVariableBuilder, Variable,
     },
-    query::{AggregateQueryBuilder, TypeQLDefine, TypeQLInsert, TypeQLMatch, TypeQLUndefine},
+    query::AggregateQueryBuilder,
     rel, rule, type_, typeql_insert, typeql_match, var, Query,
 };
 
diff --git a/rust/pattern/test/mod.rs b/rust/pattern/test/mod.rs
index fddddfa13..854aa26bb 100644
--- a/rust/pattern/test/mod.rs
+++ b/rust/pattern/test/mod.rs
@@ -22,7 +22,7 @@
 
 use crate::{
     and, not, or, parse_query,
-    pattern::{Conjunction, Disjunction, Normalisable, ThingVariableBuilder},
+    pattern::{Disjunction, Normalisable, ThingVariableBuilder},
     var,
 };