• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
69 /// Info extracted from `CargoOut` for a crate.
70 ///
71 /// Note that there is a 1-to-many relationship between a Cargo.toml file and these `Crate`
72 /// objects. For example, a Cargo.toml file might have a bin, a lib, and various tests. Each of
73 /// those will be a separate `Crate`. All of them will have the same `package_name`.
74 #[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
75 pub struct Crate {
76     pub name: String,
77     pub package_name: String,
78     pub version: Option<String>,
79     pub types: Vec<CrateType>,
80     pub target: Option<String>, // --target
81     pub features: Vec<String>,  // --cfg feature=
82     pub cfgs: Vec<String>,      // non-feature --cfg
83     pub externs: Vec<Extern>,
84     pub codegens: Vec<String>, // -C
85     pub static_libs: Vec<String>,
86     pub shared_libs: Vec<String>,
87     pub edition: String,
88     pub package_dir: PathBuf, // canonicalized
89     pub main_src: PathBuf,    // relative to package_dir
90     pub license: Option<String>,
91     pub license_file: Option<String>,
92     /// Whether it is a test crate which doesn't actually contain any tests or benchmarks.
93     pub empty_test: bool,
94 }
95 
96 /// A dependency of a Rust crate.
97 #[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
98 pub struct Extern {
99     pub name: String,
100     pub lib_name: String,
101     pub raw_name: String,
102     pub extern_type: ExternType,
103 }
104 
105 #[derive(Copy, Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
106 pub enum ExternType {
107     Rust,
108     ProcMacro,
109 }
110