1 // Copyright (C) 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! Types and functions for parsing the output of cargo.
16 
17 pub mod cargo_out;
18 pub mod metadata;
19 
20 use serde::{Deserialize, Serialize};
21 use std::path::PathBuf;
22 
23 /// Combined representation of --crate-type and --test flags.
24 #[derive(Copy, Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
25 #[serde[rename_all = "lowercase"]]
26 pub enum CrateType {
27     // --crate-type types
28     Bin,
29     Lib,
30     RLib,
31     DyLib,
32     CDyLib,
33     StaticLib,
34     #[serde(rename = "proc-macro")]
35     ProcMacro,
36     // --test
37     Test,
38     // "--cfg test" without --test. (Assume it is a test with the harness disabled.
39     TestNoHarness,
40 }
41 
42 impl CrateType {
from_str(s: &str) -> CrateType43     fn from_str(s: &str) -> CrateType {
44         match s {
45             "bin" => CrateType::Bin,
46             "lib" => CrateType::Lib,
47             "rlib" => CrateType::RLib,
48             "dylib" => CrateType::DyLib,
49             "cdylib" => CrateType::CDyLib,
50             "staticlib" => CrateType::StaticLib,
51             "proc-macro" => CrateType::ProcMacro,
52             _ => panic!("unexpected --crate-type: {}", s),
53         }
54     }
55 }
56 
57 impl CrateType {
58     /// Returns whether the crate type is a kind of library.
is_library(self) -> bool59     pub fn is_library(self) -> bool {
60         matches!(self, Self::Lib | Self::RLib | Self::DyLib | Self::CDyLib | Self::StaticLib)
61     }
62 
63     /// Returns whether the crate type is a kind of test.
is_test(self) -> bool64     pub fn is_test(self) -> bool {
65         matches!(self, Self::Test | Self::TestNoHarness)
66     }
67 
68     /// Returns whether the crate type is a kind of C ABI library.
is_c_library(self) -> bool69     pub fn is_c_library(self) -> bool {
70         matches!(self, Self::CDyLib | Self::StaticLib)
71     }
72 }
73 
74 /// Info extracted from `CargoOut` for a crate.
75 ///
76 /// Note that there is a 1-to-many relationship between a Cargo.toml file and these `Crate`
77 /// objects. For example, a Cargo.toml file might have a bin, a lib, and various tests. Each of
78 /// those will be a separate `Crate`. All of them will have the same `package_name`.
79 #[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
80 pub struct Crate {
81     pub name: String,
82     pub package_name: String,
83     pub version: Option<String>,
84     pub types: Vec<CrateType>,
85     pub target: Option<String>, // --target
86     pub features: Vec<String>,  // --cfg feature=
87     pub cfgs: Vec<String>,      // non-feature --cfg
88     pub externs: Vec<Extern>,
89     pub codegens: Vec<String>, // -C
90     pub cap_lints: String,
91     pub static_libs: Vec<String>,
92     pub shared_libs: Vec<String>,
93     pub edition: String,
94     pub package_dir: PathBuf, // canonicalized
95     pub main_src: PathBuf,    // relative to package_dir
96     /// Whether it is a test crate which doesn't actually contain any tests or benchmarks.
97     pub empty_test: bool,
98 }
99 
100 /// A dependency of a Rust crate.
101 #[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
102 pub struct Extern {
103     pub name: String,
104     pub lib_name: String,
105     pub extern_type: ExternType,
106 }
107 
108 #[derive(Copy, Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
109 pub enum ExternType {
110     Rust,
111     ProcMacro,
112 }
113