use super::{AggregatedPluginResult, CompiledPathRule, PathRule, PluginResult};
impl PluginResult {
pub(super) fn push_used_export_rule(
&mut self,
pattern: impl Into<String>,
exports: impl IntoIterator<Item = impl Into<String>>,
) {
self.used_exports
.push(UsedExportRule::new(pattern, exports));
}
pub(super) fn push_entry_export_credit(
&mut self,
pattern: impl Into<String>,
exports: impl IntoIterator<Item = impl Into<String>>,
) {
self.entry_export_credits
.push(UsedExportRule::new(pattern, exports));
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct UsedExportRule {
pub(crate) path: PathRule,
pub(crate) exports: Vec<String>,
}
impl UsedExportRule {
#[must_use]
pub(crate) fn new(
pattern: impl Into<String>,
exports: impl IntoIterator<Item = impl Into<String>>,
) -> Self {
Self {
path: PathRule::new(pattern),
exports: exports.into_iter().map(Into::into).collect(),
}
}
#[must_use]
pub(super) fn from_static(pattern: &'static str, exports: &'static [&'static str]) -> Self {
Self::new(pattern, exports.iter().copied())
}
#[must_use]
pub(super) fn with_excluded_globs<I, S>(mut self, patterns: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.path = self.path.with_excluded_globs(patterns);
self
}
#[must_use]
pub(super) fn with_excluded_regexes<I, S>(mut self, patterns: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.path = self.path.with_excluded_regexes(patterns);
self
}
#[must_use]
pub(super) fn with_excluded_segment_regexes<I, S>(mut self, patterns: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.path = self.path.with_excluded_segment_regexes(patterns);
self
}
#[must_use]
pub(super) fn prefixed(&self, ws_prefix: &str) -> Self {
Self {
path: self.path.prefixed(ws_prefix),
exports: self.exports.clone(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PluginUsedExportRule {
pub(crate) plugin_name: String,
pub(crate) rule: UsedExportRule,
}
impl PluginUsedExportRule {
#[must_use]
pub(crate) fn new(plugin_name: impl Into<String>, rule: UsedExportRule) -> Self {
Self {
plugin_name: plugin_name.into(),
rule,
}
}
#[must_use]
pub(super) fn prefixed(&self, ws_prefix: &str) -> Self {
Self {
plugin_name: self.plugin_name.clone(),
rule: self.rule.prefixed(ws_prefix),
}
}
}
pub struct ConfigExportCredit<'a> {
matchers: Vec<(CompiledPathRule, &'a [String])>,
}
impl<'a> ConfigExportCredit<'a> {
pub fn new(
plugin_result: Option<&'a AggregatedPluginResult>,
include_entry_exports: bool,
) -> Self {
let matchers = plugin_result
.filter(|_| include_entry_exports)
.map(|pr| {
pr.config_export_credits
.iter()
.filter_map(|rule| {
CompiledPathRule::for_used_export_rule(
&rule.rule.path,
"config export credit pattern",
)
.map(|matcher| (matcher, rule.rule.exports.as_slice()))
})
.collect()
})
.unwrap_or_default();
Self { matchers }
}
pub fn is_empty(&self) -> bool {
self.matchers.is_empty()
}
pub fn exports_for(&self, path: &str) -> Vec<&'a [String]> {
self.matchers
.iter()
.filter(|(matcher, _)| matcher.matches(path))
.map(|(_, exports)| *exports)
.collect()
}
pub fn credits(credited: &[&[String]], export_name: &str) -> bool {
credited
.iter()
.any(|exports| exports.iter().any(|e| e == "*" || e == export_name))
}
}