Skip to content

Commit 0128bce

Browse files
authored
feat: add name() to channel (#495)
1 parent b002f38 commit 0128bce

File tree

1 file changed

+23
-0
lines changed
  • crates/rattler_conda_types/src/channel

1 file changed

+23
-0
lines changed

crates/rattler_conda_types/src/channel/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,19 @@ impl Channel {
160160
}
161161
}
162162

163+
/// Returns the name of the channel
164+
pub fn name(&self) -> &str {
165+
match self.base_url().scheme() {
166+
// The name of the channel is only defined for http and https channels.
167+
// If the name is not defined we return the base url.
168+
"https" | "http" => self
169+
.name
170+
.as_deref()
171+
.unwrap_or_else(|| self.base_url.as_str()),
172+
_ => self.base_url.as_str(),
173+
}
174+
}
175+
163176
/// Returns the base Url of the channel. This does not include the platform part.
164177
pub fn base_url(&self) -> &Url {
165178
&self.base_url
@@ -442,6 +455,7 @@ mod tests {
442455
Url::from_str("https://conda.anaconda.org/conda-forge/").unwrap()
443456
);
444457
assert_eq!(channel.name.as_deref(), Some("conda-forge"));
458+
assert_eq!(channel.name(), "conda-forge");
445459
assert_eq!(channel.platforms, None);
446460

447461
assert_eq!(channel, Channel::from_name("conda-forge/", None, &config));
@@ -458,6 +472,7 @@ mod tests {
458472
Url::from_str("https://conda.anaconda.org/conda-forge/").unwrap()
459473
);
460474
assert_eq!(channel.name.as_deref(), Some("conda-forge"));
475+
assert_eq!(channel.name(), "conda-forge");
461476
assert_eq!(channel.platforms, None);
462477
assert_eq!(
463478
channel.base_url().to_string(),
@@ -471,6 +486,7 @@ mod tests {
471486

472487
let channel = Channel::from_str("file:///var/channels/conda-forge", &config).unwrap();
473488
assert_eq!(channel.name.as_deref(), Some("conda-forge"));
489+
assert_eq!(channel.name(), "file:///var/channels/conda-forge/");
474490
assert_eq!(
475491
channel.base_url,
476492
Url::from_str("file:///var/channels/conda-forge/").unwrap()
@@ -484,6 +500,12 @@ mod tests {
484500
let current_dir = std::env::current_dir().expect("no current dir?");
485501
let channel = Channel::from_str("./dir/does/not_exist", &config).unwrap();
486502
assert_eq!(channel.name.as_deref(), Some("./dir/does/not_exist"));
503+
assert_eq!(
504+
channel.name(),
505+
Url::from_directory_path(absolute_path(Path::new("./dir/does/not_exist")))
506+
.unwrap()
507+
.as_str()
508+
);
487509
assert_eq!(channel.platforms, None);
488510
assert_eq!(
489511
channel.base_url().to_file_path().unwrap(),
@@ -502,6 +524,7 @@ mod tests {
502524
);
503525
assert_eq!(channel.name, None);
504526
assert_eq!(channel.platforms, None);
527+
assert_eq!(channel.name(), "http://localhost:1234/");
505528

506529
let noarch_url = channel.platform_url(Platform::NoArch);
507530
assert_eq!(noarch_url.to_string(), "http://localhost:1234/noarch/");

0 commit comments

Comments
 (0)