use std::ops::Deref;
use crate::{
ast::{self, span::Span},
attributes::LinkedItemGraph,
printer::pretty_ast::ToDocument,
};
use ast::visitors::dyn_compatible;
pub mod pretty_ast;
pub use pretty_ast::PrettyAst;
pub mod render_view;
pub trait Resugaring: for<'a> dyn_compatible::AstVisitorMut<'a> {
fn name(&self) -> String;
}
pub trait Printer: Sized + PrettyAst<Span> + Default + HasLinkedItemGraph {
const NAME: &'static str = <Self as PrettyAst<Span>>::NAME;
}
pub trait HasLinkedItemGraph {
fn linked_item_graph(&self) -> &LinkedItemGraph;
fn with_linked_item_graph(self, graph: std::rc::Rc<LinkedItemGraph>) -> Self;
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct SourceMap;
pub trait Print<T>
where
for<'a> dyn Resugaring: dyn_compatible::AstVisitableMut<'a, T>,
{
fn print_returning_fragment(&mut self, fragment: T) -> (String, SourceMap, T)
where
T: ToDocument<Self, Span>;
fn print(&mut self, fragment: T) -> (String, SourceMap)
where
T: ToDocument<Self, Span>;
}
impl<P: Printer, T> Print<T> for P
where
for<'a> dyn Resugaring: dyn_compatible::AstVisitableMut<'a, T>,
{
fn print_returning_fragment(&mut self, fragment: T) -> (String, SourceMap, T)
where
T: ToDocument<Self, Span>,
{
let doc_builder = fragment.to_document(self).into_doc();
(
doc_builder.deref().pretty(80).to_string(),
SourceMap,
fragment,
)
}
fn print(&mut self, fragment: T) -> (String, SourceMap)
where
T: ToDocument<Self, Span>,
{
let (rendered, sourcemap, _) = <Self as Print<_>>::print_returning_fragment(self, fragment);
(rendered, sourcemap)
}
}