freighter_api_types/index/response.rs
1use super::DependencyKind;
2use chrono::{DateTime, Utc};
3use semver::{Version, VersionReq};
4#[cfg(any(feature = "index", feature = "client"))]
5use serde::Deserialize;
6#[cfg(any(feature = "index", feature = "server"))]
7use serde::Serialize;
8use std::collections::HashMap;
9
10#[cfg_attr(feature = "client", derive(Deserialize))]
11#[cfg_attr(feature = "server", derive(Serialize))]
12#[cfg_attr(any(feature = "client", feature = "server"), serde(rename_all = "kebab-case"))]
13pub struct RegistryConfig {
14 pub dl: String,
15 pub api: String,
16 pub auth_required: bool,
17}
18
19#[derive(Debug)]
20#[cfg_attr(feature = "client", derive(Deserialize))]
21#[cfg_attr(feature = "server", derive(Serialize))]
22pub struct CompletedPublication {
23 /// Optional object of warnings to display to the user.
24 pub warnings: Option<CompletedPublicationWarnings>,
25}
26
27#[derive(Debug)]
28#[cfg_attr(feature = "client", derive(Deserialize))]
29#[cfg_attr(feature = "server", derive(Serialize))]
30pub struct CompletedPublicationWarnings {
31 /// Array of strings of categories that are invalid and ignored.
32 pub invalid_categories: Vec<String>,
33 /// Array of strings of badge names that are invalid and ignored.
34 pub invalid_badges: Vec<String>,
35 /// Array of strings of arbitrary warnings to display to the user.
36 pub other: Vec<String>,
37}
38
39#[cfg_attr(any(feature = "index", feature = "client"), derive(Deserialize))]
40#[cfg_attr(any(feature = "index", feature = "server"), derive(Serialize))]
41#[derive(Clone, Eq, PartialEq)]
42pub struct CrateVersion {
43 /// The name of the package.
44 ///
45 /// This must only contain alphanumeric, `-`, or `_` characters.
46 pub name: String,
47 /// The version of the package this row is describing.
48 ///
49 /// This must be a valid version number according to the Semantic Versioning 2.0.0 spec at
50 /// <https://semver.org/>.
51 pub vers: Version,
52 /// Array of direct dependencies of the package.
53 pub deps: Vec<Dependency>,
54 /// A SHA256 checksum of the `.crate` file.
55 pub cksum: String,
56 /// Set of features defined for the package.
57 ///
58 /// Each feature maps to an array of features or dependencies it enables.
59 pub features: HashMap<String, Vec<String>>,
60 /// Boolean of whether or not this version has been yanked.
61 pub yanked: bool,
62 /// The `links` string value from the package's manifest, or null if not specified.
63 ///
64 /// This field is optional and defaults to null.
65 pub links: Option<String>,
66 /// An unsigned 32-bit integer value indicating the schema version of this entry.
67 ///
68 /// If this not specified, it should be interpreted as the default of 1.
69 ///
70 /// Cargo (starting with version 1.51) will ignore versions it does not recognize.
71 /// This provides a method to safely introduce changes to index entries and allow older
72 /// versions of cargo to ignore newer entries it doesn't understand. Versions older than 1.51
73 /// ignore this field, and thus may misinterpret the meaning of the index entry.
74 ///
75 /// The current values are:
76 ///
77 /// * 1: The schema as documented here, not including newer additions.
78 /// This is honored in Rust version 1.51 and newer.
79 /// * 2: The addition of the `features2` field.
80 /// This is honored in Rust version 1.60 and newer.
81 #[cfg_attr(feature = "client", serde(default = "default_v"))]
82 pub v: u32,
83 /// This optional field contains features with new, extended syntax.
84 ///
85 /// Specifically, namespaced features (`dep:`) and weak dependencies (`pkg?/feat`).
86 ///
87 /// This is separated from `features` because versions older than 1.19 will fail to load due to
88 /// not being able to parse the new syntax, even with a `Cargo.lock` file.
89 ///
90 /// Cargo will merge any values listed here with the "features" field.
91 ///
92 /// If this field is included, the "v" field should be set to at least 2.
93 ///
94 /// Registries are not required to use this field for extended feature syntax, they are allowed
95 /// to include those in the "features" field. Using this is only necessary if the registry
96 /// wants to support cargo versions older than 1.19, which in practice is only crates.io since
97 /// those older versions do not support other registries.
98 #[cfg_attr(feature = "client", serde(default))]
99 pub features2: HashMap<String, Vec<String>>,
100}
101
102#[cfg_attr(any(feature = "index", feature = "client"), derive(Deserialize))]
103#[cfg_attr(any(feature = "index", feature = "server"), derive(Serialize))]
104#[derive(Clone, Eq, PartialEq)]
105pub struct Dependency {
106 /// Name of the dependency.
107 ///
108 /// If the dependency is renamed from the original package name, this is the new name.
109 /// The original package name is stored in the `package` field.
110 pub name: String,
111 /// The SemVer requirement for this dependency.
112 ///
113 /// This must be a valid version requirement defined at
114 /// <https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html>.
115 pub req: VersionReq,
116 /// Array of features (as strings) enabled for this dependency.
117 pub features: Vec<String>,
118 /// Boolean of whether or not this is an optional dependency.
119 pub optional: bool,
120 /// Boolean of whether or not default features are enabled.
121 pub default_features: bool,
122 /// The target platform for the dependency.
123 ///
124 /// null if not a target dependency. Otherwise, a string such as `cfg(windows)`.
125 pub target: Option<String>,
126 /// The dependency kind.
127 ///
128 /// "dev", "build", or "normal".
129 /// Note: this is a required field, but a small number of entries exist in the crates.io index
130 /// with either a missing or null kind` field due to implementation bugs.
131 pub kind: DependencyKind,
132 /// The URL of the index of the registry where this dependency is from as a string.
133 ///
134 /// If not specified or null, it is assumed the dependency is in the current registry.
135 pub registry: Option<String>,
136 /// If the dependency is renamed, this is a string of the actual package name.
137 ///
138 /// If not specified or null, this dependency is not
139 /// renamed.
140 pub package: Option<String>,
141}
142
143#[cfg_attr(feature = "client", derive(Deserialize))]
144#[cfg_attr(feature = "server", derive(Serialize))]
145pub struct ListAll {
146 pub results: Vec<ListAllCrateEntry>,
147}
148
149#[cfg_attr(feature = "client", derive(Deserialize))]
150#[cfg_attr(feature = "server", derive(Serialize))]
151pub struct ListAllCrateEntry {
152 /// Name of the crate.
153 pub name: String,
154 /// List of published versions of the crate.
155 pub versions: Vec<ListAllCrateVersion>,
156 /// Textual description of the crate.
157 pub description: String,
158 /// Date and time that this crate was created.
159 pub created_at: DateTime<Utc>,
160 /// Date and time that this crate was last updated.
161 pub updated_at: DateTime<Utc>,
162 /// Optional homepage for the crate.
163 pub homepage: Option<String>,
164 /// Optional repository link.
165 pub repository: Option<String>,
166 /// Optional documentation link.
167 pub documentation: Option<String>,
168 /// List of keywords for the crate.
169 pub keywords: Vec<String>,
170 /// List of categories for the crate.
171 pub categories: Vec<String>,
172}
173
174#[cfg_attr(feature = "client", derive(Deserialize))]
175#[cfg_attr(feature = "server", derive(Serialize))]
176pub struct ListAllCrateVersion {
177 pub version: Version,
178}
179
180#[cfg_attr(feature = "client", derive(Deserialize))]
181#[cfg_attr(feature = "server", derive(Serialize))]
182pub struct SearchResults {
183 /// Array of results.
184 pub crates: Vec<SearchResultsEntry>,
185 pub meta: SearchResultsMeta,
186}
187
188#[cfg_attr(feature = "client", derive(Deserialize))]
189#[cfg_attr(feature = "server", derive(Serialize))]
190pub struct SearchResultsMeta {
191 /// Total number of results available on the server.
192 pub total: usize,
193}
194
195#[cfg_attr(feature = "client", derive(Deserialize))]
196#[cfg_attr(feature = "server", derive(Serialize))]
197pub struct SearchResultsEntry {
198 /// Name of the crate.
199 pub name: String,
200 /// The highest version available.
201 pub max_version: Version,
202 /// Textual description of the crate.
203 pub description: String,
204}
205
206#[cfg(feature = "client")]
207fn default_v() -> u32 {
208 1
209}
210
211impl CrateVersion {
212 /// "Normalize" a crate version such that any functionally equivalent versions will be identical.
213 pub fn normalize(&mut self) {
214 self.normalize_features();
215 self.normalize_dependencies();
216 }
217
218 fn normalize_dependencies(&mut self) {
219 for d in &mut self.deps {
220 d.features.sort();
221 }
222
223 self.deps.sort_by_cached_key(|x| {
224 (
225 x.registry.clone(),
226 x.name.clone(),
227 x.package.clone(),
228 x.req.to_string(),
229 x.kind,
230 x.target.clone(),
231 )
232 });
233 }
234
235 fn normalize_features(&mut self) {
236 let mut features_2 = HashMap::new();
237
238 std::mem::swap(&mut features_2, &mut self.features2);
239
240 for (k, mut v) in features_2 {
241 v.sort();
242
243 self.features.insert(k, v);
244 }
245 }
246}