use std::fs;
use std::path::Path;

use serde::Deserialize;

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
struct CargoBindingArtifact {
    bindings: Vec<CargoBindingRow>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub(super) struct CargoBindingRow {
    pub(super) descriptor_id: u32,
    pub(super) band: String,
    pub(super) cargo_name: String,
    pub(super) binding_index: usize,
}

pub(super) fn load_cargo_bindings(
    path: Option<&Path>,
) -> Result<Option<Vec<CargoBindingRow>>, Box<dyn std::error::Error>> {
    let Some(path) = path else {
        return Ok(None);
    };
    if !path.exists() {
        return Ok(None);
    }
    let artifact: CargoBindingArtifact = serde_json::from_str(&fs::read_to_string(path)?)?;
    Ok(Some(artifact.bindings))
}
