use std::collections::BTreeSet;
use crate::{
TierMetadata,
export::{json_pretty, json_value},
};
use schemars::JsonSchema;
use serde_json::Value;
use super::json::annotated_json_schema_for;
use crate::schema::example::build_example_value;
use crate::schema::model::{ConfigExampleReport, SCHEMA_EXPORT_FORMAT_VERSION};
#[must_use]
pub fn config_example_for<T>() -> Value
where
T: JsonSchema + TierMetadata,
{
let schema = annotated_json_schema_for::<T>();
build_example_value(&schema, &schema, &mut BTreeSet::new(), None)
.unwrap_or(Value::Object(Default::default()))
}
#[must_use]
pub fn config_example_pretty<T>() -> String
where
T: JsonSchema + TierMetadata,
{
json_pretty(
&config_example_for::<T>(),
"{\"error\":\"failed to render example config\"}",
)
}
#[must_use]
pub fn config_example_report<T>() -> ConfigExampleReport
where
T: JsonSchema + TierMetadata,
{
ConfigExampleReport {
format_version: SCHEMA_EXPORT_FORMAT_VERSION,
example: config_example_for::<T>(),
}
}
#[must_use]
pub fn config_example_report_json<T>() -> Value
where
T: JsonSchema + TierMetadata,
{
json_value(
&config_example_report::<T>(),
Value::Object(Default::default()),
)
}
#[must_use]
pub fn config_example_report_json_pretty<T>() -> String
where
T: JsonSchema + TierMetadata,
{
json_pretty(
&config_example_report_json::<T>(),
"{\"error\":\"failed to render example report\"}",
)
}