sexprs_data_structures/
traits.rs1#![allow(unused)]
2use std::cmp::{Eq, Ord, PartialEq, PartialOrd};
3use std::fmt::{Debug, Display};
4
5use crate::{
6 impl_as_number, impl_quotable_false, impl_quotable_false_t,
7 impl_quotable_false_tn, impl_quotable_string,
8};
9
10#[rustfmt::skip]
11pub trait ListValue:Sized + PartialOrd + Ord + PartialEq + Eq + Clone + Debug + Display{}
12#[rustfmt::skip]
13impl<T: Sized + PartialOrd + Ord + PartialEq + Eq + Clone + Debug + Display>ListValue for T{}
14
15pub trait Quotable: Sized + Debug {
16 fn is_quoted(&self) -> bool;
17 fn set_quoted(&mut self, quoted: bool);
18
19 fn quote(&self) -> Self {
20 use unique_pointer::UniquePointer;
21 let mut item = UniquePointer::from_ref(self).read();
22 item.set_quoted(true);
23 item
24 }
25
26 fn unquote(&self) -> Self {
27 use unique_pointer::UniquePointer;
28 let mut item = UniquePointer::from_ref(self).read();
29 item.set_quoted(false);
30 item
31 }
32}
33
34impl<T> Quotable for &T
35where
36 T: Quotable,
37{
38 fn set_quoted(&mut self, quoted: bool) {
39 use unique_pointer::UniquePointer;
40 Quotable::set_quoted(UniquePointer::from_ref(*self).inner_mut(), quoted)
41 }
42 fn is_quoted(&self) -> bool {
43 Quotable::is_quoted(*self)
44 }
45}
46impl_as_number!(u8);
47impl_as_number!(u16);
48impl_as_number!(u32);
49impl_as_number!(u64);
50impl_as_number!(usize);
51
52impl_as_number!(i8);
53impl_as_number!(i16);
54impl_as_number!(i32);
55impl_as_number!(i64);
56impl_as_number!(isize);
57
58impl_as_number!(f32);
59impl_as_number!(f64);
60
61impl_quotable_false!((), bool, u8, u32, u64, i32, i64);
62
63impl_quotable_false_tn!([T; N]);
64impl_quotable_string!(&'c str, &'c mut str, String, std::borrow::Cow<'_, str>);
65
66impl Quotable for Option<String> {
67 fn is_quoted(&self) -> bool {
68 if let Some(string) = self {
69 string.starts_with('\'')
70 } else {
71 false
72 }
73 }
74 fn set_quoted(&mut self, quoted: bool) {
75 unreachable!("{:#?}.set_quoted({})", self, quoted)
76 }
77}
78
79pub trait AsNumber<T>:
96 Sized + PartialOrd + PartialEq + Clone + Debug + Display
97{
98 fn as_number(&self) -> T;
99}
100
101#[macro_export]
102macro_rules! impl_as_number {
103 ($type:ty) => {
104 impl AsNumber<$type> for $type {
105 fn as_number(&self) -> $type {
106 *self
107 }
108 }
109 };
115}
116
117#[macro_export]
118macro_rules! impl_quotable_false {
119 ($($type:ty),* $(,)?) => {
120 $(
121 impl Quotable for $type {
122 fn is_quoted(&self) -> bool {
123 false
124 }
125 fn set_quoted(&mut self, quoted: bool) {
126 unreachable!("{:#?}.set_quoted({})", self, quoted)
127 }
128
129 }
130 )*
131 };
132}
133#[macro_export]
134macro_rules! impl_quotable_false_t {
135 ($type:tt) => {
136 impl<T> Quotable for $type {
137 fn is_quoted(&self) -> bool {
138 false
139 }
140 }
141 fn set_quoted(&mut self, quoted: bool) {
142 unreachable!("{:#?}.set_quoted({})", self, quoted)
143 }
144 };
145}
146#[macro_export]
147macro_rules! impl_quotable_false_tn {
148 ($type:tt) => {
149 impl<T: Quotable, const N: usize> Quotable for $type {
150 fn is_quoted(&self) -> bool {
151 for item in self {
152 if item.is_quoted() {
153 return true;
154 }
155 }
156 false
157 }
158 fn set_quoted(&mut self, quoted: bool) {
159 for item in self.iter_mut() {
160 eprintln!("{:#?}.set_quoted({})", &item, quoted);
161 item.set_quoted(quoted);
162 }
163 }
164 }
165 };
166}
167
168#[macro_export]
169macro_rules! impl_quotable_string {
170 ($($type:ty),* $(,)?) => {
171 $(
172 impl<'c> Quotable for $type {
173 fn is_quoted(&self) -> bool {
174 self.starts_with('\'')
175 }
176 fn set_quoted(&mut self, quoted: bool) {
177 unreachable!("{:#?}.set_quoted({})", self, quoted)
178 }
179 }
180 )*
181 };
182}