Skip to main content

kellnr_common/
publish_metadata.rs

1use std::collections::{BTreeMap, HashMap};
2
3use serde::{Deserialize, Serialize};
4
5use crate::index_metadata::IndexDep;
6
7// The Metadata struct defined here is the one send by Cargo to the registry.
8// It is different to the one saved in the index!
9// See: https://doc.rust-lang.org/cargo/reference/registries.html#publish
10// Crates.io implementation: https://github.com/rust-lang/crates.io/blob/5c7070829418f72639dd0b91993eaf91e516416b/src/views/krate_publish.rs
11
12#[derive(Default, Serialize, Deserialize, Debug, PartialEq, Eq)]
13pub struct PublishMetadata {
14    // The name of the package.
15    pub name: String,
16    // The version of the package being published.
17    pub vers: String,
18    // Array of direct dependencies of the package.
19    pub deps: Vec<RegistryDep>,
20    // Set of features defined for the package.
21    // Each feature maps to an array of features or dependencies it enables.
22    // Cargo does not impose limitations on feature names, but crates.io
23    // requires alphanumeric ASCII, `_` or `-` characters.
24    pub features: BTreeMap<String, Vec<String>>,
25    // List of strings of the authors.
26    // May be empty.
27    pub authors: Option<Vec<String>>,
28    // Description field from the manifest.
29    // May be null. crates.io requires at least some content.
30    pub description: Option<String>,
31    // String of the URL to the website for this package's documentation.
32    // May be null.
33    pub documentation: Option<String>,
34    // String of the URL to the website for this package's home page.
35    // May be null.
36    pub homepage: Option<String>,
37    // String of the content of the README file.
38    // May be null.
39    pub readme: Option<String>,
40    // String of a relative path to a README file in the crate.
41    // May be null.
42    pub readme_file: Option<String>,
43    // Array of strings of keywords for the package.
44    #[serde(default)]
45    pub keywords: Vec<String>,
46    // Array of strings of categories for the package.
47    #[serde(default)]
48    pub categories: Vec<String>,
49    // String of the license for the package.
50    // May be null. crates.io requires either `license` or `license_file` to be set.
51    pub license: Option<String>,
52    // String of a relative path to a license file in the crate.
53    // May be null.
54    pub license_file: Option<String>,
55    // String of the URL to the website for the source repository of this package.
56    // May be null.
57    pub repository: Option<String>,
58    // Optional object of "status" badges. Each value is an object of
59    // arbitrary string to string mappings.
60    // crates.io has special interpretation of the format of the badges.
61    pub badges: Option<HashMap<String, Option<HashMap<String, String>>>>,
62    // The `links` string value from the package's manifest, or null if not
63    // specified. This field is optional and defaults to null.
64    #[serde(default)]
65    pub links: Option<String>,
66    // The minimal supported Rust version (optional)
67    // This must be a valid version requirement without an operator (e.g. no `=`)
68    #[serde(rename(serialize = "rust-version"))]
69    pub rust_version: Option<String>,
70}
71
72impl PublishMetadata {
73    pub fn minimal(name: &str, vers: &str) -> Self {
74        Self {
75            name: name.to_string(),
76            vers: vers.to_string(),
77            ..Default::default()
78        }
79    }
80}
81
82#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
83pub struct RegistryDep {
84    // Name of the dependency.
85    // If the dependency is renamed from the original package name,
86    // this is the original name. The new package name is stored in
87    // the `explicit_name_in_toml` field.
88    pub name: String,
89    // The semver requirement for this dependency.
90    pub version_req: String,
91    // Array of features (as strings) enabled for this dependency.
92    pub features: Option<Vec<String>>,
93    // Boolean of whether or not this is an optional dependency.
94    pub optional: bool,
95    // Boolean of whether or not default features are enabled.
96    pub default_features: bool,
97    // The target platform for the dependency.
98    // null if not a target dependency.
99    // Otherwise, a string such as "cfg(windows)".
100    pub target: Option<String>,
101    // The dependency kind.
102    // "dev", "build", or "normal".
103    pub kind: Option<String>,
104    // The URL of the index of the registry where this dependency is
105    // from as a string. If not specified or null, it is assumed the
106    // dependency is in the current registry.
107    pub registry: Option<String>,
108    // If the dependency is renamed, this is a string of the new
109    // package name. If not specified or null, this dependency is not
110    // renamed.
111    pub explicit_name_in_toml: Option<String>,
112}
113
114impl From<IndexDep> for RegistryDep {
115    fn from(dep: IndexDep) -> Self {
116        RegistryDep {
117            name: match dep.package {
118                Some(ref package) => package.clone(),
119                None => dep.name.clone(),
120            },
121            version_req: dep.req,
122            features: Some(dep.features),
123            optional: dep.optional,
124            default_features: dep.default_features,
125            target: dep.target,
126            kind: dep.kind.map(|k| k.to_string()),
127            registry: dep.registry,
128            explicit_name_in_toml: match dep.package {
129                Some(_) => Some(dep.name),
130                None => None,
131            },
132        }
133    }
134}