1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use derive_builder::Builder;
use node_semver::Version;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use url::Url;

use crate::{CorgiManifest, Manifest, PersonField};

/// A serializable representation of a Packument -- the toplevel metadata
/// object containing information about package versions, dist-tags, etc.
///
/// This version is a reduced-size packument that only contains fields from
/// "Corgi" packuments (or will only (de)serialize those specific fields).
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CorgiPackument {
    #[serde(default)]
    pub versions: HashMap<Version, CorgiVersionMetadata>,
    #[serde(default, rename = "dist-tags")]
    pub tags: HashMap<String, Version>,
}

/// A serializable representation of a Packument -- the toplevel metadata
/// object containing information about package versions, dist-tags, etc.
#[derive(Builder, Default, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Packument {
    #[serde(default)]
    pub versions: HashMap<Version, VersionMetadata>,
    #[serde(default)]
    pub time: HashMap<String, String>,
    #[serde(default, rename = "dist-tags")]
    pub tags: HashMap<String, Version>,
    #[serde(flatten)]
    pub rest: HashMap<String, Value>,
}

impl From<CorgiPackument> for Packument {
    fn from(value: CorgiPackument) -> Self {
        Packument {
            versions: value
                .versions
                .into_iter()
                .map(|(k, v)| (k, v.into()))
                .collect(),
            tags: value.tags,
            ..Default::default()
        }
    }
}

impl From<Packument> for CorgiPackument {
    fn from(value: Packument) -> Self {
        CorgiPackument {
            versions: value
                .versions
                .into_iter()
                .map(|(k, v)| (k, v.into()))
                .collect(),
            tags: value.tags,
        }
    }
}

/// A manifest for an individual package version.
///
/// This version is a reduced-size VersionMetadata that only contains fields
/// from "Corgi" packuments (or will only (de)serialize those specific
/// fields).
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CorgiVersionMetadata {
    #[serde(default)]
    pub dist: CorgiDist,
    #[serde(rename = "_hasShrinkwrap", skip_serializing_if = "Option::is_none")]
    pub has_shrinkwrap: Option<bool>,
    #[serde(flatten)]
    pub manifest: CorgiManifest,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deprecated: Option<String>,
}

/// A manifest for an individual package version.
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct VersionMetadata {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub maintainers: Vec<PersonField>,
    #[serde(rename = "_npmUser", skip_serializing_if = "Option::is_none")]
    pub npm_user: Option<NpmUser>,
    #[serde(default)]
    pub dist: Dist,
    #[serde(rename = "_hasShrinkwrap", skip_serializing_if = "Option::is_none")]
    pub has_shrinkwrap: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deprecated: Option<String>,

    #[serde(flatten)]
    pub manifest: Manifest,
}

impl From<CorgiVersionMetadata> for VersionMetadata {
    fn from(value: CorgiVersionMetadata) -> Self {
        VersionMetadata {
            dist: value.dist.into(),
            has_shrinkwrap: value.has_shrinkwrap,
            manifest: value.manifest.into(),
            ..Default::default()
        }
    }
}

impl From<VersionMetadata> for CorgiVersionMetadata {
    fn from(value: VersionMetadata) -> Self {
        CorgiVersionMetadata {
            dist: value.dist.into(),
            has_shrinkwrap: value.has_shrinkwrap,
            manifest: value.manifest.into(),
            deprecated: value.deprecated,
        }
    }
}

impl From<CorgiVersionMetadata> for CorgiManifest {
    fn from(value: CorgiVersionMetadata) -> Self {
        value.manifest
    }
}

impl From<VersionMetadata> for Manifest {
    fn from(value: VersionMetadata) -> Self {
        value.manifest
    }
}

/// Representation for the `bin` field in package manifests.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Bin {
    Str(String),
    Hash(HashMap<String, String>),
}

/// Represents a human!
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct NpmUser {
    pub name: String,
    pub email: Option<String>,
}

/// Distribution information for a particular package version.
///
/// This version is a reduced-size CorgiDist that only contains fields from
/// "Corgi" packuments (or will only (de)serialize those specific fields).
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct CorgiDist {
    pub shasum: Option<String>,
    pub tarball: Option<Url>,
    pub integrity: Option<String>,
    #[serde(rename = "npm-signature")]
    pub npm_signature: Option<String>,
}

/// Distribution information for a particular package version.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Dist {
    pub shasum: Option<String>,
    pub tarball: Option<Url>,

    pub integrity: Option<String>,
    #[serde(rename = "fileCount")]
    pub file_count: Option<usize>,
    #[serde(rename = "unpackedSize")]
    pub unpacked_size: Option<usize>,
    #[serde(rename = "npm-signature")]
    pub npm_signature: Option<String>,

    #[serde(flatten)]
    pub rest: HashMap<String, Value>,
}

impl From<CorgiDist> for Dist {
    fn from(value: CorgiDist) -> Self {
        Dist {
            shasum: value.shasum,
            tarball: value.tarball,
            integrity: value.integrity,
            npm_signature: value.npm_signature,
            ..Default::default()
        }
    }
}

impl From<Dist> for CorgiDist {
    fn from(value: Dist) -> Self {
        CorgiDist {
            shasum: value.shasum,
            tarball: value.tarball,
            integrity: value.integrity,
            npm_signature: value.npm_signature,
        }
    }
}