Skip to main content

kellnr_common/
crate_data.rs

1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4use utoipa::ToSchema;
5
6use crate::index_metadata::IndexDep;
7use crate::publish_metadata::RegistryDep;
8
9#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
10pub struct CrateData {
11    pub name: String,
12    // additional information from kellnr about the crate
13    pub owners: Vec<String>,
14    pub max_version: String,
15    pub total_downloads: i64,
16    pub last_updated: String,
17    // metadata information from the publishing
18    pub homepage: Option<String>,
19    pub description: Option<String>,
20    pub repository: Option<String>,
21    pub categories: Vec<String>,
22    pub keywords: Vec<String>,
23    pub authors: Vec<String>,
24    pub versions: Vec<CrateVersionData>,
25}
26
27#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
28pub struct CrateVersionData {
29    pub version: String,
30    // additional information about the crate version from kellnr
31    pub created: String,
32    pub downloads: i64,
33    // metadata information from the publishing
34    pub readme: Option<String>,
35    pub license: Option<String>,
36    pub license_file: Option<String>,
37    pub documentation: Option<String>,
38    pub dependencies: Vec<CrateRegistryDep>,
39    pub checksum: String,
40    pub features: BTreeMap<String, Vec<String>>,
41    pub yanked: bool,
42    pub links: Option<String>,
43    pub v: i32,
44}
45
46#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
47pub struct CrateRegistryDep {
48    // Name of the dependency.
49    // If the dependency is renamed from the original package name,
50    // this is the original name. The new package name is stored in
51    // the `explicit_name_in_toml` field.
52    pub name: String,
53    pub description: Option<String>,
54    // The semver requirement for this dependency.
55    pub version_req: String,
56    // Array of features (as strings) enabled for this dependency.
57    pub features: Option<Vec<String>>,
58    // Boolean of whether or not this is an optional dependency.
59    pub optional: bool,
60    // Boolean of whether or not default features are enabled.
61    pub default_features: bool,
62    // The target platform for the dependency.
63    // null if not a target dependency.
64    // Otherwise, a string such as "cfg(windows)".
65    pub target: Option<String>,
66    // The dependency kind.
67    // "dev", "build", or "normal".
68    pub kind: Option<String>,
69    // The URL of the index of the registry where this dependency is
70    // from as a string. If not specified or null, it is assumed the
71    // dependency is in the current registry.
72    pub registry: Option<String>,
73    // If the dependency is renamed, this is a string of the new
74    // package name. If not specified or null, this dependency is not
75    // renamed.
76    pub explicit_name_in_toml: Option<String>,
77}
78
79impl From<RegistryDep> for CrateRegistryDep {
80    fn from(dep: RegistryDep) -> Self {
81        CrateRegistryDep {
82            name: dep.name,
83            description: None,
84            version_req: dep.version_req,
85            features: dep.features,
86            optional: dep.optional,
87            default_features: dep.default_features,
88            target: dep.target,
89            kind: dep.kind,
90            registry: dep.registry,
91            explicit_name_in_toml: dep.explicit_name_in_toml,
92        }
93    }
94}
95
96impl CrateRegistryDep {
97    pub fn from_index(desc: Option<String>, dep: IndexDep) -> Self {
98        CrateRegistryDep {
99            name: match dep.package {
100                Some(ref package) => package.clone(),
101                None => dep.name.clone(),
102            },
103            description: desc,
104            version_req: dep.req,
105            features: Some(dep.features),
106            optional: dep.optional,
107            default_features: dep.default_features,
108            target: dep.target,
109            kind: dep.kind.map(|k| k.to_string()),
110            registry: dep.registry,
111            explicit_name_in_toml: match dep.package {
112                Some(_) => Some(dep.name),
113                None => None,
114            },
115        }
116    }
117}