Skip to main content

cargo_toml2/
manifest.rs

1//! Structures for pasing Cargo.toml
2use serde_derive::{Deserialize, Serialize};
3use std::{collections::BTreeMap, path::PathBuf};
4use toml::Value;
5
6#[derive(Deserialize, Debug, Serialize, Clone)]
7#[serde(untagged)]
8pub enum StringOrBool {
9    String(String),
10    Bool(bool),
11}
12
13type DependencyT = BTreeMap<String, Dependency>;
14
15/// The root Cargo.toml
16#[derive(Deserialize, Debug, Serialize, Default, Clone)]
17#[serde(rename_all = "kebab-case")]
18pub struct CargoToml {
19    pub package: Package,
20    pub badges: Option<Badges>,
21    pub dependencies: Option<DependencyT>,
22    pub dev_dependencies: Option<DependencyT>,
23    pub build_dependencies: Option<DependencyT>,
24    pub target: Option<Target>,
25    pub profile: Option<Profile>,
26    pub features: Option<Features>,
27    pub workspace: Option<Workspace>,
28    #[serde(rename = "example")]
29    pub examples: Option<Vec<TargetConfig>>,
30    #[serde(rename = "bin")]
31    pub bins: Option<Vec<TargetConfig>>,
32    pub lib: Option<TargetConfig>,
33    #[serde(rename = "bench")]
34    pub benches: Option<Vec<TargetConfig>>,
35    #[serde(rename = "test")]
36    pub tests: Option<Vec<TargetConfig>>,
37    pub patch: Option<Patches>,
38    pub replace: Option<DependencyT>,
39}
40
41#[derive(Deserialize, Debug, Serialize, Default, Clone)]
42#[serde(rename_all = "kebab-case")]
43pub struct Package {
44    pub name: String,
45    pub version: String,
46    pub authors: Vec<String>,
47    pub edition: Option<String>,
48    pub build: Option<StringOrBool>,
49    pub links: Option<String>,
50    pub documentation: Option<String>,
51    pub exclude: Option<Vec<String>>,
52    pub include: Option<Vec<String>>,
53    pub publish: Option<bool>,
54    pub workspace: Option<PathBuf>,
55    pub description: Option<String>,
56    pub homepage: Option<String>,
57    pub repository: Option<String>,
58    pub readme: Option<String>,
59    pub keywords: Option<Vec<String>>,
60    pub categories: Option<Vec<String>>,
61    pub license: Option<String>,
62    pub license_file: Option<String>,
63    pub autobins: Option<bool>,
64    pub autoexamples: Option<bool>,
65    pub autotests: Option<bool>,
66    pub autobenches: Option<bool>,
67    pub metadata: Option<BTreeMap<String, Value>>,
68}
69
70#[derive(Deserialize, Debug, Serialize, Default, Clone)]
71#[serde(rename_all = "kebab-case")]
72pub struct Badges {
73    pub appveyor: Option<BuildBadge>,
74    pub circle_ci: Option<BuildBadge>,
75    pub gitlab: Option<BuildBadge>,
76    pub travis_ci: Option<BuildBadge>,
77    pub codecov: Option<BuildBadge>,
78    pub coveralls: Option<BuildBadge>,
79    pub is_it_maintained_issue_resolution: Option<BuildBadge>,
80    pub is_it_maintained_open_issues: Option<BuildBadge>,
81    pub maintenance: Option<Maintenance>,
82}
83
84#[derive(Deserialize, Debug, Serialize, Default, Clone)]
85pub struct Maintenance {
86    pub status: String,
87}
88
89/// These are more or less common to all currently supported badges.
90#[derive(Deserialize, Debug, Serialize, Default, Clone)]
91pub struct BuildBadge {
92    // This is the only one valid for the is-it-maintained variants
93    pub repository: String,
94    pub branch: Option<String>,
95    pub service: Option<String>,
96    // Only appveyor
97    pub id: Option<String>,
98    pub project_name: Option<String>,
99}
100
101/// Due to issues with `toml-rs`, this will fail to serialize if both Simple and Full variants exist.
102/// Specifically, issue #256 blocks this working properly.
103#[derive(Deserialize, Debug, Serialize, Clone)]
104#[serde(untagged)]
105pub enum Dependency {
106    Simple(String),
107    Full(DependencyFull),
108}
109
110#[derive(Deserialize, Debug, Serialize, Default, Clone)]
111#[serde(rename_all = "kebab-case")]
112pub struct DependencyFull {
113    pub git: Option<String>,
114    pub branch: Option<String>,
115    pub tag: Option<String>,
116    pub rev: Option<String>,
117    pub path: Option<PathBuf>,
118    pub version: Option<String>,
119    pub features: Option<Vec<String>>,
120    pub default_features: Option<bool>,
121    pub optional: Option<bool>,
122    pub package: Option<String>,
123}
124
125#[derive(Deserialize, Debug, Serialize, Default, Clone)]
126#[serde(transparent)]
127pub struct Target {
128    pub targets: BTreeMap<String, TargetDep>,
129}
130
131#[derive(Deserialize, Debug, Serialize, Default, Clone)]
132pub struct TargetDep {
133    pub dependencies: Option<DependencyT>,
134    pub dev_dependencies: Option<DependencyT>,
135    pub build_dependencies: Option<DependencyT>,
136}
137
138#[derive(Deserialize, Debug, Serialize, Default, Clone)]
139pub struct Profile {
140    pub dev: Option<ProfileVal>,
141    pub release: Option<ProfileVal>,
142    pub test: Option<ProfileVal>,
143    pub bench: Option<ProfileVal>,
144}
145
146#[derive(Deserialize, Debug, Serialize, Default, Clone)]
147#[serde(rename_all = "kebab-case")]
148pub struct ProfileVal {
149    pub opt_level: Option<i64>,
150    pub debug: Option<bool>,
151    pub rpath: Option<bool>,
152    pub lto: Option<bool>,
153    pub debug_assertions: Option<bool>,
154    pub codegen_units: Option<u64>,
155    pub panic: Option<String>,
156    pub incremental: Option<bool>,
157    pub overflow_checks: Option<bool>,
158}
159
160#[derive(Deserialize, Debug, Serialize, Default, Clone)]
161pub struct Features {
162    pub default: Option<Vec<String>>,
163    #[serde(flatten)]
164    pub features: BTreeMap<String, Vec<String>>,
165}
166
167#[derive(Deserialize, Debug, Serialize, Default, Clone)]
168#[serde(rename_all = "kebab-case")]
169pub struct Workspace {
170    pub members: Option<Vec<String>>,
171    pub default_members: Option<Vec<String>>,
172    pub exclude: Option<Vec<String>>,
173}
174
175/// All the sections here use the same stuff.
176/// <https://doc.rust-lang.org/cargo/reference/manifest.html#configuring-a-target>
177#[derive(Deserialize, Debug, Serialize, Default, Clone)]
178#[serde(rename_all = "kebab-case")]
179pub struct TargetConfig {
180    pub name: Option<String>,
181    pub path: Option<PathBuf>,
182    pub test: Option<bool>,
183    pub doctest: Option<bool>,
184    pub bench: Option<bool>,
185    pub plugin: Option<bool>,
186    pub proc_macro: Option<bool>,
187    pub harness: Option<bool>,
188    pub edition: Option<String>,
189    pub required_features: Option<Vec<String>>,
190    pub crate_type: Option<Vec<String>>,
191}
192
193#[derive(Deserialize, Debug, Serialize, Default, Clone)]
194#[serde(transparent, rename_all = "kebab-case")]
195pub struct Patches {
196    pub sources: BTreeMap<String, DependencyT>,
197}