use tokio::{fs::File, io::AsyncReadExt};
use std::{io::Read, path::Path};
use serde::{Deserialize, Serialize};
#[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) -> AuthorityDefinition {
return toml::from_str(input).unwrap();
}
pub async fn parse_authority_toml_files(files: Vec<&Path>) -> Vec<AuthorityDefinition> {
let mut r = vec![];
for f in files {
let buf: &mut String = &mut String::new();
File::open(f).await.unwrap().read_to_string(buf).await.unwrap();
r.push(parse_authority_toml(buf.clone().as_str()));
}
return r;
}