oak_pretty_print/
to_doc.rs1use crate::Document;
2use alloc::{boxed::Box, string::String, vec::Vec};
3
4pub trait AsDocument {
43 type Params = ();
46
47 fn as_document(&self, params: &Self::Params) -> Document<'_>;
55}
56
57pub trait ToDocument<'a> {
59 fn to_document(self) -> Document<'a>;
61}
62
63impl AsDocument for String {
64 fn as_document(&self, _params: &Self::Params) -> Document<'_> {
65 Document::Text(self.as_str().into())
66 }
67}
68
69impl AsDocument for str {
70 fn as_document(&self, _params: &Self::Params) -> Document<'_> {
71 Document::Text(self.into())
72 }
73}
74
75impl<'a> AsDocument for Document<'a> {
76 fn as_document(&self, _params: &Self::Params) -> Document<'_> {
77 self.clone()
78 }
79}
80
81impl<T: AsDocument> AsDocument for Vec<T> {
82 type Params = T::Params;
83
84 fn as_document(&self, params: &Self::Params) -> Document<'_> {
85 Document::Concat(self.iter().map(|t| t.as_document(params)).collect())
86 }
87}
88
89impl<T: AsDocument> AsDocument for Option<T> {
90 type Params = T::Params;
91
92 fn as_document(&self, params: &Self::Params) -> Document<'_> {
93 match self {
94 Some(t) => t.as_document(params),
95 None => Document::Nil,
96 }
97 }
98}
99
100impl<T: AsDocument + ?Sized> AsDocument for &T {
101 type Params = T::Params;
102
103 fn as_document(&self, params: &Self::Params) -> Document<'_> {
104 (**self).as_document(params)
105 }
106}
107
108impl<T: AsDocument + ?Sized> AsDocument for Box<T> {
109 type Params = T::Params;
110
111 fn as_document(&self, params: &Self::Params) -> Document<'_> {
112 self.as_ref().as_document(params)
113 }
114}
115
116impl<'a> ToDocument<'a> for Document<'a> {
117 fn to_document(self) -> Document<'a> {
118 self
119 }
120}
121
122impl<'a, T: AsDocument + ?Sized> ToDocument<'a> for &'a T
123where
124 T::Params: Default,
125{
126 fn to_document(self) -> Document<'a> {
127 self.as_document(&T::Params::default())
128 }
129}
130
131impl<'a> ToDocument<'a> for String {
132 fn to_document(self) -> Document<'a> {
133 Document::Text(self.into())
134 }
135}