use crate::v2::{MAX_JSON_NESTING, read_json, render_json};
pub mod v1 {
include!(concat!(env!("OUT_DIR"), "/ridl.codegen.v1.rs"));
#[expect(clippy::useless_borrows_in_formatting)]
mod serde_impls {
use super::*;
include!(concat!(env!("OUT_DIR"), "/ridl.codegen.v1.serde.rs"));
}
}
mod clauses;
mod contract;
mod facts;
mod flatbuffers;
mod lower;
mod names;
mod resolve;
mod unbounded;
pub use contract::{
Backend, ModelBackend, RawIr, SCHEMA, check_path, error, has_error, request_from_json,
request_to_json, response_from_json, response_to_json, text_file,
};
pub use lower::lower;
#[derive(Debug)]
pub enum SerializeError {
Json(serde_json::Error),
Text(prost::DecodeError),
}
impl std::fmt::Display for SerializeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Json(source) => write!(
f,
"cannot render the codegen model as canonical protobuf JSON: {source}; the known \
cause is an enum field holding a discriminant outside the schema"
),
Self::Text(source) => write!(
f,
"cannot render the codegen model as prototext: {source}; the known cause is \
composite nesting deeper than the transcoding decoder's recursion limit"
),
}
}
}
impl SerializeError {
fn from_v2(error: crate::v2::SerializeError) -> Self {
match error {
crate::v2::SerializeError::Json(source) => Self::Json(source),
crate::v2::SerializeError::Text(source) => Self::Text(source),
}
}
}
impl std::error::Error for SerializeError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Json(source) => Some(source),
Self::Text(source) => Some(source),
}
}
}
pub fn to_json_pretty(model: &v1::Model) -> Result<String, SerializeError> {
render_json(model).map_err(SerializeError::from_v2)
}
pub fn from_json(text: &str) -> Result<v1::Model, serde_json::Error> {
read_json(text)
}
pub const MAX_JSON_NESTING_LEVELS: usize = MAX_JSON_NESTING;
pub fn to_text_format(model: &v1::Model) -> Result<String, SerializeError> {
crate::v2::render_text_for(crate::v2::codegen_model_descriptor(), model)
.map_err(SerializeError::from_v2)
}
pub fn to_binary(model: &v1::Model) -> Vec<u8> {
prost::Message::encode_to_vec(model)
}
pub fn from_binary(bytes: &[u8]) -> Result<v1::Model, prost::DecodeError> {
prost::Message::decode(bytes)
}
#[cfg(test)]
mod tests;