Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
indent_style = tab
indent_size = 5
indent_size = 4
max_line_length = 80
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "include_tt"
version = "1.0.5"
version = "1.0.6"
edition = "2024"
authors = ["Denis Kotlyarov (Денис Котляров) <denis2005991@gmail.com>"]
repository = "https://github.com/clucompany/include_tt.git"
Expand All @@ -16,9 +16,9 @@ proc-macro = true

[dependencies]
quote = "1.0.40"
proc-macro2 = "1.0.94"
proc-macro2 = "1.0.95"

[dependencies.syn]
version = "2.0.100"
version = "2.0.104"
default-features = false
features = ["parsing"]
2 changes: 1 addition & 1 deletion examples/mrules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ fn main() {
}
}
assert_eq!(n, a + b);
println!("n: {:?}", n); // 30
println!("n: {n:?}"); // 30
}
22 changes: 11 additions & 11 deletions src/exprs/literal.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::sg_err;
use crate::throw_sg_err;
use alloc::{borrow::ToOwned, string::String};
use core::{
borrow::Borrow,
Expand All @@ -14,14 +14,14 @@ pub struct ExprLit {
}

impl PartialEq<ExprLit> for ExprLit {
#[inline(always)]
#[inline]
fn eq(&self, other: &ExprLit) -> bool {
PartialEq::eq(self.as_str(), other.as_str())
}
}

impl PartialEq<str> for ExprLit {
#[inline(always)]
#[inline]
fn eq(&self, other: &str) -> bool {
PartialEq::eq(self.as_str(), other)
}
Expand All @@ -43,10 +43,10 @@ impl ExprLitTryNewErr {
#[inline]
pub fn into_tt_err(self, span: Span) -> TokenStream2 {
match self {
Self::ExpLen { current, exp } => sg_err! {
Self::ExpLen { current, exp } => throw_sg_err! {
[span]: "More char expected, current: ", #current, "exp: {}", #exp, "."
},
Self::ExpQuotes => sg_err! {
Self::ExpQuotes => throw_sg_err! {
[span]: "Double quotes were expected."
},
}
Expand All @@ -56,21 +56,21 @@ impl ExprLitTryNewErr {
impl Deref for ExprLit {
type Target = str;

#[inline(always)]
#[inline]
fn deref(&self) -> &Self::Target {
self.as_str()
}
}

impl Debug for ExprLit {
#[inline(always)]
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Debug::fmt(self as &str, f)
}
}

impl Display for ExprLit {
#[inline(always)]
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Display::fmt(self as &str, f)
}
Expand Down Expand Up @@ -102,7 +102,7 @@ impl ExprLit {
}

/// Creating `ExprLit` without clipping.
#[inline(always)]
#[inline]
pub const unsafe fn unchecked(a: &str) -> &Self {
Self::__new(a)
}
Expand Down Expand Up @@ -172,14 +172,14 @@ impl ExprLit {
})
}

#[inline(always)]
#[inline]
/// Returns `true` if self has a length of zero bytes.
pub const fn is_empty(&self) -> bool {
self.data.is_empty()
}

/// Getting a string of actual data.
#[inline(always)]
#[inline]
pub const fn as_str(&self) -> &str {
&self.data
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ use alloc::string::ToString;
use core::slice::IterMut;
use proc_macro::TokenStream;
use proc_macro2::{Group, TokenStream as TokenStream2, TokenTree as TokenTree2};
use trees::sg_err;
use crate::trees::throw_sg_err;

/// Components, templates, code for the search
/// and final construction of trees.
Expand All @@ -128,7 +128,7 @@ pub(crate) mod trees {
#[macro_use]
pub mod sq_err;
#[allow(clippy::single_component_path_imports)]
pub(crate) use sg_err;
pub(crate) use throw_sg_err;
pub mod loader;
}

Expand Down Expand Up @@ -203,12 +203,12 @@ fn search_include_and_replacegroup(iter: &mut IterMut<'_, TokenTree2>) -> Search
}
}

sg_err! {
throw_sg_err! {
return [ident.span()]: "`;` was expected."
}
}

_ => sg_err! {
_ => throw_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`, `include_and_break`, `include_tt_and_break`, `include_and_fix_unknown_start_token_and_break`, `include_tt_and_fix_unknown_start_token_and_break`, `include_str_and_break`, `include_arr_and_break`."
},
}
Expand Down
38 changes: 28 additions & 10 deletions src/macros/include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ use crate::{
group::g_stringify,
loader::{LoadFileAndAutoMakeTreeErr, load_file_and_automake_tree_with_fns},
result::TreeResult,
sg_err, ttry,
throw_sg_err, ttry,
},
};
use alloc::string::String;
use proc_macro2::{
Delimiter, Group, Literal, Span, TokenStream as TokenStream2, TokenTree as TokenTree2,
};
use std::{fs::File, io::Read};
use std::{
borrow::Cow,
fs::File,
io::Read,
path::Path,
};

/// A trait that specifies the final behavior for the `include` macro.
pub trait BehMacroInclude {
Expand Down Expand Up @@ -92,8 +97,9 @@ impl BehMacroInclude for IncludeTT {
) -> TreeResult<Self::Result> {
arg0.get_str_with_fns(
|sspath| {
let path = Path::new(sspath);
load_file_and_automake_tree_with_fns(
sspath,
path,
|_| {}, /* skip_prepare */
|fs_tt| {
let ett = match fs_tt {
Expand Down Expand Up @@ -138,6 +144,7 @@ impl BehMacroInclude for IncludeTTAndFixUnkStartToken {
) -> TreeResult<Self::Result> {
arg0.get_str_with_fns(
|sspath| {
let sspath = Path::new(sspath);
load_file_and_automake_tree_with_fns(
sspath,
|p_string| {
Expand Down Expand Up @@ -238,10 +245,14 @@ impl BehMacroInclude for IncludeStr {
) -> TreeResult<Self::Result> {
arg0.get_str_with_fns(
|sspath| {
let data = match std::fs::read_to_string(sspath) {
let path = Path::new(sspath);
let data = match std::fs::read_to_string(path) {
Ok(a) => a,
Err(e) => {
return LoadFileAndAutoMakeTreeErr::read_to_string(e, sspath)
let path = path
.canonicalize()
.map_or_else(|_| Cow::Borrowed(path), Cow::Owned);
return LoadFileAndAutoMakeTreeErr::read_to_string(e, path)
.into_tt_err(literal_span)
.into();
}
Expand Down Expand Up @@ -280,18 +291,25 @@ impl BehMacroInclude for IncludeArr {
arg0.get_str_with_fns(
|sspath| {
let vec = {
let mut file = match File::open(sspath) {
let path = Path::new(sspath);
let mut file = match File::open(path) {
Ok(a) => a,
Err(e) => {
return LoadFileAndAutoMakeTreeErr::read_to_string(e, sspath)
let path = path
.canonicalize()
.map_or_else(|_| Cow::Borrowed(path), Cow::Owned);
return LoadFileAndAutoMakeTreeErr::read_to_string(e, path)
.into_tt_err(literal_span)
.into();
}
};

let mut vec = Vec::new(); // capacity is not required.
if let Err(e) = file.read_to_end(&mut vec) {
return LoadFileAndAutoMakeTreeErr::read_to_string(e, sspath)
let path = path
.canonicalize()
.map_or_else(|_| Cow::Borrowed(path), Cow::Owned);
return LoadFileAndAutoMakeTreeErr::read_to_string(e, path)
.into_tt_err(literal_span)
.into();
};
Expand Down Expand Up @@ -320,7 +338,7 @@ where

let stream0 = iter.next();
if let Some(unk) = iter.next() {
sg_err! {
throw_sg_err! {
return [unk.span()]: "Specify a valid path to the file written with `\"/Test.tt\"`, or `'T'`, or use a group of different trees `[/, \"Test\", '/']`."
}
}
Expand Down Expand Up @@ -355,7 +373,7 @@ where
literal.span(),
)
}
Some(g_stream) => sg_err! {
Some(g_stream) => throw_sg_err! {
return [g_stream.span()]: "The path was expected as a single string (example: \"../test.tt\") or a path formatted as separate TokenTrees (example: ['.' '.' test \".tt\"])."
},
}
Expand Down
28 changes: 14 additions & 14 deletions src/trees/group.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{TreeResult, exprs::literal::ExprLit, trees::sg_err, trees::ttry};
use crate::{exprs::literal::ExprLit, throw_sg_err, trees::ttry, TreeResult};
use alloc::{
fmt::Write,
format,
Expand All @@ -24,11 +24,11 @@ pub fn check_correct_endgroup<'i>(

let mut iter = endgroup.iter();
if let Some(a) = iter.next() {
if let Err(..) = write!(str, "`{}`", a) {
if let Err(..) = write!(str, "`{a}`") {
return str;
}
for a in iter {
if let Err(..) = write!(str, ", `{}`", a) {
if let Err(..) = write!(str, ", `{a}`") {
return str;
}
}
Expand Down Expand Up @@ -57,7 +57,7 @@ pub fn check_correct_endgroup<'i>(
};
if !is_valid {
let e_group_str = make_endroup_str(endgroup);
sg_err! {
throw_sg_err! {
return [punct.span()]: "", #e_group_str, " was expected."
}
}
Expand All @@ -67,21 +67,21 @@ pub fn check_correct_endgroup<'i>(

_ => {
let e_group_str = make_endroup_str(endgroup);
sg_err! {
throw_sg_err! {
return [m_punct.span()]: "", #e_group_str, " was expected."
}
}
}
} else {
let e_group_str = make_endroup_str(endgroup);
sg_err! {
throw_sg_err! {
return [group.span()]: "", #e_group_str, " was expected."
}
}
}
Delimiter::Brace => return TreeResult::Ok(None), // `{ ... }`, ok
Delimiter::Bracket | Delimiter::None => {
sg_err! {
throw_sg_err! {
return [group.span()]: "Unsupported group type."
}
}
Expand Down Expand Up @@ -117,16 +117,16 @@ fn __g_stringify(tt: TokenTree2, w: &mut impl Write) -> TreeResult<()> {
}
TokenTree2::Ident(i) => {
if let Err(e) = w.write_str(&i.to_string()) {
let debug = format!("{:?}", e);
sg_err! {
let debug = format!("{e:?}");
throw_sg_err! {
return [i.span()]: "Ident, ", #debug
}
}
}
TokenTree2::Punct(p) => {
if let Err(e) = w.write_char(p.as_char()) {
let debug = format!("{:?}", e);
sg_err! {
let debug = format!("{e:?}");
throw_sg_err! {
return [p.span()]: "Punct, ", #debug
}
}
Expand All @@ -137,8 +137,8 @@ fn __g_stringify(tt: TokenTree2, w: &mut impl Write) -> TreeResult<()> {
|sspath| match w.write_str(sspath) {
Ok(..) => TreeResult::Ok(()),
Err(e) => {
let debug = format!("{:?}", e);
sg_err! {
let debug = format!("{e:?}");
throw_sg_err! {
return [l.span()]: "Literal, ", #debug
}
}
Expand All @@ -147,7 +147,7 @@ fn __g_stringify(tt: TokenTree2, w: &mut impl Write) -> TreeResult<()> {
let span = l.span();
let debug = e.into_tt_err(span);

sg_err! {
throw_sg_err! {
return [span]: "Literal, ", #debug
}
},
Expand Down
Loading