pub(crate) mod cache;
pub(crate) mod common;
pub(crate) mod discriminator;
pub(crate) mod fields;
pub(crate) mod hashing;
pub(crate) mod methods;
pub(crate) mod operations;
pub(crate) mod parameters;
pub(crate) mod relaxed_enum;
pub(crate) mod requests;
pub(crate) mod responses;
pub(crate) mod struct_summaries;
pub(crate) mod structs;
pub(crate) mod type_resolver;
pub(crate) mod type_usage_recorder;
pub(crate) mod union_types;
pub(crate) mod unions;
pub(crate) mod value_enums;
pub(crate) mod variants;
use std::{
cell::RefCell,
collections::{BTreeSet, HashMap},
rc::Rc,
sync::Arc,
};
pub(crate) use common::{ConversionOutput, SchemaExt};
use oas3::spec::ObjectSchema;
pub(crate) use type_resolver::TypeResolver;
pub(crate) use type_usage_recorder::TypeUsageRecorder;
use super::ast::RustType;
use crate::generator::{converter::cache::SharedSchemaCache, schema_registry::SchemaRegistry};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EnumCasePolicy {
Preserve,
#[default]
Deduplicate,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EnumHelperPolicy {
#[default]
Generate,
Disable,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EnumDeserializePolicy {
#[default]
CaseSensitive,
CaseInsensitive,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ODataPolicy {
#[default]
Disabled,
Enabled,
}
#[derive(Debug, Clone, Default)]
pub(crate) struct CodegenConfig {
pub enum_case: EnumCasePolicy,
pub enum_helpers: EnumHelperPolicy,
pub enum_deserialize: EnumDeserializePolicy,
pub odata: ODataPolicy,
pub customizations: HashMap<String, String>,
}
impl CodegenConfig {
#[must_use]
pub fn preserve_case_variants(&self) -> bool {
self.enum_case == EnumCasePolicy::Preserve
}
#[must_use]
pub fn case_insensitive_enums(&self) -> bool {
self.enum_deserialize == EnumDeserializePolicy::CaseInsensitive
}
#[must_use]
pub fn no_helpers(&self) -> bool {
self.enum_helpers == EnumHelperPolicy::Disable
}
#[must_use]
pub fn odata_support(&self) -> bool {
self.odata == ODataPolicy::Enabled
}
}
#[derive(Debug, Clone)]
pub(crate) struct ConverterContext {
pub(crate) graph: Arc<SchemaRegistry>,
pub(crate) config: CodegenConfig,
pub(crate) cache: RefCell<SharedSchemaCache>,
pub(crate) reachable_schemas: Option<Arc<BTreeSet<String>>>,
}
impl ConverterContext {
pub(crate) fn new(
graph: Arc<SchemaRegistry>,
config: CodegenConfig,
cache: SharedSchemaCache,
reachable_schemas: Option<Arc<BTreeSet<String>>>,
) -> Self {
Self {
graph,
config,
cache: RefCell::new(cache),
reachable_schemas,
}
}
pub(crate) fn graph(&self) -> &SchemaRegistry {
&self.graph
}
pub(crate) fn config(&self) -> &CodegenConfig {
&self.config
}
}
#[derive(Debug, Clone)]
pub(crate) struct SchemaConverter {
context: Rc<ConverterContext>,
type_resolver: TypeResolver,
}
impl SchemaConverter {
pub(crate) fn new(context: &Rc<ConverterContext>) -> Self {
Self {
context: context.clone(),
type_resolver: TypeResolver::new(context.clone()),
}
}
pub(crate) fn context(&self) -> &Rc<ConverterContext> {
&self.context
}
pub(crate) fn convert_schema(&self, name: &str, schema: &ObjectSchema) -> anyhow::Result<Vec<RustType>> {
self.type_resolver.convert_schema(name, schema)
}
pub(crate) fn contains(&self, name: &str) -> bool {
self.context.graph().contains(name)
}
}
#[cfg(test)]
mod tests;