use serde::{Deserialize, Serialize};
use crate::options::Options;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PortEntry {
#[serde(rename = "type")]
pub kind: String,
#[serde(flatten)]
pub options: Options,
}
impl PortEntry {
pub fn new(kind: impl Into<String>) -> Self {
Self {
kind: kind.into(),
options: Options::empty(),
}
}
pub fn with_options(kind: impl Into<String>, options: Options) -> Self {
Self {
kind: kind.into(),
options,
}
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
#[test]
fn type_alone_is_an_empty_options_map() {
let entry: PortEntry = toml::from_str("type = \"stdout\"\n").unwrap();
assert_eq!(entry, PortEntry::new("stdout"));
assert!(entry.options.is_empty());
}
#[test]
fn missing_type_is_an_error() {
let error = toml::from_str::<PortEntry>("pretty = true\n").unwrap_err();
assert!(error.to_string().contains("type"), "{error}");
}
#[test]
fn nested_tables_round_trip() {
let text = "type = \"postgres\"\n\n[connection_url]\nhost = \"db\"\nport = 5432\n";
let entry: PortEntry = toml::from_str(text).unwrap();
let again = toml::to_string(&entry).unwrap();
assert_eq!(again, text);
}
}
impl schemars::JsonSchema for PortEntry {
fn schema_name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed("PortEntry")
}
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
schemars::json_schema!({
"type": "object",
"required": ["type"],
"properties": {
"type": { "type": "string", "description": "The adapter that implements this port." }
},
"additionalProperties": true
})
}
}