binread_derive/codegen/
sanitization.rs

1///! Utilities for helping sanitize macro
2use proc_macro2::{Ident, Span, TokenStream};
3use quote::{format_ident, quote, ToTokens, TokenStreamExt};
4
5macro_rules! ident_str {
6    () => {};
7
8    ($vis:vis $ident:ident = $path:expr; $($tail:tt)*) => {
9        ident_str!($vis $ident = $path);
10        ident_str!($($tail)*);
11    };
12
13    ($vis:vis $ident:ident = $path:expr) => {
14        $vis const $ident: $crate::codegen::sanitization::IdentStr =
15            $crate::codegen::sanitization::IdentStr::new($path);
16    };
17}
18
19macro_rules! from_crate {
20    ($path:path) => {
21        concat!("binread::", stringify!($path))
22    };
23}
24
25macro_rules! from_trait {
26    () => {
27        from_crate!(BinRead)
28    };
29    ($path:path) => {
30        concat!("binread::BinRead::", stringify!($path))
31    };
32}
33
34ident_str! {
35    pub(super) TRAIT_NAME = from_trait!();
36    pub(super) BIN_ERROR = from_crate!(Error);
37    pub(super) OPTIONS = from_crate!(ReadOptions);
38    pub(super) READ_TRAIT = from_crate!(io::Read);
39    pub(super) SEEK_TRAIT = from_crate!(io::Seek);
40    pub(super) POS_TRAIT = from_crate!(io::StreamPosition);
41    pub(super) SEEK_FROM = from_crate!(io::SeekFrom);
42    pub(super) BIN_RESULT = from_crate!(BinResult);
43    pub(super) ENDIAN_ENUM = from_crate!(Endian);
44    pub(super) READ_METHOD = from_trait!(read_options);
45    pub(super) AFTER_PARSE = from_trait!(after_parse);
46    pub(super) READER = "__binread_generated_var_reader";
47    pub(super) OPT = "__binread_generated_var_options";
48    pub(super) ARGS = "__binread_generated_var_arguments";
49    pub(super) SAVED_POSITION = "__binread_generated_saved_position";
50    pub(super) ASSERT_MAGIC = from_crate!(error::magic);
51    pub(super) ASSERT = from_crate!(__private::assert);
52    pub(super) ASSERT_ERROR_FN = from_crate!(__private::AssertErrorFn);
53    pub(super) COERCE_FN = from_crate!(__private::coerce_fn);
54    pub(super) TRY_AFTER_PARSE = from_crate!(__private::try_after_parse);
55    pub(super) TEMP = "__binread_temp";
56    pub(super) POS = "__binread_generated_position_temp";
57    pub(super) ERROR_BASKET = "__binread_generated_error_basket";
58}
59
60pub(crate) fn make_ident(ident: &Ident, kind: &str) -> Ident {
61    format_ident!("__binread_generated_{}_{}", kind, ident.to_string())
62}
63
64/// A string wrapper that converts the str to a $path `TokenStream`, allowing
65/// for constant-time idents that can be shared across threads
66#[derive(Debug, Clone, Copy, Eq, PartialEq)]
67pub struct IdentStr(&'static str);
68
69impl IdentStr {
70    pub(crate) const fn new(str: &'static str) -> Self {
71        IdentStr(str)
72    }
73}
74
75impl ToTokens for IdentStr {
76    fn to_tokens(&self, tokens: &mut TokenStream) {
77        let idents = self
78            .0
79            .split("::")
80            .map(|ident| Ident::new(ident, Span::call_site()));
81        tokens.append_separated(idents, quote!(::));
82    }
83}