webpack_stats/common/
import.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
17use crate::module::ModuleName;
18use serde::{Deserialize, Deserializer};
19use std::borrow::Cow;
20use std::path::Path;
21use std::str::FromStr;
22use thiserror::Error;
23
24#[derive(Deserialize, Debug, Default)]
25#[serde(transparent)]
26pub struct SourceFilePath<'a>(#[serde(borrow)] pub Cow<'a, Path>);
27
28#[derive(Deserialize, Debug, Default)]
29#[serde(transparent)]
30pub struct SourceText<'a>(#[serde(borrow)] Cow<'a, str>);
31
32#[derive(Debug, Copy, Clone)]
33pub enum ImportType {
34    // TODO: Figure out what the actual webpack values here are. The documentation is
35    // sparse as hell.
36    /// Webpack require.context call.
37    RequireContext,
38    /// ES6 import statement
39    Import,
40    /// Deferred (async) import statement import()
41    ImportDynamic,
42    /// CJS Require statement
43    Require,
44    CJSSelfExport,
45    /// Required by default as an entrypoint
46    Entry,
47    // Harmony imports
48    Es6SideEffect,
49    // export { } from "..."
50    Es6ExportImport,
51
52    // ?????
53    ModuleDecorator,
54
55    Url,
56
57    ///
58    AmdRequire,
59    /// The value was missing from the stats file
60    Empty,
61}
62
63impl Default for ImportType {
64    fn default() -> Self {
65        Self::Empty
66    }
67}
68
69#[derive(Error, Debug)]
70#[error("Invalid import type: {msg}")]
71pub struct ImportTypeError {
72    msg: String,
73}
74
75impl FromStr for ImportType {
76    type Err = ImportTypeError;
77
78    fn from_str(s: &str) -> Result<Self, Self::Err> {
79        match s {
80            "require.context" => Ok(Self::RequireContext),
81            "import" => Ok(Self::Import),
82            "import()" => Ok(Self::ImportDynamic),
83            "require" | "cjs require" | "cjs full require" => Ok(Self::Require),
84            "entry" => Ok(Self::Entry),
85            "harmony side effect evaluation" => Ok(Self::Es6SideEffect),
86            "harmony import specifier" => Ok(Self::Import),
87
88            "cjs self exports reference" => Ok(Self::CJSSelfExport),
89            "cjs export require" => Ok(Self::CJSSelfExport),
90            "harmony export imported specifier" => Ok(Self::Es6ExportImport),
91
92            "module decorator" => Ok(Self::ModuleDecorator),
93            "new URL()" => Ok(Self::Url),
94
95            "amd require" => Ok(Self::AmdRequire),
96            _ => Err(ImportTypeError { msg: s.to_owned() }),
97        }
98    }
99}
100
101impl<'de> Deserialize<'de> for ImportType {
102    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
103    where
104        D: Deserializer<'de>,
105    {
106        let v = <&str>::deserialize(deserializer)?;
107
108        Self::from_str(v).map_err(|e| serde::de::Error::custom(e.to_string()))
109    }
110}
111
112#[derive(Deserialize, Debug, Default)]
113#[serde(transparent)]
114pub struct ImportString<'a>(Cow<'a, str>);
115
116pub struct ResolvedModule(pub ModuleName);