webpack_stats/v5/
reason.rs

1/*
2 * Copyright [2022] [Kevin Velasco]
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Modules or chunks may be in the graph for a multitude of
18//! [reasons](Reason). This type attempts to abstract over the constellation
19//! of boolean flags in webpack stats to provide some semantics
20//!
21//! [`Reason`] is meant to be used in the context of collections. It does
22//! not provide a default value.
23
24use crate::common::import::{ImportString, ImportType};
25use crate::common::module::{ModuleId, ModuleIdentifier, ModuleName, RelativeModulePath};
26use empty_type::{Empty, EmptyType};
27use serde::{Deserialize, Deserializer};
28use std::borrow::Cow;
29use std::ops::Deref;
30
31#[derive(Debug, Default)]
32pub struct Reasons<'a> {
33    reasons: Vec<Reason<'a>>,
34}
35
36impl<'a> Deref for Reasons<'a> {
37    type Target = Vec<Reason<'a>>;
38    fn deref(&self) -> &Self::Target {
39        &self.reasons
40    }
41}
42
43impl<'de: 'a, 'a> Deserialize<'de> for Reasons<'a> {
44    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
45    where
46        D: Deserializer<'de>,
47    {
48        type Container<'a> = Vec<Empty<Reason<'a>>>;
49
50        let value = <Container<'a> as Deserialize>::deserialize(deserializer)?;
51
52        let resolved = value
53            .into_iter()
54            .filter(|module| module.module_identifier.is_some())
55            .map(Empty::resolve)
56            .collect();
57        Ok(Self { reasons: resolved })
58    }
59}
60
61/// Metadata to describe the source of an import.
62/// Normally helps locate the upstream modules that
63/// required this one
64#[derive(Deserialize, Debug, EmptyType)]
65#[serde(rename_all = "camelCase", default)]
66#[empty(bounds = "'a", deserialize)]
67pub struct Reason<'a> {
68    #[serde(borrow)]
69    #[empty(fail_safe)]
70    pub loc: Cow<'a, str>,
71    pub module: RelativeModulePath<'a>,
72    pub module_id: Option<ModuleId>,
73    pub module_name: ModuleName,
74    pub resolved_module: ModuleName,
75    pub module_identifier: ModuleIdentifier,
76    pub r#type: ImportType,
77    #[empty(fail_safe)]
78    pub user_request: ImportString<'a>,
79}