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

Alorel / macroific-rs / 11197184821

05 Oct 2024 11:43PM UTC coverage: 81.468%. First build
11197184821

Pull #14

github

web-flow
Merge c8342d59b into 697e66c3a
Pull Request #14: v2

430 of 476 new or added lines in 16 files covered. (90.34%)

1099 of 1349 relevant lines covered (81.47%)

46.82 hits per line

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

90.91
/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
#[cfg(feature = "module-prefix")]
40
pub struct ModulePrefix<'a, const LEN: usize> {
41
    path: [&'a str; LEN],
42
    leading_sep: bool,
43
}
44

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

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

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

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

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

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

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

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

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

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

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

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

119
/// # Example
120
///
121
/// ```
122
/// # use macroific_core::elements::ModulePrefix;
1✔
123
/// #
124
/// fn accept_str_slice(_: &[&str]) {}
125
///
1✔
126
/// let prefix = ModulePrefix::new(["foo", "bar"]);
127
/// accept_str_slice(&prefix); // derefs fine
1✔
128
/// ```
1✔
129
impl<'a, const LEN: usize> Deref for ModulePrefix<'a, LEN> {
1✔
130
    type Target = [&'a str];
131

132
    #[inline]
133
    fn deref(&self) -> &Self::Target {
1✔
134
        &self.path
1✔
135
    }
1✔
136
}
137

138
/// # Example
139
///
140
/// ```
141
/// # use macroific_core::elements::ModulePrefix;
1✔
142
/// #
143
/// let prefix = ModulePrefix::new(["foo", "bar"]);
144
/// assert_eq!(&prefix[0], "foo");
1✔
145
/// assert_eq!(&prefix[1], "bar");
1✔
146
/// ```
1✔
147
impl<'a, const LEN: usize> Index<usize> for ModulePrefix<'a, LEN> {
1✔
148
    type Output = str;
149

150
    #[inline]
151
    fn index(&self, index: usize) -> &Self::Output {
2✔
152
        self.path[index]
2✔
153
    }
2✔
154
}
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

© 2025 Coveralls, Inc