use crate::{PrettyPrint, PrettyTree};
use alloc::{borrow::Cow, rc::Rc};
use color_ansi::AnsiStyle;
use core::fmt::{Debug, Formatter};
pub struct PrettyProvider {
width: usize,
keyword: Rc<AnsiStyle>,
string: Rc<AnsiStyle>,
number: Rc<AnsiStyle>,
macros: Rc<AnsiStyle>,
argument: Rc<AnsiStyle>,
argument_mut: Rc<AnsiStyle>,
local: Rc<AnsiStyle>,
local_mut: Rc<AnsiStyle>,
operator: Rc<AnsiStyle>,
structure: Rc<AnsiStyle>,
variant: Rc<AnsiStyle>,
interface: Rc<AnsiStyle>,
}
impl Debug for PrettyProvider {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("PrettyProvider").finish()
}
}
impl PrettyProvider {
pub fn new(width: usize) -> Self {
Self {
width,
keyword: AnsiStyle::rgb(197, 119, 207).into(),
string: AnsiStyle::rgb(152, 195, 121).into(),
number: AnsiStyle::rgb(206, 153, 100).into(),
macros: AnsiStyle::rgb(87, 182, 194).into(),
argument: AnsiStyle::rgb(239, 112, 117).into(),
argument_mut: AnsiStyle::rgb(239, 112, 117).with_underline().into(),
local: AnsiStyle::rgb(152, 195, 121).into(),
local_mut: AnsiStyle::rgb(152, 195, 121).with_underline().into(),
operator: AnsiStyle::rgb(90, 173, 238).into(),
structure: AnsiStyle::rgb(197, 119, 207).into(),
variant: AnsiStyle::rgb(239, 112, 117).into(),
interface: AnsiStyle::rgb(197, 119, 207).into(),
}
}
}
impl PrettyProvider {
pub fn get_width(&self) -> usize {
self.width
}
pub fn set_width(&mut self, width: usize) {
self.width = width;
}
pub fn text<S>(&self, text: S) -> PrettyTree
where
S: Into<Cow<'static, str>>,
{
PrettyTree::text(text)
}
pub fn custom<S>(&self, text: S, style: Rc<AnsiStyle>) -> PrettyTree
where
S: Into<Cow<'static, str>>,
{
PrettyTree::text(text).annotate(style)
}
pub fn keyword<S>(&self, text: S) -> PrettyTree
where
S: Into<Cow<'static, str>>,
{
PrettyTree::text(text).annotate(self.keyword.clone())
}
pub fn identifier<S>(&self, text: S) -> PrettyTree
where
S: Into<Cow<'static, str>>,
{
PrettyTree::text(text).annotate(self.operator.clone())
}
pub fn generic<S>(&self, text: S) -> PrettyTree
where
S: Into<Cow<'static, str>>,
{
PrettyTree::text(text).annotate(self.macros.clone())
}
pub fn variable<S>(&self, text: S, mutable: bool) -> PrettyTree
where
S: Into<Cow<'static, str>>,
{
if mutable {
PrettyTree::text(text).annotate(self.local_mut.clone())
}
else {
PrettyTree::text(text).annotate(self.local.clone())
}
}
pub fn argument<S>(&self, text: S, mutable: bool) -> PrettyTree
where
S: Into<Cow<'static, str>>,
{
if mutable {
PrettyTree::text(text).annotate(self.argument_mut.clone())
}
else {
PrettyTree::text(text).annotate(self.argument.clone())
}
}
pub fn operator<S>(&self, text: S) -> PrettyTree
where
S: Into<Cow<'static, str>>,
{
PrettyTree::text(text).annotate(self.operator.clone())
}
pub fn string<S>(&self, text: S) -> PrettyTree
where
S: Into<Cow<'static, str>>,
{
PrettyTree::text(text).annotate(self.string.clone())
}
pub fn annotation<S>(&self, text: S) -> PrettyTree
where
S: Into<Cow<'static, str>>,
{
PrettyTree::text(text).annotate(self.macros.clone())
}
pub fn number<S>(&self, text: S) -> PrettyTree
where
S: Into<Cow<'static, str>>,
{
PrettyTree::text(text).annotate(self.number.clone())
}
pub fn structure<S>(&self, text: S) -> PrettyTree
where
S: Into<Cow<'static, str>>,
{
PrettyTree::text(text).annotate(self.structure.clone())
}
pub fn variant<S>(&self, text: S) -> PrettyTree
where
S: Into<Cow<'static, str>>,
{
PrettyTree::text(text).annotate(self.variant.clone())
}
pub fn interface<S>(&self, text: S) -> PrettyTree
where
S: Into<Cow<'static, str>>,
{
PrettyTree::text(text).annotate(self.interface.clone())
}
}
impl PrettyProvider {
pub fn join<I, T1, T2>(&self, iter: I, joint: T2) -> PrettyTree
where
I: IntoIterator<Item = T1>,
T1: PrettyPrint,
T2: PrettyPrint,
{
PrettyTree::join(iter.into_iter().map(|x| x.pretty(self)), joint.pretty(self))
}
pub fn join_slice<I, T>(&self, iter: &[I], joint: T) -> PrettyTree
where
I: PrettyPrint,
T: PrettyPrint,
{
PrettyTree::join(iter.iter().map(|s| s.pretty(self)), joint.pretty(self))
}
pub fn concat<I, T>(&self, iter: I) -> PrettyTree
where
I: IntoIterator<Item = T>,
T: PrettyPrint,
{
PrettyTree::concat(iter.into_iter().map(|x| x.pretty(self)))
}
pub fn concat_slice<T>(&self, iter: &[T]) -> PrettyTree
where
T: PrettyPrint,
{
PrettyTree::concat(iter.iter().map(|s| s.pretty(self)))
}
}