Skip to content

Commit

Permalink
1. Update rust example
Browse files Browse the repository at this point in the history
  • Loading branch information
denisandroid committed Apr 3, 2024
1 parent 6ebf17f commit 34575fa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@ use include_tt::include_tt;
use include_tt::include_tt;
use std::fmt::Write;

// Embedding compiler trees from a file in an arbitrary place of other macros.
{
// Example demonstrating the usage of include_tt! macro for embedding content from files.

{
// Embedding trees from a file in an arbitrary place of other macros.
let a = 10;
let b = 20;
let mut end_str = String::new();

// Using include_tt! to embed content into a macro.
include_tt! {
let _e = write!(
&mut end_str,
Expand All @@ -66,22 +70,28 @@ use std::fmt::Write;
#include!("./for_examples/full.tt")
);
}

// Asserting the result matches the expected output.
assert_eq!(end_str, "arg1: 10, arg2: 20");
}

// Loading a string from a file.
{
{
// Loading a string from "full.tt" using include_tt! macro.
let str = include_tt!(
#include_str!("./for_examples/full.tt")
);

// Asserting the result matches the expected output.
assert_eq!(str, "a, b");
}

// Loading an array from a file.
{
// Loading a array from "full.tt" using include_tt! macro.
let array: &'static [u8; 4] = include_tt!(
#include_arr!("./for_examples/full.tt")
);

// Asserting the result matches the expected output.
assert_eq!(array, b"a, b");
}
```
Expand Down
28 changes: 20 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,20 @@
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.

/*! Macro for including trees, strings, arrays from files.
/*! Macro for embedding (trees, strings, arrays) into macro trees directly from files.
```rust
use include_tt::include_tt;
use std::fmt::Write;
{ // Embedding compiler trees from a file in an arbitrary place of other macros.
// Example demonstrating the usage of include_tt! macro for embedding content from files.
{
// Embedding trees from a file in an arbitrary place of other macros.
let a = 10;
let b = 20;
let mut end_str = String::new();
// Using include_tt! to embed content into a macro.
include_tt! {
let _e = write!(
&mut end_str,
Expand All @@ -54,29 +59,36 @@ use std::fmt::Write;
#include!("./for_examples/full.tt")
);
}
// Asserting the result matches the expected output.
assert_eq!(end_str, "arg1: 10, arg2: 20");
}
{ // Loading a string from a file.
{
// Loading a string from "full.tt" using include_tt! macro.
let str = include_tt!(
#include_str!("./for_examples/full.tt")
);
// Asserting the result matches the expected output.
assert_eq!(str, "a, b");
}
{ // Loading an array from a file.
{
// Loading a array from "full.tt" using include_tt! macro.
let array: &'static [u8; 4] = include_tt!(
#include_arr!("./for_examples/full.tt")
);
// Asserting the result matches the expected output.
assert_eq!(array, b"a, b");
}
```
*/

use std::slice::IterMut;
use proc_macro2::{TokenTree as TokenTree2, Group};
use proc_macro2::{TokenTree as TokenTree2, TokenStream as TokenStream2, Group};
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use trees::sg_err;
use crate::{trees::{null::make_null_ttree, replace::{support_replace_tree_in_group, support_replace_tree_in_stream}, result::TreeResult, ttry, search::SearchGroup}, macros::include::{macro_rule_include, IncludeTt, IncludeStr, IncludeArr, IncludeTtAndFixUnkStartToken}};

Expand Down Expand Up @@ -167,7 +179,7 @@ fn search_include_and_replacegroup(
let macro_fn = match macro_fn {
Some(a) => a,
None => sg_err! {
return [ident.span()]: "Unknown macro, expected `include`, `include_tt`, `include_and_fix_unknown_start_token`, `include_tt_and_fix_unknown_start_token`, `include_str`, `include_arr`"
return [ident.span()]: "Unknown macro, expected `include`, `include_tt`, `include_and_fix_unknown_start_token`, `include_tt_and_fix_unknown_start_token`, `include_str`, `include_arr`."
}
};

Expand All @@ -190,7 +202,7 @@ fn search_include_and_replacegroup(

if macro_fn.is_some() { // The required macro was defined earlier, which means an error.
sg_err! {
return [ident.span()]: "Unknown macro, expected `include(...)`, `include_tt(...)`, `include_and_fix_unknown_start_token(...)`, `include_tt_and_fix_unknown_start_token(...)`, `include_str(...)`, `include_arr(...)`"
return [ident.span()]: "Unknown macro, expected `include(...)`, `include_tt(...)`, `include_and_fix_unknown_start_token(...)`, `include_tt_and_fix_unknown_start_token(...)`, `include_str(...)`, `include_arr(...)`."
}
}
}
Expand Down

0 comments on commit 34575fa

Please sign in to comment.