Skip to content

Commit 8d65906

Browse files
committed
Consolidate net configuration into a typed structure
Less need for `get_bool` and friends!
1 parent cbda532 commit 8d65906

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/cargo/sources/git/utils.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -707,10 +707,8 @@ pub fn fetch(
707707
// repositories instead of `libgit2`-the-library. This should make more
708708
// flavors of authentication possible while also still giving us all the
709709
// speed and portability of using `libgit2`.
710-
if let Some(val) = config.get_bool("net.git-fetch-with-cli")? {
711-
if val.val {
712-
return fetch_with_cli(repo, url, refspec, config);
713-
}
710+
if let Some(true) = config.net_config()?.git_fetch_with_cli {
711+
return fetch_with_cli(repo, url, refspec, config);
714712
}
715713

716714
debug!("doing a fetch for {}", url);

src/cargo/util/config/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ pub struct Config {
9494
/// Lock, if held, of the global package cache along with the number of
9595
/// acquisitions so far.
9696
package_cache_lock: RefCell<Option<(Option<FileLock>, usize)>>,
97-
/// HTTP configuration for Cargo
97+
/// Cached configuration parsed by Cargo
9898
http_config: LazyCell<CargoHttpConfig>,
99+
net_config: LazyCell<CargoNetConfig>,
99100
}
100101

101102
impl Config {
@@ -155,6 +156,7 @@ impl Config {
155156
updated_sources: LazyCell::new(),
156157
package_cache_lock: RefCell::new(None),
157158
http_config: LazyCell::new(),
159+
net_config: LazyCell::new(),
158160
}
159161
}
160162

@@ -638,8 +640,9 @@ impl Config {
638640
self.locked = locked;
639641
self.offline = offline
640642
|| self
641-
.get::<Option<bool>>("net.offline")
642-
.unwrap_or(None)
643+
.net_config()
644+
.ok()
645+
.and_then(|n| n.offline)
643646
.unwrap_or(false);
644647
self.target_dir = cli_target_dir;
645648
self.cli_flags.parse(unstable_flags)?;
@@ -929,6 +932,11 @@ impl Config {
929932
.try_borrow_with(|| Ok(self.get::<CargoHttpConfig>("http")?))
930933
}
931934

935+
pub fn net_config(&self) -> CargoResult<&CargoNetConfig> {
936+
self.net_config
937+
.try_borrow_with(|| Ok(self.get::<CargoNetConfig>("net")?))
938+
}
939+
932940
pub fn crates_io_source_id<F>(&self, f: F) -> CargoResult<SourceId>
933941
where
934942
F: FnMut() -> CargoResult<SourceId>,
@@ -1457,3 +1465,11 @@ pub enum SslVersionConfig {
14571465
Single(String),
14581466
Range(SslVersionConfigRange),
14591467
}
1468+
1469+
#[derive(Debug, Deserialize)]
1470+
pub struct CargoNetConfig {
1471+
pub retry: Option<u32>,
1472+
pub offline: Option<bool>,
1473+
#[serde(rename = "git-fetch-with-cli")]
1474+
pub git_fetch_with_cli: Option<bool>,
1475+
}

src/cargo/util/network.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl<'a> Retry<'a> {
1515
pub fn new(config: &'a Config) -> CargoResult<Retry<'a>> {
1616
Ok(Retry {
1717
config,
18-
remaining: config.get::<Option<u32>>("net.retry")?.unwrap_or(2),
18+
remaining: config.net_config()?.retry.unwrap_or(2),
1919
})
2020
}
2121

0 commit comments

Comments
 (0)