1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
use std::cell::RefCell;
use vector_config_common::{attributes::CustomAttribute, constants};
use crate::schema::generate_optional_schema;
use crate::{
num::NumberClass,
schema::{generate_number_schema, SchemaGenerator, SchemaObject},
Configurable, GenerateError, Metadata,
};
// Blanket implementation of `Configurable` for any `serde_with` helper that is also `Configurable`.
impl<T> Configurable for serde_with::As<T>
where
T: Configurable,
{
fn referenceable_name() -> Option<&'static str> {
// Forward to the underlying `T`.
T::referenceable_name()
}
fn metadata() -> Metadata {
// Forward to the underlying `T`.
//
// We have to convert from `Metadata` to `Metadata` which erases the default value,
// notably, but `serde_with` helpers should never actually have default values, so this is
// essentially a no-op.
T::metadata().convert()
}
fn validate_metadata(metadata: &Metadata) -> Result<(), GenerateError> {
// Forward to the underlying `T`.
//
// We have to convert from `Metadata` to `Metadata` which erases the default value,
// notably, but `serde_with` helpers should never actually have default values, so this is
// essentially a no-op.
let converted = metadata.convert();
T::validate_metadata(&converted)
}
fn generate_schema(gen: &RefCell<SchemaGenerator>) -> Result<SchemaObject, GenerateError> {
// Forward to the underlying `T`.
//
// We have to convert from `Metadata` to `Metadata` which erases the default value,
// notably, but `serde_with` helpers should never actually have default values, so this is
// essentially a no-op.
T::generate_schema(gen)
}
}
impl Configurable for serde_with::DurationSeconds<u64, serde_with::formats::Strict> {
fn referenceable_name() -> Option<&'static str> {
// We're masking the type parameters here because we only deal with whole seconds via this
// version, and handle fractional seconds with `DurationSecondsWithFrac<f64, Strict>`, which we
// expose as `serde_with::DurationFractionalSeconds`.
Some("serde_with::DurationSeconds")
}
fn metadata() -> Metadata {
let mut metadata = Metadata::default();
metadata.set_description("A span of time, in whole seconds.");
metadata.add_custom_attribute(CustomAttribute::kv(
constants::DOCS_META_NUMERIC_TYPE,
NumberClass::Unsigned,
));
metadata.add_custom_attribute(CustomAttribute::kv(
constants::DOCS_META_TYPE_UNIT,
"seconds",
));
metadata
}
fn generate_schema(_: &RefCell<SchemaGenerator>) -> Result<SchemaObject, GenerateError> {
// This boils down to a number schema, but we just need to shuttle around the metadata so
// that we can call the relevant schema generation function.
Ok(generate_number_schema::<u64>())
}
}
impl Configurable for serde_with::DurationSecondsWithFrac<f64, serde_with::formats::Strict> {
fn referenceable_name() -> Option<&'static str> {
// We're masking the type parameters here because we only deal with fractional seconds via this
// version, and handle whole seconds with `DurationSeconds<u64, Strict>`, which we
// expose as `serde_with::DurationSeconds`.
Some("serde_with::DurationFractionalSeconds")
}
fn metadata() -> Metadata {
let mut metadata = Metadata::default();
metadata.set_description("A span of time, in fractional seconds.");
metadata.add_custom_attribute(CustomAttribute::kv(
constants::DOCS_META_NUMERIC_TYPE,
NumberClass::FloatingPoint,
));
metadata.add_custom_attribute(CustomAttribute::kv(
constants::DOCS_META_TYPE_UNIT,
"seconds",
));
metadata
}
fn generate_schema(_: &RefCell<SchemaGenerator>) -> Result<SchemaObject, GenerateError> {
// This boils down to a number schema, but we just need to shuttle around the metadata so
// that we can call the relevant schema generation function.
Ok(generate_number_schema::<f64>())
}
}
impl Configurable for serde_with::DurationMilliSeconds<u64, serde_with::formats::Strict> {
fn referenceable_name() -> Option<&'static str> {
// We're masking the type parameters here because we only deal with whole milliseconds via this
// version.
Some("serde_with::DurationMilliSeconds")
}
fn metadata() -> Metadata {
let mut metadata = Metadata::default();
metadata.set_description("A span of time, in whole milliseconds.");
metadata.add_custom_attribute(CustomAttribute::kv(
constants::DOCS_META_NUMERIC_TYPE,
NumberClass::Unsigned,
));
metadata.add_custom_attribute(CustomAttribute::kv(
constants::DOCS_META_TYPE_UNIT,
"milliseconds",
));
metadata
}
fn generate_schema(_: &RefCell<SchemaGenerator>) -> Result<SchemaObject, GenerateError> {
// This boils down to a number schema, but we just need to shuttle around the metadata so
// that we can call the relevant schema generation function.
Ok(generate_number_schema::<u64>())
}
}
impl Configurable for Option<serde_with::DurationMilliSeconds<u64, serde_with::formats::Strict>> {
fn generate_schema(gen: &RefCell<SchemaGenerator>) -> Result<SchemaObject, GenerateError>
where
Self: Sized,
{
generate_optional_schema(&u64::as_configurable_ref(), gen)
}
}