#![allow(async_fn_in_trait)]
use crate::context::{Context, ContextProvides, DynContext};
pub mod common;
pub mod context;
pub(crate) mod cstyle_common;
pub mod java;
pub mod rust;
pub mod util;
pub trait Writable<O: Output> {
async fn write_to(&self, output: &mut O) -> Result<(), O::Error>;
}
impl<'w, W, O> Writable<O> for &'w W
where
W: Writable<O>,
O: Output,
{
async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
(**self).write_to(output).await
}
}
pub trait WritableSeq<O: Output> {
async fn for_each<S>(&self, sink: &mut S) -> Result<(), O::Error>
where
S: SequenceAccept<O>;
}
impl<'w, W, O> WritableSeq<O> for &'w W
where
O: Output,
W: WritableSeq<O>,
{
async fn for_each<S>(&self, sink: &mut S) -> Result<(), O::Error>
where
S: SequenceAccept<O>,
{
(**self).for_each(sink).await
}
}
pub trait SequenceAccept<O: Output> {
async fn accept<W>(&mut self, writable: &W) -> Result<(), O::Error>
where
W: Writable<O>;
}
impl<'s, S, O> SequenceAccept<O> for &'s mut S
where
O: Output,
S: SequenceAccept<O>,
{
async fn accept<W>(&mut self, writable: &W) -> Result<(), O::Error>
where
W: Writable<O>,
{
(**self).accept(writable).await
}
}
pub trait Output {
type Io<'o>: IoOutput<Error: Into<Self::Error>>
where
Self: 'o;
type Ctx: Context;
type Error;
async fn write(&mut self, value: &str) -> Result<(), Self::Error>;
fn split(&mut self) -> (Self::Io<'_>, &Self::Ctx);
fn context(&self) -> &Self::Ctx;
fn get_ctx<T>(&self) -> &T
where
Self::Ctx: ContextProvides<T>,
{
self.context().provide()
}
}
impl<'o, O> Output for &'o mut O
where
O: Output,
{
type Io<'b>
= O::Io<'b>
where
Self: 'b;
type Ctx = O::Ctx;
type Error = O::Error;
async fn write(&mut self, value: &str) -> Result<(), Self::Error> {
(**self).write(value).await
}
fn split(&mut self) -> (Self::Io<'_>, &Self::Ctx) {
(**self).split()
}
fn context(&self) -> &Self::Ctx {
(**self).context()
}
}
pub struct SimpleOutput<I: IoOutput> {
pub io_output: I,
pub context: DynContext,
}
impl<I> Output for SimpleOutput<I>
where
I: IoOutput,
{
type Io<'b>
= &'b mut I
where
Self: 'b;
type Ctx = DynContext;
type Error = I::Error;
async fn write(&mut self, value: &str) -> Result<(), Self::Error> {
self.io_output.write(value).await
}
fn split(&mut self) -> (Self::Io<'_>, &Self::Ctx) {
(&mut self.io_output, &self.context)
}
fn context(&self) -> &Self::Ctx {
&self.context
}
}
pub trait IoOutput {
type Error;
async fn write(&mut self, value: &str) -> Result<(), Self::Error>;
}
impl<'o, O> IoOutput for &'o mut O
where
O: IoOutput,
{
type Error = O::Error;
async fn write(&mut self, value: &str) -> Result<(), Self::Error> {
(**self).write(value).await
}
}