rattler_config 0.4.0

A crate to configure rattler and derived tools.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Per-channel index configuration consumed by `rattler-index`.
//!
//! Mirrors the layout of [`crate::config::repodata_config::RepodataConfig`]:
//! a flat default block plus a per-channel map. Per-channel keys are full
//! channel URLs (`s3://my-bucket/my-channel`), URL prefixes
//! (`s3://my-bucket`), or absolute filesystem paths (`/srv/conda/internal`).
//! Longest matching prefix wins; values from less-specific entries are
//! layered as fallbacks.
//!
//! ```toml
//! [index-config]
//! write-zst = true
//! write-shards = true
//!
//! [index-config."s3://my-bucket"]
//! base-url = "../packages/"
//!
//! [index-config."s3://my-bucket/staging"]
//! write-shards = false
//! package-revision-assignment = "latest"
//!
//! [index-config."s3://my-bucket/staging".channel-relations]
//! base = "../conda-forge"
//!
//! [index-config."/srv/conda/internal"]
//! base-url = "../packages/"
//! ```
use std::{collections::HashMap, str::FromStr};

use rattler_conda_types::{ChannelRelations, RepodataRevision, RepodataRevisionInfo};
use serde::{Deserialize, Deserializer, Serialize, de::Error as DeError};

use crate::config::{Config, MergeError, ValidationError};
#[cfg(feature = "edit")]
use crate::edit::ConfigEditError;

/// How packages are assigned to repodata revisions while indexing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum PackageRevisionAssignment {
    /// Assign each package to the revision required by its `info/index.json`.
    /// Packages without an explicit `repodata_revision` are assigned to the
    /// oldest known revision that can represent their fields.
    #[default]
    FromIndexJson,
    /// Assign every package to the newest revision configured for the index.
    /// If no revisions are configured, packages are assigned to `Legacy`.
    Latest,
}

impl PackageRevisionAssignment {
    /// Pick the effective revision for a package given the latest configured
    /// revision for the channel.
    pub fn assign(
        self,
        package_revision: RepodataRevision,
        latest_revision: RepodataRevision,
    ) -> RepodataRevision {
        match self {
            Self::FromIndexJson => package_revision,
            Self::Latest => latest_revision,
        }
    }
}

/// Index options that apply to a single channel.
///
/// Every field is optional so that `IndexConfig` can layer multiple entries
/// without losing earlier values.
#[derive(Clone, Default, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub struct IndexChannelConfig {
    /// Whether to write `repodata.json.zst`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub write_zst: Option<bool>,

    /// Whether to write `repodata_shards.msgpack.zst` and shard files.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub write_shards: Option<bool>,

    /// Repodata revisions to advertise in generated repodata.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_optional_repodata_revisions"
    )]
    pub repodata_revisions: Option<Vec<RepodataRevisionInfo>>,

    /// How packages are assigned to repodata revisions.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub package_revision_assignment: Option<PackageRevisionAssignment>,

    /// `info.base_url` value written to generated repodata.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub base_url: Option<String>,

    /// `info.channel_relations` value written to generated repodata.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub channel_relations: Option<ChannelRelations>,
}

impl IndexChannelConfig {
    /// Returns true if no fields are set.
    pub fn is_empty(&self) -> bool {
        self.write_zst.is_none()
            && self.write_shards.is_none()
            && self.repodata_revisions.is_none()
            && self.package_revision_assignment.is_none()
            && self.base_url.is_none()
            && self.channel_relations.is_none()
    }

    /// Layer `other` on top of `self`. Fields set in `other` win.
    pub fn merge(&self, other: Self) -> Self {
        Self {
            write_zst: other.write_zst.or(self.write_zst),
            write_shards: other.write_shards.or(self.write_shards),
            repodata_revisions: other
                .repodata_revisions
                .or_else(|| self.repodata_revisions.clone()),
            package_revision_assignment: other
                .package_revision_assignment
                .or(self.package_revision_assignment),
            base_url: other.base_url.or_else(|| self.base_url.clone()),
            channel_relations: other
                .channel_relations
                .or_else(|| self.channel_relations.clone()),
        }
    }
}

/// Index configuration with default options and per-channel overrides.
#[derive(Clone, Default, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub struct IndexConfig {
    /// Default options used when no `per_channel` entry matches.
    #[serde(flatten)]
    pub default: IndexChannelConfig,

    /// Per-channel overrides keyed by channel URL or absolute filesystem
    /// path. Longest matching prefix wins; less-specific entries are layered
    /// as fallbacks. Examples: `s3://my-bucket`,
    /// `s3://my-bucket/staging`, `/srv/conda/internal`.
    #[serde(flatten)]
    pub per_channel: HashMap<String, IndexChannelConfig>,
}

impl IndexConfig {
    /// Returns true if neither defaults nor per-channel entries are set.
    pub fn is_empty(&self) -> bool {
        self.default.is_empty() && self.per_channel.is_empty()
    }

    /// Resolve the effective options for a channel target.
    ///
    /// `target` is the canonical channel reference: a URL like
    /// `s3://my-bucket/staging` or an absolute filesystem path like
    /// `/srv/conda/internal`. All keys whose normalised form is a
    /// component-boundary prefix of `target` are layered onto `default`,
    /// shortest first, so the most specific match wins.
    pub fn resolve(&self, target: &str) -> IndexChannelConfig {
        let target = target.trim_end_matches('/');

        let mut matches: Vec<(&str, &IndexChannelConfig)> = self
            .per_channel
            .iter()
            .filter_map(|(key, cfg)| {
                let key_norm = key.trim_end_matches('/');
                is_prefix_match(key_norm, target).then_some((key_norm, cfg))
            })
            .collect();
        matches.sort_by_key(|(key, _)| key.len());

        let mut effective = self.default.clone();
        for (_, cfg) in matches {
            effective = effective.merge(cfg.clone());
        }
        effective
    }
}

/// True iff `key` equals `target` or is followed by a `/` separator in
/// `target`. Both inputs are expected to be slash-trimmed.
fn is_prefix_match(key: &str, target: &str) -> bool {
    if key == target {
        return true;
    }
    let Some(rest) = target.strip_prefix(key) else {
        return false;
    };
    rest.starts_with('/')
}

impl Config for IndexConfig {
    fn get_extension_name(&self) -> String {
        "index-config".to_string()
    }

    /// Merge another `IndexConfig` on top of this one.
    fn merge_config(self, other: &Self) -> Result<Self, MergeError> {
        let mut merged = self.clone();
        merged.default = merged.default.merge(other.default.clone());
        for (key, cfg) in &other.per_channel {
            merged
                .per_channel
                .entry(key.clone())
                .and_modify(|existing| *existing = existing.merge(cfg.clone()))
                .or_insert_with(|| cfg.clone());
        }
        Ok(merged)
    }

    fn validate(&self) -> Result<(), ValidationError> {
        for (key, cfg) in &self.per_channel {
            validate_channel_relations(key, cfg)?;
        }
        validate_channel_relations("default", &self.default)?;
        Ok(())
    }

    fn keys(&self) -> Vec<String> {
        vec!["default".to_string(), "per-channel".to_string()]
    }

    #[cfg(feature = "edit")]
    fn set(&mut self, key: &str, value: Option<String>) -> Result<(), ConfigEditError> {
        if key == "index-config" {
            *self = value
                .map(|v| {
                    serde_json::de::from_str(&v).map_err(|e| ConfigEditError::JsonParseError {
                        key: key.to_string(),
                        source: e,
                    })
                })
                .transpose()?
                .unwrap_or_default();
            return Ok(());
        }
        Err(ConfigEditError::UnknownKey {
            key: key.to_string(),
            supported_keys: self.keys().join(", "),
        })
    }
}

fn validate_channel_relations(
    label: &str,
    cfg: &IndexChannelConfig,
) -> Result<(), ValidationError> {
    let Some(relations) = &cfg.channel_relations else {
        return Ok(());
    };
    if relations.base.is_some()
        && relations.overrides.is_some()
        && relations.base == relations.overrides
    {
        return Err(ValidationError::InvalidValue(
            format!("index-config.{label}.channel-relations"),
            "base and overrides must not be the same channel".to_string(),
        ));
    }
    Ok(())
}

fn deserialize_optional_repodata_revisions<'de, D>(
    deserializer: D,
) -> Result<Option<Vec<RepodataRevisionInfo>>, D::Error>
where
    D: Deserializer<'de>,
{
    let revisions: Option<Vec<String>> = Option::deserialize(deserializer)?;
    revisions
        .map(|revs| {
            revs.into_iter()
                .map(|s| {
                    RepodataRevision::from_str(&s)
                        .map(|revision| RepodataRevisionInfo {
                            revision,
                            n_packages: None,
                            oldest: None,
                            newest: None,
                        })
                        .map_err(D::Error::custom)
                })
                .collect::<Result<Vec<_>, _>>()
        })
        .transpose()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn parse(toml: &str) -> IndexConfig {
        toml::from_str::<IndexConfig>(toml).unwrap()
    }

    #[test]
    fn parses_default_block() {
        let cfg = parse(
            r#"
write-zst = true
write-shards = false
repodata-revisions = ["v3"]
package-revision-assignment = "latest"
base-url = "../packages/"

[channel-relations]
base = "../conda-forge"
"#,
        );
        assert_eq!(cfg.default.write_zst, Some(true));
        assert_eq!(cfg.default.write_shards, Some(false));
        assert_eq!(cfg.default.repodata_revisions.as_ref().unwrap().len(), 1);
        assert_eq!(
            cfg.default.repodata_revisions.as_ref().unwrap()[0].revision,
            RepodataRevision::V3
        );
        assert_eq!(
            cfg.default.package_revision_assignment,
            Some(PackageRevisionAssignment::Latest)
        );
        assert_eq!(cfg.default.base_url.as_deref(), Some("../packages/"));
        assert_eq!(
            cfg.default
                .channel_relations
                .as_ref()
                .unwrap()
                .base
                .as_deref(),
            Some("../conda-forge")
        );
        assert!(cfg.per_channel.is_empty());
    }

    #[test]
    fn parses_per_channel_entries() {
        let cfg = parse(
            r#"
write-zst = true

["s3://my-bucket"]
base-url = "../packages/"

["s3://my-bucket/staging"]
write-shards = false
package-revision-assignment = "latest"

["/srv/conda/internal"]
base-url = "../local-packages/"
"#,
        );
        assert_eq!(cfg.default.write_zst, Some(true));
        assert_eq!(cfg.per_channel.len(), 3);
        assert_eq!(
            cfg.per_channel["s3://my-bucket"].base_url.as_deref(),
            Some("../packages/")
        );
        assert_eq!(
            cfg.per_channel["s3://my-bucket/staging"].write_shards,
            Some(false)
        );
        assert_eq!(
            cfg.per_channel["/srv/conda/internal"].base_url.as_deref(),
            Some("../local-packages/")
        );
    }

    #[test]
    fn resolves_longest_prefix_match() {
        let cfg = parse(
            r#"
write-zst = true
write-shards = true

["s3://my-bucket"]
base-url = "../packages/"
package-revision-assignment = "from-index-json"

["s3://my-bucket/staging"]
package-revision-assignment = "latest"
write-shards = false
"#,
        );
        let resolved = cfg.resolve("s3://my-bucket/staging");
        assert_eq!(resolved.write_zst, Some(true));
        assert_eq!(resolved.write_shards, Some(false)); // staging wins
        assert_eq!(resolved.base_url.as_deref(), Some("../packages/")); // host fallback
        assert_eq!(
            resolved.package_revision_assignment,
            Some(PackageRevisionAssignment::Latest) // staging wins
        );
    }

    #[test]
    fn resolve_falls_back_to_default_for_unknown_target() {
        let cfg = parse(
            r#"
write-zst = false

["s3://my-bucket"]
base-url = "../packages/"
"#,
        );
        let resolved = cfg.resolve("s3://other-bucket/channel");
        assert_eq!(resolved.write_zst, Some(false));
        assert!(resolved.base_url.is_none());
    }

    #[test]
    fn resolve_does_not_match_partial_component() {
        let cfg = parse(
            r#"
["s3://my-bucket"]
base-url = "../packages/"
"#,
        );
        let resolved = cfg.resolve("s3://my-bucket-other/channel");
        assert!(resolved.base_url.is_none());
    }

    #[test]
    fn rejects_numeric_repodata_revisions() {
        let err = toml::from_str::<IndexConfig>("repodata-revisions = [3]\n").unwrap_err();
        assert!(
            err.to_string().contains("invalid type"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn validates_matching_base_and_overrides() {
        let cfg = parse(
            r#"
[channel-relations]
base = "../same"
overrides = "../same"
"#,
        );
        let err = cfg.validate().unwrap_err();
        assert!(err.to_string().contains("must not be the same channel"));
    }

    #[test]
    fn merge_layers_per_channel_entries() {
        let base = parse(
            r#"
write-zst = true

["s3://my-bucket"]
base-url = "../packages/"
"#,
        );
        let other = parse(
            r#"
write-shards = false

["s3://my-bucket"]
package-revision-assignment = "latest"
"#,
        );
        let merged = base.merge_config(&other).unwrap();
        assert_eq!(merged.default.write_zst, Some(true));
        assert_eq!(merged.default.write_shards, Some(false));
        let entry = &merged.per_channel["s3://my-bucket"];
        assert_eq!(entry.base_url.as_deref(), Some("../packages/"));
        assert_eq!(
            entry.package_revision_assignment,
            Some(PackageRevisionAssignment::Latest)
        );
    }
}