webpack_stats/common/
chunk.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::ModuleIdentifier;
18use crate::SizeBytes;
19use meshed::prelude::*;
20use serde::{Deserialize, Serialize};
21use std::borrow::Cow;
22use std::fmt::{Display, Formatter};
23use zerovec::ule::AsULE;
24
25#[derive(Deserialize, Serialize, Debug, Clone, Hash, PartialOrd, PartialEq, Eq)]
26#[serde(transparent)]
27pub struct ChunkName<'a>(#[serde(borrow)] Cow<'a, str>);
28
29#[derive(Deserialize, Serialize, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Copy, Clone)]
30#[repr(transparent)]
31pub struct ChunkId(pub u32);
32
33impl Display for ChunkId {
34    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
35        Display::fmt(&self.0, f)
36    }
37}
38
39impl Identity for ChunkId {}
40
41impl AsULE for ChunkId {
42    type ULE = <u32 as AsULE>::ULE;
43
44    fn to_unaligned(self) -> Self::ULE {
45        u32::to_unaligned(self.0)
46    }
47
48    fn from_unaligned(unaligned: Self::ULE) -> Self {
49        Self(u32::from_unaligned(unaligned))
50    }
51}
52
53pub struct ChunkChild;
54pub struct ChunkParentOrSibling;
55
56#[repr(transparent)]
57pub struct ChunkChildren(pub Vec<ChunkId>);
58pub type ChunkModules = Vec<ModuleIdentifier>;
59#[derive(Debug)]
60pub struct ChunkInitial(pub bool);
61
62#[derive(Debug)]
63pub struct Files(pub Vec<String>);
64
65pub trait Chunk:
66    Identifiable<ChunkId>
67    + Edges<ChunkId, ChunkChild>
68    + Edges<ChunkId, ChunkParentOrSibling>
69    + ExtractData<ChunkId>
70    + ExtractData<ChunkChildren>
71    + ExtractData<SizeBytes>
72    + ExtractData<ChunkModules>
73    + ExtractData<ChunkInitial>
74    + ExtractData<Files>
75    + Label<Label = ChunkId>
76{
77}
78
79pub trait Chunks<T>: Query<ChunkId, T>
80where
81    T: Chunk,
82{
83}