use std::io::Read;
use serde::Deserialize;
use super::{AdapterError, ErrorReason};
pub const MAX_MANIFEST_BYTES: usize = 1024 * 1024;
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AdapterConfig {
pub version: u32,
#[serde(default)]
pub selectors: Vec<ClosedSelector>,
#[serde(default)]
pub payloads: Vec<PayloadSelector>,
#[serde(default)]
pub borrowed: Vec<BorrowedAdapter>,
#[serde(default)]
pub ports: Vec<CapabilityPort>,
}
impl AdapterConfig {
pub fn from_json(bytes: &[u8]) -> Result<Self, AdapterError> {
if bytes.len() > MAX_MANIFEST_BYTES {
return Err(ErrorReason::ManifestTooLarge.into());
}
let config: Self =
serde_json::from_slice(bytes).map_err(|_| ErrorReason::InvalidManifest)?;
if config.version != 1 {
return Err(ErrorReason::UnsupportedVersion.into());
}
Ok(config)
}
pub fn read_json(path: impl AsRef<std::path::Path>) -> Result<Self, AdapterError> {
let limit = MAX_MANIFEST_BYTES
.checked_add(1)
.and_then(|n| u64::try_from(n).ok())
.ok_or(ErrorReason::ManifestTooLarge)?;
let file = std::fs::File::open(path).map_err(|_| ErrorReason::ManifestIo)?;
let mut bytes = Vec::new();
file.take(limit)
.read_to_end(&mut bytes)
.map_err(|_| ErrorReason::ManifestIo)?;
Self::from_json(&bytes)
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ErrorMapping {
pub rust_type: String,
pub value: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct WireEnum {
pub source: String,
pub rust_type: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SelectorVariant {
pub source: String,
pub name: String,
#[serde(default)]
pub aliases: Vec<String>,
pub identifier: Option<i64>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Normalization {
pub source: String,
pub target: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LegacySelector {
pub wire: WireEnum,
pub normalizations: Vec<Normalization>,
#[serde(default)]
pub rejected: Vec<String>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ClosedSelector {
pub file: String,
pub name: String,
pub wire: WireEnum,
pub error: ErrorMapping,
#[serde(default)]
pub non_exhaustive: bool,
pub variants: Vec<SelectorVariant>,
#[serde(default)]
pub normalizations: Vec<Normalization>,
#[serde(default)]
pub rejected: Vec<String>,
#[serde(default)]
pub legacy: Vec<LegacySelector>,
pub identifier_error: Option<ErrorMapping>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FieldSource {
pub message: String,
pub path: Vec<String>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(from = "PayloadTypeJson")]
pub enum PayloadType {
Bytes,
Value {
source: String,
rust_type: String,
},
}
#[derive(Deserialize)]
#[serde(tag = "kind", deny_unknown_fields)]
enum PayloadTypeJson {
Bytes {},
Value { source: String, rust_type: String },
}
impl From<PayloadTypeJson> for PayloadType {
fn from(value: PayloadTypeJson) -> Self {
match value {
PayloadTypeJson::Bytes {} => Self::Bytes,
PayloadTypeJson::Value { source, rust_type } => Self::Value { source, rust_type },
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PayloadField {
pub name: String,
pub source: FieldSource,
pub representation: PayloadType,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(from = "PayloadJson")]
pub enum Payload {
Unit,
Tuple { field: PayloadField },
Struct { fields: Vec<PayloadField> },
Port { name: String },
}
#[derive(Deserialize)]
#[serde(tag = "kind", deny_unknown_fields)]
enum PayloadJson {
Unit {},
Tuple { field: PayloadField },
Struct { fields: Vec<PayloadField> },
Port { name: String },
}
impl From<PayloadJson> for Payload {
fn from(value: PayloadJson) -> Self {
match value {
PayloadJson::Unit {} => Self::Unit,
PayloadJson::Tuple { field } => Self::Tuple { field },
PayloadJson::Struct { fields } => Self::Struct { fields },
PayloadJson::Port { name } => Self::Port { name },
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PayloadVariant {
pub name: String,
pub cases: Vec<String>,
pub payload: Payload,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PayloadSelector {
pub file: String,
pub name: String,
pub source: String,
pub variants: Vec<PayloadVariant>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "kind", deny_unknown_fields)]
pub enum BorrowedSource {
Enum { source: String },
Oneof { message: String, name: String },
}
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct BorrowedAdapter {
pub file: String,
pub name: String,
pub source: BorrowedSource,
pub variants: Vec<PayloadVariant>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CapabilityPort {
pub name: String,
pub rust_trait: String,
}