use super::declare::{refusal_conversion, refusal_declaration};
use super::place::published_module;
use super::read::decode_road;
use super::spell::type_path;
use super::write::encode_road;
use super::{CodecContent, CodecPlacement, CodecProjection};
use crate::bounded::Overflow;
use crate::kind::SoleRole;
use crate::plan::Plan;
use crate::render::{Output, RenderError};
use crate::token::{GeneratedToken, GeneratedTree, implementation};
pub fn render_codec(
plan: &Plan<CodecProjection>,
out: &mut Output<'_, CodecProjection>,
) -> Result<(), RenderError> {
let tree = codec_surface(plan.content())?;
out.unit(SoleRole::Sole, tree)
}
pub fn codec_surface(content: &CodecContent) -> Result<GeneratedTree, Overflow> {
let shape = &content.shape;
let reads = content.direction.reads();
let mut tokens: Vec<GeneratedToken> = Vec::new();
if reads {
tokens.extend(refusal_declaration(shape)?);
tokens.extend(refusal_conversion(shape)?);
}
let mut inherent: Vec<GeneratedToken> = Vec::new();
if content.direction.writes() {
inherent.extend(encode_road(shape)?);
}
if reads {
inherent.extend(decode_road(shape)?);
}
tokens.extend(implementation(
Vec::new(),
Vec::new(),
None,
type_path(shape.owner()),
Vec::new(),
inherent,
)?);
let placed = match &content.placement {
CodecPlacement::AtDeclarationSite => tokens,
CodecPlacement::PublishedModule { spelling } => {
published_module(spelling.spelling(), tokens)?
}
};
GeneratedTree::assembled(placed)
}