• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Alorel / macroific-rs / 11195403850

05 Oct 2024 06:27PM UTC coverage: 75.693%. First build
11195403850

Pull #14

github

web-flow
Merge 622ba6823 into de4fadcc0
Pull Request #14: v2

299 of 358 new or added lines in 13 files covered. (83.52%)

928 of 1226 relevant lines covered (75.69%)

50.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.57
/modules/macroific-core/src/elements/module_prefix.rs
1
//! A `const`-table module prefix, e.g. `::your_crate::__private`.
2

3
use std::ops::{Deref, Index};
4
use std::{array, fmt};
5

6
use crate::core_ext::*;
7
use proc_macro2::{Ident, TokenStream};
8
use quote::{ToTokens, TokenStreamExt};
9
use syn::Token;
10

11
/// Prefix for [`::core::option::Option`].
12
pub const OPTION: ModulePrefix<'static, 3> = ModulePrefix::new(["core", "option", "Option"]);
13

14
/// Prefix for [`::core::result::Result`].
15
pub const RESULT: ModulePrefix<'static, 3> = ModulePrefix::new(["core", "result", "Result"]);
16

17
/// A `const`-table module prefix, e.g. `::your_crate::__private`.
18
///
19
/// ```
20
/// # use macroific_core::elements::*;
1✔
21
/// # use syn::parse_quote;
22
/// # use quote::{quote, ToTokens};
23
/// #
24
/// const PREFIXED: ModulePrefix<'static, 2> = ModulePrefix::new(["foo", "bar"]);
25
/// const UNPREFIXED: ModulePrefix<'static, 2> = ModulePrefix::new(["foo", "bar"])
26
///   .with_leading_sep(false);
27
///
28
/// let tokens_prefixed = PREFIXED.to_token_stream().to_string();
29
/// let tokens_unprefixed = UNPREFIXED.to_token_stream().to_string();
1✔
30
///
1✔
31
/// assert_eq!(tokens_prefixed, ":: foo :: bar");
1✔
32
/// assert_eq!(tokens_unprefixed, "foo :: bar");
1✔
33
///
1✔
34
/// // Display is also implemented
35
/// assert_eq!(PREFIXED.to_string(), tokens_prefixed);
36
/// assert_eq!(UNPREFIXED.to_string(), tokens_unprefixed);
1✔
37
/// ```
1✔
38
#[derive(Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Debug)]
1✔
39
pub struct ModulePrefix<'a, const LEN: usize> {
40
    path: [&'a str; LEN],
41
    leading_sep: bool,
42
}
43

44
impl<'a, const LEN: usize> ModulePrefix<'a, LEN> {
45
    /// Create a new `ModulePrefix` from a slice of segments.
46
    #[inline]
47
    #[must_use]
48
    pub const fn new(segments: [&'a str; LEN]) -> Self {
3✔
49
        Self {
3✔
50
            path: segments,
3✔
51
            leading_sep: true,
3✔
52
        }
3✔
53
    }
3✔
54

55
    /// `true` (default) will include the leading `::`, `false` will omit it.
56
    #[inline]
57
    #[must_use]
NEW
58
    pub const fn with_leading_sep(mut self, leading_sep: bool) -> Self {
×
NEW
59
        self.leading_sep = leading_sep;
×
NEW
60
        self
×
61
    }
×
62
}
63

64
impl<'a, const LEN: usize> IntoIterator for ModulePrefix<'a, LEN> {
65
    type Item = &'a str;
66
    type IntoIter = array::IntoIter<&'a str, LEN>;
67

68
    #[inline]
69
    fn into_iter(self) -> Self::IntoIter {
874✔
70
        self.path.into_iter()
874✔
71
    }
874✔
72
}
73

74
impl<'a, const LEN: usize> ToTokens for ModulePrefix<'a, LEN> {
75
    fn to_tokens(&self, tokens: &mut TokenStream) {
872✔
76
        let mut iter = self.into_iter();
872✔
77

872✔
78
        if !self.leading_sep {
872✔
79
            if let Some(first) = iter.next() {
1✔
80
                tokens.append(Ident::create(first));
1✔
81
            } else {
1✔
NEW
82
                return;
×
83
            }
84
        }
871✔
85

86
        let sep = <Token![::]>::default();
872✔
87

88
        for segment in iter {
3,468✔
89
            sep.to_tokens(tokens);
2,596✔
90
            tokens.append(Ident::create(segment));
2,596✔
91
        }
2,596✔
92
    }
872✔
93
}
94

95
impl<'a, const LEN: usize> fmt::Display for ModulePrefix<'a, LEN> {
96
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2✔
97
        let mut iter = self.into_iter();
2✔
98
        let Some(first) = iter.next() else {
2✔
NEW
99
            return Ok(());
×
100
        };
101

102
        if self.leading_sep {
2✔
103
            f.write_str(":: ")?;
1✔
104
            f.write_str(first)?;
1✔
105
        } else {
106
            f.write_str(first)?;
1✔
107
        }
108

109
        for item in iter {
4✔
110
            f.write_str(" :: ")?;
2✔
111
            f.write_str(item)?;
2✔
112
        }
113

114
        Ok(())
2✔
115
    }
2✔
116
}
117

118
impl<'a, const LEN: usize> Deref for ModulePrefix<'a, LEN> {
119
    type Target = [&'a str];
120

121
    #[inline]
NEW
122
    fn deref(&self) -> &Self::Target {
×
NEW
123
        &self.path
×
124
    }
×
125
}
126

127
impl<'a, const LEN: usize> Index<usize> for ModulePrefix<'a, LEN> {
128
    type Output = str;
129

130
    #[inline]
NEW
131
    fn index(&self, index: usize) -> &Self::Output {
×
NEW
132
        self.path[index]
×
133
    }
×
134
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc