cargo_config2/
lib.rs

1// SPDX-License-Identifier: Apache-2.0 OR MIT
2
3/*!
4<!-- Note: Document from sync-markdown-to-rustdoc:start through sync-markdown-to-rustdoc:end
5     is synchronized from README.md. Any changes to that range are not preserved. -->
6<!-- tidy:sync-markdown-to-rustdoc:start -->
7
8Load and resolve [Cargo configuration](https://doc.rust-lang.org/nightly/cargo/reference/config.html).
9
10This library is intended to accurately emulate the actual behavior of Cargo configuration, for example, this supports the following behaviors:
11
12- [Hierarchical structure and merge](https://doc.rust-lang.org/nightly/cargo/reference/config.html#hierarchical-structure)
13- [Environment variables](https://doc.rust-lang.org/nightly/cargo/reference/config.html#environment-variables) and [relative paths](https://doc.rust-lang.org/nightly/cargo/reference/config.html#config-relative-paths) resolution.
14- `target.<triple>` and `target.<cfg>` resolution.
15
16Supported tables and fields are mainly based on [cargo-llvm-cov](https://github.com/taiki-e/cargo-llvm-cov)'s use cases, but feel free to submit an issue if you see something missing in your use case.
17
18## Usage
19
20Add this to your `Cargo.toml`:
21
22```toml
23[dependencies]
24cargo-config2 = "0.1"
25```
26
27`cargo-config2` is usually runnable with Cargo versions older than the Rust version required for build. (e.g., a cargo subcommand using `cargo-config2` could work with older versions such as `cargo +1.59 <subcommand>`.)
28
29## Examples
30
31```
32// Read config files hierarchically from the current directory, merge them,
33// apply environment variables, and resolve relative paths.
34let config = cargo_config2::Config::load().unwrap();
35let target = "x86_64-unknown-linux-gnu";
36// Resolve target-specific configuration (`target.<triple>` and `target.<cfg>`),
37// and returns the resolved rustflags for `target`.
38let rustflags = config.rustflags(target).unwrap();
39println!("{rustflags:?}");
40```
41
42See also the [`get` example](https://github.com/taiki-e/cargo-config2/blob/HEAD/examples/get.rs) that partial re-implementation of `cargo config get` using cargo-config2.
43
44<!-- tidy:sync-markdown-to-rustdoc:end -->
45*/
46
47#![doc(test(
48    no_crate_inject,
49    attr(
50        deny(warnings, rust_2018_idioms, single_use_lifetimes),
51        allow(dead_code, unused_variables)
52    )
53))]
54// Windows needs unsafe code until MSRV become Rust 1.85: https://github.com/rust-lang/rust/pull/132515
55#![cfg_attr(not(windows), forbid(unsafe_code))]
56#![warn(
57    // Lints that may help when writing public library.
58    missing_debug_implementations,
59    // missing_docs,
60    clippy::alloc_instead_of_core,
61    clippy::exhaustive_enums,
62    clippy::exhaustive_structs,
63    clippy::impl_trait_in_params,
64    // clippy::missing_inline_in_public_items,
65    // clippy::std_instead_of_alloc,
66    clippy::std_instead_of_core,
67)]
68#![allow(clippy::must_use_candidate)]
69
70// Refs:
71// - https://doc.rust-lang.org/nightly/cargo/reference/config.html
72
73#[cfg(test)]
74#[path = "gen/tests/assert_impl.rs"]
75mod assert_impl;
76#[path = "gen/is_none.rs"]
77mod is_none_impl;
78#[cfg(test)]
79#[cfg(not(windows))] // TODO: size is large for some reason.
80#[path = "gen/tests/track_size.rs"]
81mod track_size;
82
83#[macro_use]
84mod error;
85
86#[macro_use]
87mod process;
88
89mod cfg_expr;
90pub mod de;
91mod easy;
92mod env;
93mod merge;
94mod resolve;
95mod value;
96mod walk;
97
98#[doc(no_inline)]
99pub use self::de::{Color, Frequency, RegistriesProtocol, VersionControlSoftware, When};
100pub use self::{
101    easy::{
102        BuildConfig, CargoNewConfig, Config, CredentialProvider, DocConfig, EnvConfigValue, Flags,
103        FutureIncompatReportConfig, GlobalCredentialProviders, HttpConfig, NetConfig, PathAndArgs,
104        RegistriesConfigValue, RegistryConfig, SourceConfigValue, StringList, TargetConfig,
105        TermConfig, TermProgressConfig,
106    },
107    error::Error,
108    resolve::{CargoVersion, ResolveOptions, RustcVersion, TargetTriple, TargetTripleRef},
109    walk::{Walk, cargo_home_with_cwd, home_dir, rustup_home_with_cwd},
110};