rust_jsr_registry/types/
package.rs

1use semver::{Version, VersionReq};
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4use url::Url;
5use std::collections::HashMap;
6
7use crate::{priv_as_ref, priv_from_info, priv_impl_getinfo};
8
9use super::{graph::two::ModuleGraph2};
10
11/// The package result
12///
13/// See https://jsr.io/docs/api#package-version-metadata
14#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
15#[serde(rename_all = "camelCase")]
16pub struct Package {
17    /// List of file manifests in one package
18    /// 
19    /// ([HashMap] Key prefix: `/` ([see this example](https://jsr.io/@dunno/obfuscatemail/1.5.2_meta.json) for differences))
20    pub manifest: HashMap<String, Manifest>,
21    /// Module graph 1. Since it was used **only** for old & early JSR packages ([example](https://jsr.io/@luca/flag/1.0.1_meta.json)),
22    /// [Package::module_graph2] was widely used
23    /// 
24    /// ([HashMap] Key prefix: `/` ([see this example](https://jsr.io/@dunno/obfuscatemail/1.5.2_meta.json) for differences))
25    pub module_graph1: Option<HashMap<String, Value>>,
26    /// Module graph 2. This is the most widely used graph on JSR packages
27    /// 
28    /// ([HashMap] Key prefix: `/` ([see this example](https://jsr.io/@dunno/obfuscatemail/1.5.2_meta.json) for differences))
29    pub module_graph2: Option<HashMap<String, ModuleGraph2>>,
30    /// Exported files. For main entry, usually `.` exist. But can be changed using `./` prefix
31    /// 
32    /// The key and value on `exports` outputted from `meta.json` are converted to [HashMap]
33    /// 
34    /// So if `deno.json` file only has this
35    /// ```json
36    /// "exports":"hi.ts"
37    /// ```
38    /// 
39    /// JSR will convert it as this in `meta.json`
40    /// 
41    /// ```json
42    /// "exports":{
43    ///   ".":"./hi.ts"
44    /// }
45    /// ```
46    /// 
47    /// Though, you can change it so it dosent contain the main entry:
48    /// 
49    /// ```json
50    /// "exports":{
51    ///   "./hi":"./hi.ts"
52    /// }
53    /// ```
54    /// 
55    /// JSR will kept it like that
56    pub exports:HashMap<String, String>
57}
58
59/// File manifests for [Package::manifest]
60#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
61pub struct Manifest {
62    /// Object size
63    pub size: u64,
64    /// Object checksum (dosent validated, beware)
65    pub checksum: String,
66}
67
68/// Creates a builder for [Package]
69#[derive(Debug, Clone)]
70pub struct PackageBuilder {
71    /// Package scope
72    ///
73    /// ## Example
74    ///
75    /// `dunno` from `@dunno/object`
76    pub scope: String,
77    /// Package name
78    ///
79    /// ## Example
80    ///
81    /// `object` from `@dunno/object`
82    pub name: String,
83    /// Package version, parsed as [semver::Version]
84    pub version: Version,
85}
86/// Creates a builder for [Package]
87impl PackageBuilder {
88    /// Creates a builder for [Package]
89    #[allow(clippy::new_without_default)]
90    pub fn new() -> Self {
91        return Self {
92            version: Version::new(0, 0, 0),
93            scope: "".to_string(),
94            name: "".to_string(),
95        };
96    }
97    /// Set package version to choose
98    pub fn set_version(mut self, value: Version) -> Self {
99        self.version = value;
100        return self;
101    }
102    priv_from_info!(version: Version::new(0, 0, 0));
103}
104impl PartialEq for PackageBuilder {
105    fn eq(&self, other: &Self) -> bool {
106        return self.scope == other.scope && self.name == other.name && self.version.to_string() == other.version.to_string();
107    }
108}
109impl Eq for PackageBuilder {}
110priv_as_ref!(PackageBuilder);
111priv_impl_getinfo!(PackageBuilder);
112
113/// Distribution infos for [NpmCompPackage::dist]
114#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
115pub struct NpmCompDist {
116    /// The package tarball from npm
117    #[serde(with = "crate::serde::url")]
118    pub tarball:Url,
119    /// The shasum hash from package
120    pub shasum:String,
121    /// Package integrity (as sha-256)
122    pub integrity:String,
123}
124priv_as_ref!(NpmCompDist);
125/// The package result
126///
127/// See https://jsr.io/docs/api#package-version-metadata
128#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
129#[serde(rename_all = "camelCase")]
130pub struct NpmCompPackage {
131    /// JSR-to-npm equivalent package name
132    /// 
133    /// Note: this is different than normal [Meta::name].
134    /// 
135    /// If your [MetaBuilder] contains like this
136    /// 
137    /// ```
138    /// MetaBuilder::new()
139    ///     .set_scope("dunno")
140    ///     .set_name("object")
141    /// ```
142    /// 
143    /// It would be added like this, right?
144    /// 
145    /// ```
146    /// {
147    ///     scope: "dunno",
148    ///     name: "object"
149    /// }
150    /// ```
151    /// 
152    /// Since the scope (in JSR) is actually fake on npm, it would be listed as
153    /// 
154    /// `@<jsr provider scope>/<jsr package scope>__<jsr package name>`
155    /// 
156    /// So, this [Self::name] is equivalent to
157    /// 
158    /// `@jsr/dunno__object`
159    pub name:String,
160    /// The version currently selected
161    pub version:Version,
162    /// Package desription
163    pub description:String,
164    /// Distribution info
165    pub dist:NpmCompDist,
166    /// Timestamp for package activities
167    pub dependencies:HashMap<String, VersionReq>
168}
169priv_as_ref!(NpmCompPackage);