use serde::{Deserialize, Serialize};
use std::{io::Read, path::Path};
use tokio::{fs::File, io::AsyncReadExt};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthorityDefinition {
pub schema: u32,
pub metadata: AuthorityMetadata,
#[serde(rename = "registry")]
pub registries: Vec<RegistryDefinition>,
pub(crate) id: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthorityMetadata {
pub id: String,
pub name: String,
pub owners: Vec<String>,
pub homepage: String,
pub description: String,
pub signing_keys: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegistryDefinition {
#[serde(rename = "type")]
pub registry_type: String,
#[serde(rename = "ref")]
pub reference: String,
pub namespace: String,
#[serde(default)]
pub history: Vec<RegistryHistoryDefinition>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegistryHistoryDefinition {
#[serde(rename = "type")]
pub registry_type: String,
#[serde(rename = "ref")]
pub reference: String,
pub deprecated: bool,
pub until: String,
}
fn parse_authority_toml(input: &str) -> anyhow::Result<AuthorityDefinition> {
toml::from_str::<AuthorityDefinition>(input).map_err(Into::into)
}
pub async fn parse_authority_toml_files(
files: Vec<&Path>,
) -> anyhow::Result<Vec<AuthorityDefinition>> {
let mut r: Vec<AuthorityDefinition> = vec![];
for f in files {
let buf: &mut String = &mut String::new();
File::open(f).await?.read_to_string(buf).await?;
r.push(parse_authority_toml(buf.as_str())?);
}
Ok(r)
}