bphelper-cli 0.7.3

CLI for creating and managing battery packs
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! Cargo.toml manipulation helpers and metadata location abstraction.
//!
//! This module handles reading and writing battery pack registrations,
//! feature storage, and dependency management in user Cargo.toml files.
//! No dependencies on other internal modules.

use anyhow::{Context, Result, bail};
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};

// ============================================================================
// Cargo.toml location helpers
// ============================================================================

/// Find the user's Cargo.toml in the given directory.
pub(crate) fn find_user_manifest(project_dir: &Path) -> Result<PathBuf> {
    let path = project_dir.join("Cargo.toml");
    if path.exists() {
        Ok(path)
    } else {
        bail!("No Cargo.toml found in {}", project_dir.display());
    }
}

/// Extract battery pack crate names from a parsed Cargo.toml.
///
/// Filters `[build-dependencies]` for entries ending in `-battery-pack` or equal to `"battery-pack"`.
// [impl manifest.register.location]
pub(crate) fn find_installed_bp_names(manifest_content: &str) -> Result<Vec<String>> {
    let raw: toml::Value =
        toml::from_str(manifest_content).context("Failed to parse Cargo.toml")?;

    let build_deps = raw
        .get("build-dependencies")
        .and_then(|bd| bd.as_table())
        .cloned()
        .unwrap_or_default();

    Ok(build_deps
        .keys()
        .filter(|k| k.ends_with("-battery-pack") || *k == "battery-pack")
        .cloned()
        .collect())
}

/// Find the workspace root Cargo.toml, if any.
/// Returns None if the crate is not in a workspace.
// [impl manifest.register.workspace-default]
// [impl manifest.register.both-levels]
pub(crate) fn find_workspace_manifest(crate_manifest: &Path) -> Result<Option<PathBuf>> {
    let parent = crate_manifest.parent().unwrap_or(Path::new("."));
    let parent = if parent.as_os_str().is_empty() {
        Path::new(".")
    } else {
        parent
    };
    let crate_dir = parent
        .canonicalize()
        .context("Failed to resolve crate directory")?;

    // Walk up from the crate directory looking for a workspace root
    let mut dir = crate_dir.clone();
    loop {
        let candidate = dir.join("Cargo.toml");
        if candidate.exists() && candidate != crate_dir.join("Cargo.toml") {
            let content = std::fs::read_to_string(&candidate)?;
            if content.contains("[workspace]") {
                return Ok(Some(candidate));
            }
        }
        if !dir.pop() {
            break;
        }
    }

    // Also check if the crate's own Cargo.toml has a [workspace] section
    // (single-crate workspace) — in that case we don't use workspace deps
    Ok(None)
}

// ============================================================================
// Dependency section helpers
// ============================================================================

/// Return the TOML section name for a dependency kind.
pub(crate) fn dep_kind_section(kind: bphelper_manifest::DepKind) -> &'static str {
    match kind {
        bphelper_manifest::DepKind::Normal => "dependencies",
        bphelper_manifest::DepKind::Dev => "dev-dependencies",
        bphelper_manifest::DepKind::Build => "build-dependencies",
    }
}

/// Write dependencies (with full version+features) to the correct sections by `dep_kind`.
///
/// When `if_missing` is true, only inserts crates that don't already exist in
/// the target section. Returns the number of crates actually written.
// [impl cli.add.dep-kind]
pub(crate) fn write_deps_by_kind(
    doc: &mut toml_edit::DocumentMut,
    crates: &BTreeMap<String, bphelper_manifest::CrateSpec>,
    if_missing: bool,
) -> usize {
    let mut written = 0;
    for (dep_name, dep_spec) in crates {
        let section = dep_kind_section(dep_spec.dep_kind);
        let table = doc[section].or_insert(toml_edit::Item::Table(toml_edit::Table::new()));
        if let Some(table) = table.as_table_mut()
            && (!if_missing || !table.contains_key(dep_name))
        {
            add_dep_to_table(table, dep_name, dep_spec);
            written += 1;
        }
    }
    written
}

/// Write workspace references (`{ workspace = true }`) to the correct
/// dependency sections based on each crate's `dep_kind`.
///
/// When `if_missing` is true, only inserts references for crates that don't
/// already exist in the target section. Returns the number of refs written.
// [impl cli.add.dep-kind]
pub(crate) fn write_workspace_refs_by_kind(
    doc: &mut toml_edit::DocumentMut,
    crates: &BTreeMap<String, bphelper_manifest::CrateSpec>,
    if_missing: bool,
) -> usize {
    let mut written = 0;
    for (dep_name, dep_spec) in crates {
        let section = dep_kind_section(dep_spec.dep_kind);
        let table = doc[section].or_insert(toml_edit::Item::Table(toml_edit::Table::new()));
        if let Some(table) = table.as_table_mut()
            && (!if_missing || !table.contains_key(dep_name))
        {
            let mut dep = toml_edit::InlineTable::new();
            dep.insert("workspace", toml_edit::Value::from(true));
            table.insert(
                dep_name,
                toml_edit::Item::Value(toml_edit::Value::InlineTable(dep)),
            );
            written += 1;
        }
    }
    written
}

/// Add a dependency to a toml_edit table (non-workspace mode).
// [impl manifest.deps.add]
// [impl manifest.deps.version-features]
// [impl manifest.toml.style]
// [impl cli.add.idempotent]
pub(crate) fn add_dep_to_table(
    table: &mut toml_edit::Table,
    name: &str,
    spec: &bphelper_manifest::CrateSpec,
) {
    if spec.features.is_empty() {
        table.insert(name, toml_edit::value(&spec.version));
    } else {
        let mut dep = toml_edit::InlineTable::new();
        dep.insert("version", toml_edit::Value::from(spec.version.as_str()));
        let mut features = toml_edit::Array::new();
        for feat in &spec.features {
            features.push(feat.as_str());
        }
        dep.insert("features", toml_edit::Value::Array(features));
        table.insert(
            name,
            toml_edit::Item::Value(toml_edit::Value::InlineTable(dep)),
        );
    }
}

/// Return true when `recommended` is strictly newer than `current` (semver).
///
/// Falls back to string equality when either side is not a valid semver
/// version, so non-standard version strings still get updated when they
/// differ.
pub(crate) fn should_upgrade_version(current: &str, recommended: &str) -> bool {
    match (
        semver::Version::parse(current)
            .or_else(|_| semver::Version::parse(&format!("{}.0", current)))
            .or_else(|_| semver::Version::parse(&format!("{}.0.0", current))),
        semver::Version::parse(recommended)
            .or_else(|_| semver::Version::parse(&format!("{}.0", recommended)))
            .or_else(|_| semver::Version::parse(&format!("{}.0.0", recommended))),
    ) {
        // [impl manifest.sync.version-bump]
        (Ok(cur), Ok(rec)) => rec > cur,
        // Non-parsable: fall back to "update if different"
        _ => current != recommended,
    }
}

/// Sync a dependency in-place: update version if behind, add missing features.
/// Returns true if changes were made.
// [impl manifest.deps.existing]
// [impl manifest.toml.style]
pub(crate) fn sync_dep_in_table(
    table: &mut toml_edit::Table,
    name: &str,
    spec: &bphelper_manifest::CrateSpec,
) -> bool {
    let Some(existing) = table.get_mut(name) else {
        // Not present — add it
        add_dep_to_table(table, name, spec);
        return true;
    };

    let mut changed = false;

    match existing {
        toml_edit::Item::Value(toml_edit::Value::String(version_str)) => {
            let current = version_str.value().to_string();
            // [impl manifest.sync.version-bump]
            if !spec.version.is_empty() && should_upgrade_version(&current, &spec.version) {
                *version_str = toml_edit::Formatted::new(spec.version.clone());
                changed = true;
            }
            // [impl manifest.sync.feature-add]
            if !spec.features.is_empty() {
                let keep_version = if !spec.version.is_empty()
                    && should_upgrade_version(&current, &spec.version)
                {
                    spec.version.clone()
                } else {
                    current.clone()
                };
                let patched = bphelper_manifest::CrateSpec {
                    version: keep_version,
                    features: spec.features.clone(),
                    dep_kind: spec.dep_kind,
                    optional: spec.optional,
                };
                add_dep_to_table(table, name, &patched);
                changed = true;
            }
        }
        toml_edit::Item::Value(toml_edit::Value::InlineTable(inline)) => {
            // [impl manifest.sync.version-bump]
            if let Some(toml_edit::Value::String(v)) = inline.get_mut("version")
                && !spec.version.is_empty()
                && should_upgrade_version(v.value(), &spec.version)
            {
                *v = toml_edit::Formatted::new(spec.version.clone());
                changed = true;
            }
            // [impl manifest.sync.feature-add]
            if !spec.features.is_empty() {
                let existing_features: Vec<String> = inline
                    .get("features")
                    .and_then(|f| f.as_array())
                    .map(|arr| {
                        arr.iter()
                            .filter_map(|v| v.as_str().map(String::from))
                            .collect()
                    })
                    .unwrap_or_default();

                let mut needs_update = false;
                let existing_set: BTreeSet<&str> =
                    existing_features.iter().map(|s| s.as_str()).collect();
                let mut all_features = existing_features.clone();
                for feat in &spec.features {
                    if !existing_set.contains(feat.as_str()) {
                        all_features.push(feat.clone());
                        needs_update = true;
                    }
                }

                if needs_update {
                    let mut arr = toml_edit::Array::new();
                    for f in &all_features {
                        arr.push(f.as_str());
                    }
                    inline.insert("features", toml_edit::Value::Array(arr));
                    changed = true;
                }
            }
        }
        toml_edit::Item::Table(tbl) => {
            // [impl manifest.sync.version-bump]
            if let Some(toml_edit::Item::Value(toml_edit::Value::String(v))) =
                tbl.get_mut("version")
                && !spec.version.is_empty()
                && should_upgrade_version(v.value(), &spec.version)
            {
                *v = toml_edit::Formatted::new(spec.version.clone());
                changed = true;
            }
            // [impl manifest.sync.feature-add]
            if !spec.features.is_empty() {
                let existing_features: Vec<String> = tbl
                    .get("features")
                    .and_then(|f| f.as_value())
                    .and_then(|v| v.as_array())
                    .map(|arr| {
                        arr.iter()
                            .filter_map(|v| v.as_str().map(String::from))
                            .collect()
                    })
                    .unwrap_or_default();

                let existing_set: BTreeSet<&str> =
                    existing_features.iter().map(|s| s.as_str()).collect();
                let mut all_features = existing_features.clone();
                let mut needs_update = false;
                for feat in &spec.features {
                    if !existing_set.contains(feat.as_str()) {
                        all_features.push(feat.clone());
                        needs_update = true;
                    }
                }

                if needs_update {
                    let mut arr = toml_edit::Array::new();
                    for f in &all_features {
                        arr.push(f.as_str());
                    }
                    tbl.insert(
                        "features",
                        toml_edit::Item::Value(toml_edit::Value::Array(arr)),
                    );
                    changed = true;
                }
            }
        }
        _ => {}
    }

    changed
}

// ============================================================================
// Feature reading / writing
// ============================================================================

/// Read active features from a parsed TOML value at a given path prefix.
///
/// `prefix` is `&["package", "metadata"]` for package metadata or
/// `&["workspace", "metadata"]` for workspace metadata.
// [impl manifest.features.storage]
pub(crate) fn read_features_at(
    raw: &toml::Value,
    prefix: &[&str],
    bp_name: &str,
) -> BTreeSet<String> {
    let mut node = Some(raw);
    for key in prefix {
        node = node.and_then(|n| n.get(key));
    }
    node.and_then(|m| m.get("battery-pack"))
        .and_then(|bp| bp.get(bp_name))
        .and_then(|entry| entry.get("features"))
        .and_then(|sets| sets.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|v| v.as_str().map(String::from))
                .collect()
        })
        .unwrap_or_else(|| BTreeSet::from(["default".to_string()]))
}

/// Read active features for a battery pack from user's package metadata.
pub(crate) fn read_active_features(manifest_content: &str, bp_name: &str) -> BTreeSet<String> {
    let raw: toml::Value = match toml::from_str(manifest_content) {
        Ok(v) => v,
        Err(_) => return BTreeSet::from(["default".to_string()]),
    };
    read_features_at(&raw, &["package", "metadata"], bp_name)
}

/// Read active features from `workspace.metadata.battery-pack[bp_name].features`.
pub(crate) fn read_active_features_ws(ws_content: &str, bp_name: &str) -> BTreeSet<String> {
    let raw: toml::Value = match toml::from_str(ws_content) {
        Ok(v) => v,
        Err(_) => return BTreeSet::from(["default".to_string()]),
    };
    read_features_at(&raw, &["workspace", "metadata"], bp_name)
}

// ============================================================================
// Metadata location abstraction
// ============================================================================

/// Where battery-pack metadata (registrations, active features) is stored.
///
/// `add_battery_pack` writes to either `package.metadata` or `workspace.metadata`
/// depending on the `--target` flag. All other commands (sync, enable, load) must
/// read from the same location, so they use `resolve_metadata_location` to detect
/// where metadata currently lives.
#[derive(Debug, Clone)]
pub(crate) enum MetadataLocation {
    /// `package.metadata.battery-pack` in the user manifest.
    Package,
    /// `workspace.metadata.battery-pack` in the workspace manifest.
    Workspace { ws_manifest_path: PathBuf },
}

/// Determine where battery-pack metadata lives for this project.
///
/// If a workspace manifest exists AND already contains
/// `workspace.metadata.battery-pack`, returns `Workspace`.
/// Otherwise returns `Package`.
pub(crate) fn resolve_metadata_location(user_manifest_path: &Path) -> Result<MetadataLocation> {
    if let Some(ws_path) = find_workspace_manifest(user_manifest_path)? {
        let ws_content =
            std::fs::read_to_string(&ws_path).context("Failed to read workspace Cargo.toml")?;
        let raw: toml::Value =
            toml::from_str(&ws_content).context("Failed to parse workspace Cargo.toml")?;
        if raw
            .get("workspace")
            .and_then(|w| w.get("metadata"))
            .and_then(|m| m.get("battery-pack"))
            .is_some()
        {
            return Ok(MetadataLocation::Workspace {
                ws_manifest_path: ws_path,
            });
        }
    }
    Ok(MetadataLocation::Package)
}

/// Read active features for a battery pack, respecting metadata location.
pub(crate) fn read_active_features_from(
    location: &MetadataLocation,
    user_manifest_content: &str,
    bp_name: &str,
) -> BTreeSet<String> {
    match location {
        MetadataLocation::Package => read_active_features(user_manifest_content, bp_name),
        MetadataLocation::Workspace { ws_manifest_path } => {
            let ws_content = match std::fs::read_to_string(ws_manifest_path) {
                Ok(c) => c,
                Err(_) => return BTreeSet::from(["default".to_string()]),
            };
            read_active_features_ws(&ws_content, bp_name)
        }
    }
}

/// Write a features array into a `toml_edit::DocumentMut` at a given path prefix.
///
/// `path_prefix` is `["package", "metadata"]` for package metadata or
/// `["workspace", "metadata"]` for workspace metadata.
pub(crate) fn write_bp_features_to_doc(
    doc: &mut toml_edit::DocumentMut,
    path_prefix: &[&str],
    bp_name: &str,
    active_features: &BTreeSet<String>,
) {
    let mut features_array = toml_edit::Array::new();
    for feature in active_features {
        features_array.push(feature.as_str());
    }

    doc[path_prefix[0]].or_insert(toml_edit::Item::Table(toml_edit::Table::new()));
    doc[path_prefix[0]][path_prefix[1]].or_insert(toml_edit::Item::Table(toml_edit::Table::new()));
    doc[path_prefix[0]][path_prefix[1]]["battery-pack"]
        .or_insert(toml_edit::Item::Table(toml_edit::Table::new()));

    // Write as inline table: `bp_name = { features = [...] }`
    let mut inline = toml_edit::InlineTable::new();
    inline.insert("features", toml_edit::Value::Array(features_array));
    let bp_table = doc[path_prefix[0]][path_prefix[1]]["battery-pack"]
        .as_table_mut()
        .expect("battery-pack table must exist");
    bp_table.insert(
        bp_name,
        toml_edit::Item::Value(toml_edit::Value::InlineTable(inline)),
    );
}

/// Resolve the manifest path for a battery pack using `cargo metadata`.
///
/// Works for any dependency source: path deps, registry deps, git deps.
/// The battery pack must already be in [build-dependencies].
pub(crate) fn resolve_battery_pack_manifest(bp_name: &str) -> Result<PathBuf> {
    let metadata = cargo_metadata::MetadataCommand::new()
        .exec()
        .context("Failed to run `cargo metadata`")?;

    let package = metadata
        .packages
        .iter()
        .find(|p| p.name == bp_name)
        .ok_or_else(|| {
            anyhow::anyhow!(
                "Battery pack '{}' not found in dependency graph. Is it in [build-dependencies]?",
                bp_name
            )
        })?;

    Ok(package.manifest_path.clone().into())
}

// ============================================================================
// Version collection for status
// ============================================================================

#[cfg(test)]
mod tests;