feat(content): configure discovery provider scope

This commit is contained in:
objz committed 2026-08-04 19:45:53 +02:00
1 parent 94b71fdc4e
commit 52f074ce46
11 files changed
+124 -67

No files matched your search

+22 -11
View File
@@ -25,7 +25,7 @@ pub struct Content {
#[serde(default = "default_provider")]
pub preferred_provider: String,
#[serde(default)]
pub curseforge_api_key: Option<String>,
pub preferred_provider_only: bool,
#[serde(default = "default_unmatched_retry_hours")]
pub unmatched_retry_hours: u64,
#[serde(default = "default_max_fingerprint_size_mib")]
@@ -53,7 +53,7 @@ impl Default for Content {
Self {
ask_on_provider_conflict: true,
preferred_provider: default_provider(),
curseforge_api_key: None,
preferred_provider_only: false,
unmatched_retry_hours: default_unmatched_retry_hours(),
max_fingerprint_size_mib: default_max_fingerprint_size_mib(),
}
@@ -61,22 +61,33 @@ impl Default for Content {
}
impl Content {
pub fn curseforge_api_key(&self) -> Option<&str> {
self.curseforge_api_key
.as_deref()
.map(str::trim)
.filter(|key| !key.is_empty())
pub fn preferred_provider(&self) -> &str {
self.preferred_provider_with_curseforge(crate::net::curseforge::api_key().is_some())
}
pub fn preferred_provider(&self) -> &str {
if self.preferred_provider.eq_ignore_ascii_case("curseforge")
&& self.curseforge_api_key().is_some()
{
pub fn discovery_provider_enabled(&self, provider: &str) -> bool {
self.discovery_provider_enabled_with_curseforge(
provider,
crate::net::curseforge::api_key().is_some(),
)
}
fn preferred_provider_with_curseforge(&self, curseforge_available: bool) -> &'static str {
if self.preferred_provider.eq_ignore_ascii_case("curseforge") && curseforge_available {
"curseforge"
} else {
"modrinth"
}
}
fn discovery_provider_enabled_with_curseforge(
&self,
provider: &str,
curseforge_available: bool,
) -> bool {
!self.preferred_provider_only
|| provider == self.preferred_provider_with_curseforge(curseforge_available)
}
}
#[derive(Debug, Deserialize)]
+16 -14
View File
@@ -28,21 +28,23 @@ fn effective_java_path_some_when_set() {
}
#[test]
fn blank_curseforge_key_keeps_provider_disabled() {
let content: Content = toml::from_str("curseforge_api_key = \" \"").unwrap();
assert_eq!(content.curseforge_api_key(), None);
assert_eq!(content.preferred_provider(), "modrinth");
}
#[test]
fn curseforge_preference_requires_its_api_key() {
let without_key: Content = toml::from_str("preferred_provider = \"curseforge\"").unwrap();
assert_eq!(without_key.preferred_provider(), "modrinth");
let configured: Content =
toml::from_str("preferred_provider = \"curseforge\"\ncurseforge_api_key = \"secret\"")
fn content_provider_settings_are_normalized() {
let content: Content =
toml::from_str("preferred_provider = \"curseforge\"\npreferred_provider_only = true")
.unwrap();
assert_eq!(configured.preferred_provider(), "curseforge");
assert_eq!(
content.preferred_provider_with_curseforge(false),
"modrinth"
);
assert_eq!(
content.preferred_provider_with_curseforge(true),
"curseforge"
);
assert!(content.preferred_provider_only);
assert!(content.discovery_provider_enabled_with_curseforge("modrinth", false));
assert!(!content.discovery_provider_enabled_with_curseforge("curseforge", false));
assert!(!content.discovery_provider_enabled_with_curseforge("modrinth", true));
assert!(content.discovery_provider_enabled_with_curseforge("curseforge", true));
}
#[test]