fallow_core/plugins/
used_export_rule.rs1use super::{AggregatedPluginResult, PathRule, PathRuleIndex, PluginResult};
5
6impl PluginResult {
7 pub(super) fn push_used_export_rule(
8 &mut self,
9 pattern: impl Into<String>,
10 exports: impl IntoIterator<Item = impl Into<String>>,
11 ) {
12 self.used_exports
13 .push(UsedExportRule::new(pattern, exports));
14 }
15
16 pub(super) fn push_entry_export_credit(
19 &mut self,
20 pattern: impl Into<String>,
21 exports: impl IntoIterator<Item = impl Into<String>>,
22 ) {
23 self.entry_export_credits
24 .push(UsedExportRule::new(pattern, exports));
25 }
26}
27
28#[derive(Debug, Clone, Default, PartialEq, Eq)]
30pub struct UsedExportRule {
31 pub(crate) path: PathRule,
32 pub(crate) exports: Vec<String>,
33}
34
35impl UsedExportRule {
36 #[must_use]
37 pub(crate) fn new(
38 pattern: impl Into<String>,
39 exports: impl IntoIterator<Item = impl Into<String>>,
40 ) -> Self {
41 Self {
42 path: PathRule::new(pattern),
43 exports: exports.into_iter().map(Into::into).collect(),
44 }
45 }
46
47 #[must_use]
48 pub(super) fn from_static(pattern: &'static str, exports: &'static [&'static str]) -> Self {
49 Self::new(pattern, exports.iter().copied())
50 }
51
52 #[must_use]
53 pub(super) fn with_excluded_globs<I, S>(mut self, patterns: I) -> Self
54 where
55 I: IntoIterator<Item = S>,
56 S: Into<String>,
57 {
58 self.path = self.path.with_excluded_globs(patterns);
59 self
60 }
61
62 #[must_use]
63 pub(super) fn with_excluded_regexes<I, S>(mut self, patterns: I) -> Self
64 where
65 I: IntoIterator<Item = S>,
66 S: Into<String>,
67 {
68 self.path = self.path.with_excluded_regexes(patterns);
69 self
70 }
71
72 #[must_use]
73 pub(super) fn with_excluded_segment_regexes<I, S>(mut self, patterns: I) -> Self
74 where
75 I: IntoIterator<Item = S>,
76 S: Into<String>,
77 {
78 self.path = self.path.with_excluded_segment_regexes(patterns);
79 self
80 }
81
82 #[must_use]
83 pub(super) fn prefixed(&self, ws_prefix: &str) -> Self {
84 Self {
85 path: self.path.prefixed(ws_prefix),
86 exports: self.exports.clone(),
87 }
88 }
89}
90
91#[derive(Debug, Clone, PartialEq, Eq)]
93pub struct PluginUsedExportRule {
94 pub(crate) plugin_name: String,
95 pub(crate) rule: UsedExportRule,
96}
97
98impl PluginUsedExportRule {
99 #[must_use]
100 pub(crate) fn new(plugin_name: impl Into<String>, rule: UsedExportRule) -> Self {
101 Self {
102 plugin_name: plugin_name.into(),
103 rule,
104 }
105 }
106
107 #[must_use]
108 pub(super) fn prefixed(&self, ws_prefix: &str) -> Self {
109 Self {
110 plugin_name: self.plugin_name.clone(),
111 rule: self.rule.prefixed(ws_prefix),
112 }
113 }
114}
115
116pub struct ConfigExportCredit<'a> {
129 matchers: PathRuleIndex<&'a [String]>,
130}
131
132impl<'a> ConfigExportCredit<'a> {
133 pub fn new(
135 plugin_result: Option<&'a AggregatedPluginResult>,
136 include_entry_exports: bool,
137 ) -> Self {
138 let matchers = plugin_result
139 .filter(|_| include_entry_exports)
140 .map(|pr| {
141 PathRuleIndex::build(
142 pr.config_export_credits
143 .iter()
144 .map(|rule| (&rule.rule.path, rule.rule.exports.as_slice())),
145 "config export credit pattern",
146 )
147 })
148 .unwrap_or_default();
149 Self { matchers }
150 }
151
152 pub fn is_empty(&self) -> bool {
154 self.matchers.is_empty()
155 }
156
157 pub fn exports_for(&self, path: &str) -> Vec<&'a [String]> {
160 self.matchers.matching(path).into_iter().copied().collect()
161 }
162
163 pub fn credits(credited: &[&[String]], export_name: &str) -> bool {
167 credited
168 .iter()
169 .any(|exports| exports.iter().any(|e| e == "*" || e == export_name))
170 }
171}
172
173pub fn is_export_ignored(
175 export_name: &str,
176 matching_ignore: &[&[String]],
177 matching_plugin: &[&[&str]],
178) -> bool {
179 matching_ignore
180 .iter()
181 .any(|exports| exports.iter().any(|e| e == "*" || e == export_name))
182 || matching_plugin
183 .iter()
184 .any(|exports| exports.contains(&"*") || exports.contains(&export_name))
185}