1use super::{
27 escape_html, lean_dquote_escape, mysql_squote_escape, nix_dquote_escape, py_dquote_escape,
28 sh_dquote_escape, sql_squote_escape, ts_dquote_escape, Bash, Html, Lean, LiftTo, MySql, Nix,
29 Python, Sql, TypeScript, Wgsl, Zsh,
30};
31use crate::qterm::{leaf, sym, tb, QTerm};
32use std::sync::Arc;
33
34pub fn wgsl_int_term(text: &str) -> Arc<QTerm> {
42 leaf("int_literal", text)
43}
44impl LiftTo<Wgsl> for u8 {
45 fn lift_to(&self) -> Arc<QTerm> {
46 wgsl_int_term(&format!("{self}u"))
47 }
48}
49impl LiftTo<Wgsl> for u16 {
50 fn lift_to(&self) -> Arc<QTerm> {
51 wgsl_int_term(&format!("{self}u"))
52 }
53}
54impl LiftTo<Wgsl> for u32 {
55 fn lift_to(&self) -> Arc<QTerm> {
56 wgsl_int_term(&format!("{self}u"))
57 }
58}
59impl LiftTo<Wgsl> for usize {
60 fn lift_to(&self) -> Arc<QTerm> {
61 wgsl_int_term(&format!("{self}u"))
62 }
63}
64impl LiftTo<Wgsl> for i8 {
65 fn lift_to(&self) -> Arc<QTerm> {
66 wgsl_int_term(&format!("{self}i"))
67 }
68}
69impl LiftTo<Wgsl> for i16 {
70 fn lift_to(&self) -> Arc<QTerm> {
71 wgsl_int_term(&format!("{self}i"))
72 }
73}
74impl LiftTo<Wgsl> for i32 {
75 fn lift_to(&self) -> Arc<QTerm> {
76 wgsl_int_term(&format!("{self}i"))
77 }
78}
79impl LiftTo<Wgsl> for isize {
80 fn lift_to(&self) -> Arc<QTerm> {
81 wgsl_int_term(&format!("{self}i"))
82 }
83}
84pub fn wgsl_float_term(text: &str) -> Arc<QTerm> {
86 leaf("float_literal", text)
87}
88impl LiftTo<Wgsl> for f32 {
89 fn lift_to(&self) -> Arc<QTerm> {
90 wgsl_float_term(&format!("{self}f"))
91 }
92}
93pub fn wgsl_bool_term(b: bool) -> Arc<QTerm> {
97 if b {
98 tb("bool_literal").c(&sym("true")).b()
99 } else {
100 tb("bool_literal").c(&sym("false")).b()
101 }
102}
103impl LiftTo<Wgsl> for bool {
104 fn lift_to(&self) -> Arc<QTerm> {
105 wgsl_bool_term(*self)
106 }
107}
108pub fn py_int_term(text: &str) -> Arc<QTerm> {
111 leaf("integer", text)
112}
113impl LiftTo<Python> for u8 {
114 fn lift_to(&self) -> Arc<QTerm> {
115 py_int_term(&self.to_string())
116 }
117}
118impl LiftTo<Python> for u16 {
119 fn lift_to(&self) -> Arc<QTerm> {
120 py_int_term(&self.to_string())
121 }
122}
123impl LiftTo<Python> for u32 {
124 fn lift_to(&self) -> Arc<QTerm> {
125 py_int_term(&self.to_string())
126 }
127}
128impl LiftTo<Python> for u64 {
129 fn lift_to(&self) -> Arc<QTerm> {
130 py_int_term(&self.to_string())
131 }
132}
133impl LiftTo<Python> for u128 {
134 fn lift_to(&self) -> Arc<QTerm> {
135 py_int_term(&self.to_string())
136 }
137}
138impl LiftTo<Python> for usize {
139 fn lift_to(&self) -> Arc<QTerm> {
140 py_int_term(&self.to_string())
141 }
142}
143impl LiftTo<Python> for i8 {
144 fn lift_to(&self) -> Arc<QTerm> {
145 py_int_term(&self.to_string())
146 }
147}
148impl LiftTo<Python> for i16 {
149 fn lift_to(&self) -> Arc<QTerm> {
150 py_int_term(&self.to_string())
151 }
152}
153impl LiftTo<Python> for i32 {
154 fn lift_to(&self) -> Arc<QTerm> {
155 py_int_term(&self.to_string())
156 }
157}
158impl LiftTo<Python> for i64 {
159 fn lift_to(&self) -> Arc<QTerm> {
160 py_int_term(&self.to_string())
161 }
162}
163impl LiftTo<Python> for i128 {
164 fn lift_to(&self) -> Arc<QTerm> {
165 py_int_term(&self.to_string())
166 }
167}
168impl LiftTo<Python> for isize {
169 fn lift_to(&self) -> Arc<QTerm> {
170 py_int_term(&self.to_string())
171 }
172}
173pub fn py_float_term(text: &str) -> Arc<QTerm> {
175 leaf("float", text)
176}
177impl LiftTo<Python> for f32 {
178 fn lift_to(&self) -> Arc<QTerm> {
179 py_float_term(&format!("{self:?}"))
180 }
181}
182impl LiftTo<Python> for f64 {
183 fn lift_to(&self) -> Arc<QTerm> {
184 py_float_term(&format!("{self:?}"))
185 }
186}
187pub fn py_bool_term(b: bool) -> Arc<QTerm> {
190 if b {
191 leaf("true", "True")
192 } else {
193 leaf("false", "False")
194 }
195}
196impl LiftTo<Python> for bool {
197 fn lift_to(&self) -> Arc<QTerm> {
198 py_bool_term(*self)
199 }
200}
201pub fn py_string_term(s: &str) -> Arc<QTerm> {
211 if s.is_empty() {
212 tb("string")
213 .c(&leaf("string_start", "\""))
214 .c(&leaf("string_end", "\""))
215 .b()
216 } else {
217 tb("string")
218 .c(&leaf("string_start", "\""))
219 .c(&leaf("string_content", &py_dquote_escape(s)))
220 .c(&leaf("string_end", "\""))
221 .b()
222 }
223}
224impl LiftTo<Python> for str {
225 fn lift_to(&self) -> Arc<QTerm> {
226 py_string_term(self)
227 }
228}
229impl LiftTo<Python> for String {
230 fn lift_to(&self) -> Arc<QTerm> {
231 py_string_term(self)
232 }
233}
234pub fn py_list_term(items: &[Arc<QTerm>]) -> Arc<QTerm> {
239 let mut b = tb("list");
240 b.child(&sym("["));
241 for (i, x) in items.iter().enumerate() {
242 if i > 0 {
243 b.child(&sym(","));
244 b.write(" ");
245 }
246 b.child(x);
247 }
248 b.child(&sym("]"));
249 b.b()
250}
251impl<T: LiftTo<Python>> LiftTo<Python> for [T] {
252 fn lift_to(&self) -> Arc<QTerm> {
253 py_list_term(&self.iter().map(|x| x.lift_to()).collect::<Vec<_>>())
254 }
255}
256impl<T: LiftTo<Python>> LiftTo<Python> for Vec<T> {
257 fn lift_to(&self) -> Arc<QTerm> {
258 self.as_slice().lift_to()
259 }
260}
261pub fn bash_number_term(text: &str) -> Arc<QTerm> {
263 leaf("number", text)
264}
265impl LiftTo<Bash> for u8 {
266 fn lift_to(&self) -> Arc<QTerm> {
267 bash_number_term(&self.to_string())
268 }
269}
270impl LiftTo<Bash> for u16 {
271 fn lift_to(&self) -> Arc<QTerm> {
272 bash_number_term(&self.to_string())
273 }
274}
275impl LiftTo<Bash> for u32 {
276 fn lift_to(&self) -> Arc<QTerm> {
277 bash_number_term(&self.to_string())
278 }
279}
280impl LiftTo<Bash> for u64 {
281 fn lift_to(&self) -> Arc<QTerm> {
282 bash_number_term(&self.to_string())
283 }
284}
285impl LiftTo<Bash> for usize {
286 fn lift_to(&self) -> Arc<QTerm> {
287 bash_number_term(&self.to_string())
288 }
289}
290impl LiftTo<Bash> for i8 {
291 fn lift_to(&self) -> Arc<QTerm> {
292 bash_number_term(&self.to_string())
293 }
294}
295impl LiftTo<Bash> for i16 {
296 fn lift_to(&self) -> Arc<QTerm> {
297 bash_number_term(&self.to_string())
298 }
299}
300impl LiftTo<Bash> for i32 {
301 fn lift_to(&self) -> Arc<QTerm> {
302 bash_number_term(&self.to_string())
303 }
304}
305impl LiftTo<Bash> for i64 {
306 fn lift_to(&self) -> Arc<QTerm> {
307 bash_number_term(&self.to_string())
308 }
309}
310impl LiftTo<Bash> for isize {
311 fn lift_to(&self) -> Arc<QTerm> {
312 bash_number_term(&self.to_string())
313 }
314}
315pub fn bash_string_term(s: &str) -> Arc<QTerm> {
319 if s.is_empty() {
320 tb("string").c(&sym("\"")).c(&sym("\"")).b()
321 } else {
322 tb("string")
323 .c(&sym("\""))
324 .c(&leaf("string_content", &sh_dquote_escape(s)))
325 .c(&sym("\""))
326 .b()
327 }
328}
329impl LiftTo<Bash> for str {
330 fn lift_to(&self) -> Arc<QTerm> {
331 bash_string_term(self)
332 }
333}
334impl LiftTo<Bash> for String {
335 fn lift_to(&self) -> Arc<QTerm> {
336 bash_string_term(self)
337 }
338}
339pub fn zsh_number_term(text: &str) -> Arc<QTerm> {
343 leaf("number", text)
344}
345impl LiftTo<Zsh> for u8 {
346 fn lift_to(&self) -> Arc<QTerm> {
347 zsh_number_term(&self.to_string())
348 }
349}
350impl LiftTo<Zsh> for u16 {
351 fn lift_to(&self) -> Arc<QTerm> {
352 zsh_number_term(&self.to_string())
353 }
354}
355impl LiftTo<Zsh> for u32 {
356 fn lift_to(&self) -> Arc<QTerm> {
357 zsh_number_term(&self.to_string())
358 }
359}
360impl LiftTo<Zsh> for u64 {
361 fn lift_to(&self) -> Arc<QTerm> {
362 zsh_number_term(&self.to_string())
363 }
364}
365impl LiftTo<Zsh> for usize {
366 fn lift_to(&self) -> Arc<QTerm> {
367 zsh_number_term(&self.to_string())
368 }
369}
370impl LiftTo<Zsh> for i8 {
371 fn lift_to(&self) -> Arc<QTerm> {
372 zsh_number_term(&self.to_string())
373 }
374}
375impl LiftTo<Zsh> for i16 {
376 fn lift_to(&self) -> Arc<QTerm> {
377 zsh_number_term(&self.to_string())
378 }
379}
380impl LiftTo<Zsh> for i32 {
381 fn lift_to(&self) -> Arc<QTerm> {
382 zsh_number_term(&self.to_string())
383 }
384}
385impl LiftTo<Zsh> for i64 {
386 fn lift_to(&self) -> Arc<QTerm> {
387 zsh_number_term(&self.to_string())
388 }
389}
390impl LiftTo<Zsh> for isize {
391 fn lift_to(&self) -> Arc<QTerm> {
392 zsh_number_term(&self.to_string())
393 }
394}
395pub fn zsh_string_term(s: &str) -> Arc<QTerm> {
397 if s.is_empty() {
398 tb("string").c(&sym("\"")).c(&sym("\"")).b()
399 } else {
400 tb("string")
401 .c(&sym("\""))
402 .c(&leaf("string_content", &sh_dquote_escape(s)))
403 .c(&sym("\""))
404 .b()
405 }
406}
407impl LiftTo<Zsh> for str {
408 fn lift_to(&self) -> Arc<QTerm> {
409 zsh_string_term(self)
410 }
411}
412impl LiftTo<Zsh> for String {
413 fn lift_to(&self) -> Arc<QTerm> {
414 zsh_string_term(self)
415 }
416}
417pub fn nix_int_term(text: &str) -> Arc<QTerm> {
419 leaf("integer_expression", text)
420}
421impl LiftTo<Nix> for u8 {
422 fn lift_to(&self) -> Arc<QTerm> {
423 nix_int_term(&self.to_string())
424 }
425}
426impl LiftTo<Nix> for u16 {
427 fn lift_to(&self) -> Arc<QTerm> {
428 nix_int_term(&self.to_string())
429 }
430}
431impl LiftTo<Nix> for u32 {
432 fn lift_to(&self) -> Arc<QTerm> {
433 nix_int_term(&self.to_string())
434 }
435}
436impl LiftTo<Nix> for u64 {
437 fn lift_to(&self) -> Arc<QTerm> {
438 nix_int_term(&self.to_string())
439 }
440}
441impl LiftTo<Nix> for usize {
442 fn lift_to(&self) -> Arc<QTerm> {
443 nix_int_term(&self.to_string())
444 }
445}
446impl LiftTo<Nix> for i8 {
447 fn lift_to(&self) -> Arc<QTerm> {
448 nix_int_term(&self.to_string())
449 }
450}
451impl LiftTo<Nix> for i16 {
452 fn lift_to(&self) -> Arc<QTerm> {
453 nix_int_term(&self.to_string())
454 }
455}
456impl LiftTo<Nix> for i32 {
457 fn lift_to(&self) -> Arc<QTerm> {
458 nix_int_term(&self.to_string())
459 }
460}
461impl LiftTo<Nix> for i64 {
462 fn lift_to(&self) -> Arc<QTerm> {
463 nix_int_term(&self.to_string())
464 }
465}
466impl LiftTo<Nix> for isize {
467 fn lift_to(&self) -> Arc<QTerm> {
468 nix_int_term(&self.to_string())
469 }
470}
471pub fn nix_float_term(text: &str) -> Arc<QTerm> {
473 leaf("float_expression", text)
474}
475impl LiftTo<Nix> for f32 {
476 fn lift_to(&self) -> Arc<QTerm> {
477 nix_float_term(&format!("{self:?}"))
478 }
479}
480impl LiftTo<Nix> for f64 {
481 fn lift_to(&self) -> Arc<QTerm> {
482 nix_float_term(&format!("{self:?}"))
483 }
484}
485pub fn nix_bool_term(b: bool) -> Arc<QTerm> {
489 if b {
490 tb("variable_expression").c(&leaf("identifier", "true")).b()
491 } else {
492 tb("variable_expression")
493 .c(&leaf("identifier", "false"))
494 .b()
495 }
496}
497impl LiftTo<Nix> for bool {
498 fn lift_to(&self) -> Arc<QTerm> {
499 nix_bool_term(*self)
500 }
501}
502pub fn nix_string_term(s: &str) -> Arc<QTerm> {
505 if s.is_empty() {
506 tb("string_expression").c(&sym("\"")).c(&sym("\"")).b()
507 } else {
508 tb("string_expression")
509 .c(&sym("\""))
510 .c(&leaf("string_fragment", &nix_dquote_escape(s)))
511 .c(&sym("\""))
512 .b()
513 }
514}
515impl LiftTo<Nix> for str {
516 fn lift_to(&self) -> Arc<QTerm> {
517 nix_string_term(self)
518 }
519}
520impl LiftTo<Nix> for String {
521 fn lift_to(&self) -> Arc<QTerm> {
522 nix_string_term(self)
523 }
524}
525pub fn nix_list_term(items: &[Arc<QTerm>]) -> Arc<QTerm> {
530 let mut b = tb("list_expression");
531 b.child(&sym("["));
532 b.write(" ");
533 for (i, x) in items.iter().enumerate() {
534 if i > 0 {
535 b.write(" ");
536 }
537 b.child(x);
538 }
539 if !items.is_empty() {
540 b.write(" ");
541 }
542 b.child(&sym("]"));
543 b.b()
544}
545impl<T: LiftTo<Nix>> LiftTo<Nix> for [T] {
546 fn lift_to(&self) -> Arc<QTerm> {
547 nix_list_term(&self.iter().map(|x| x.lift_to()).collect::<Vec<_>>())
548 }
549}
550impl<T: LiftTo<Nix>> LiftTo<Nix> for Vec<T> {
551 fn lift_to(&self) -> Arc<QTerm> {
552 self.as_slice().lift_to()
553 }
554}
555pub fn lean_num_term(negative: bool, text: &str) -> Arc<QTerm> {
559 if negative {
560 tb("unary_op").c(&sym("-")).c(&leaf("num_lit", text)).b()
561 } else {
562 leaf("num_lit", text)
563 }
564}
565impl LiftTo<Lean> for u8 {
566 fn lift_to(&self) -> Arc<QTerm> {
567 lean_num_term(false, &self.to_string())
568 }
569}
570impl LiftTo<Lean> for u16 {
571 fn lift_to(&self) -> Arc<QTerm> {
572 lean_num_term(false, &self.to_string())
573 }
574}
575impl LiftTo<Lean> for u32 {
576 fn lift_to(&self) -> Arc<QTerm> {
577 lean_num_term(false, &self.to_string())
578 }
579}
580impl LiftTo<Lean> for u64 {
581 fn lift_to(&self) -> Arc<QTerm> {
582 lean_num_term(false, &self.to_string())
583 }
584}
585impl LiftTo<Lean> for u128 {
586 fn lift_to(&self) -> Arc<QTerm> {
587 lean_num_term(false, &self.to_string())
588 }
589}
590impl LiftTo<Lean> for usize {
591 fn lift_to(&self) -> Arc<QTerm> {
592 lean_num_term(false, &self.to_string())
593 }
594}
595impl LiftTo<Lean> for i8 {
596 fn lift_to(&self) -> Arc<QTerm> {
597 lean_num_term(*self < 0, &self.unsigned_abs().to_string())
598 }
599}
600impl LiftTo<Lean> for i16 {
601 fn lift_to(&self) -> Arc<QTerm> {
602 lean_num_term(*self < 0, &self.unsigned_abs().to_string())
603 }
604}
605impl LiftTo<Lean> for i32 {
606 fn lift_to(&self) -> Arc<QTerm> {
607 lean_num_term(*self < 0, &self.unsigned_abs().to_string())
608 }
609}
610impl LiftTo<Lean> for i64 {
611 fn lift_to(&self) -> Arc<QTerm> {
612 lean_num_term(*self < 0, &self.unsigned_abs().to_string())
613 }
614}
615impl LiftTo<Lean> for i128 {
616 fn lift_to(&self) -> Arc<QTerm> {
617 lean_num_term(*self < 0, &self.unsigned_abs().to_string())
618 }
619}
620impl LiftTo<Lean> for isize {
621 fn lift_to(&self) -> Arc<QTerm> {
622 lean_num_term(*self < 0, &self.unsigned_abs().to_string())
623 }
624}
625pub fn lean_float_term(negative: bool, text: &str) -> Arc<QTerm> {
627 if negative {
628 tb("unary_op")
629 .c(&sym("-"))
630 .c(&leaf("scientific_lit", text))
631 .b()
632 } else {
633 leaf("scientific_lit", text)
634 }
635}
636impl LiftTo<Lean> for f32 {
637 fn lift_to(&self) -> Arc<QTerm> {
638 {
639 let s = format!("{self:?}");
640 match s.strip_prefix('-') {
641 Some(mag) => lean_float_term(true, mag),
642 None => lean_float_term(false, &s),
643 }
644 }
645 }
646}
647impl LiftTo<Lean> for f64 {
648 fn lift_to(&self) -> Arc<QTerm> {
649 {
650 let s = format!("{self:?}");
651 match s.strip_prefix('-') {
652 Some(mag) => lean_float_term(true, mag),
653 None => lean_float_term(false, &s),
654 }
655 }
656 }
657}
658pub fn lean_bool_term(b: bool) -> Arc<QTerm> {
660 if b {
661 tb("true_const").c(&sym("true")).b()
662 } else {
663 tb("false_const").c(&sym("false")).b()
664 }
665}
666impl LiftTo<Lean> for bool {
667 fn lift_to(&self) -> Arc<QTerm> {
668 lean_bool_term(*self)
669 }
670}
671pub fn lean_string_term(s: &str) -> Arc<QTerm> {
674 if s.is_empty() {
675 tb("str_lit").c(&sym("\"")).c(&sym("\"")).b()
676 } else {
677 tb("str_lit")
678 .c(&sym("\""))
679 .w(&lean_dquote_escape(s))
680 .c(&sym("\""))
681 .b()
682 }
683}
684impl LiftTo<Lean> for str {
685 fn lift_to(&self) -> Arc<QTerm> {
686 lean_string_term(self)
687 }
688}
689impl LiftTo<Lean> for String {
690 fn lift_to(&self) -> Arc<QTerm> {
691 lean_string_term(self)
692 }
693}
694pub fn lean_list_term(items: &[Arc<QTerm>]) -> Arc<QTerm> {
699 let mut b = tb("list_lit");
700 b.child(&sym("["));
701 for (i, x) in items.iter().enumerate() {
702 if i > 0 {
703 b.child(&sym(","));
704 b.write(" ");
705 }
706 b.child(x);
707 }
708 b.child(&sym("]"));
709 b.b()
710}
711pub fn sql_number_term(negative: bool, text: &str) -> Arc<QTerm> {
717 if negative {
718 tb("literal").c(&sym("-")).w(text).b()
719 } else {
720 leaf("literal", text)
721 }
722}
723impl LiftTo<Sql> for u8 {
724 fn lift_to(&self) -> Arc<QTerm> {
725 sql_number_term(false, &self.to_string())
726 }
727}
728impl LiftTo<Sql> for u16 {
729 fn lift_to(&self) -> Arc<QTerm> {
730 sql_number_term(false, &self.to_string())
731 }
732}
733impl LiftTo<Sql> for u32 {
734 fn lift_to(&self) -> Arc<QTerm> {
735 sql_number_term(false, &self.to_string())
736 }
737}
738impl LiftTo<Sql> for u64 {
739 fn lift_to(&self) -> Arc<QTerm> {
740 sql_number_term(false, &self.to_string())
741 }
742}
743impl LiftTo<Sql> for u128 {
744 fn lift_to(&self) -> Arc<QTerm> {
745 sql_number_term(false, &self.to_string())
746 }
747}
748impl LiftTo<Sql> for usize {
749 fn lift_to(&self) -> Arc<QTerm> {
750 sql_number_term(false, &self.to_string())
751 }
752}
753impl LiftTo<Sql> for i8 {
754 fn lift_to(&self) -> Arc<QTerm> {
755 sql_number_term(*self < 0, &self.unsigned_abs().to_string())
756 }
757}
758impl LiftTo<Sql> for i16 {
759 fn lift_to(&self) -> Arc<QTerm> {
760 sql_number_term(*self < 0, &self.unsigned_abs().to_string())
761 }
762}
763impl LiftTo<Sql> for i32 {
764 fn lift_to(&self) -> Arc<QTerm> {
765 sql_number_term(*self < 0, &self.unsigned_abs().to_string())
766 }
767}
768impl LiftTo<Sql> for i64 {
769 fn lift_to(&self) -> Arc<QTerm> {
770 sql_number_term(*self < 0, &self.unsigned_abs().to_string())
771 }
772}
773impl LiftTo<Sql> for i128 {
774 fn lift_to(&self) -> Arc<QTerm> {
775 sql_number_term(*self < 0, &self.unsigned_abs().to_string())
776 }
777}
778impl LiftTo<Sql> for isize {
779 fn lift_to(&self) -> Arc<QTerm> {
780 sql_number_term(*self < 0, &self.unsigned_abs().to_string())
781 }
782}
783impl LiftTo<Sql> for f32 {
784 fn lift_to(&self) -> Arc<QTerm> {
785 {
786 let s = format!("{self:?}");
787 match s.strip_prefix('-') {
788 Some(mag) => sql_number_term(true, mag),
789 None => sql_number_term(false, &s),
790 }
791 }
792 }
793}
794impl LiftTo<Sql> for f64 {
795 fn lift_to(&self) -> Arc<QTerm> {
796 {
797 let s = format!("{self:?}");
798 match s.strip_prefix('-') {
799 Some(mag) => sql_number_term(true, mag),
800 None => sql_number_term(false, &s),
801 }
802 }
803 }
804}
805pub fn sql_bool_term(b: bool) -> Arc<QTerm> {
808 if b {
809 tb("literal").c(&leaf("keyword_true", "TRUE")).b()
810 } else {
811 tb("literal").c(&leaf("keyword_false", "FALSE")).b()
812 }
813}
814impl LiftTo<Sql> for bool {
815 fn lift_to(&self) -> Arc<QTerm> {
816 sql_bool_term(*self)
817 }
818}
819pub fn sql_string_term(s: &str) -> Arc<QTerm> {
824 leaf("literal", &format!("'{}'", sql_squote_escape(s)))
825}
826impl LiftTo<Sql> for str {
827 fn lift_to(&self) -> Arc<QTerm> {
828 sql_string_term(self)
829 }
830}
831impl LiftTo<Sql> for String {
832 fn lift_to(&self) -> Arc<QTerm> {
833 sql_string_term(self)
834 }
835}
836pub fn sql_list_term(items: &[Arc<QTerm>]) -> Arc<QTerm> {
841 let mut b = tb("list");
842 b.child(&sym("("));
843 for (i, x) in items.iter().enumerate() {
844 if i > 0 {
845 b.child(&sym(","));
846 b.write(" ");
847 }
848 b.child(x);
849 }
850 b.child(&sym(")"));
851 b.b()
852}
853impl<T: LiftTo<Sql>> LiftTo<Sql> for [T] {
854 fn lift_to(&self) -> Arc<QTerm> {
855 sql_list_term(&self.iter().map(|x| x.lift_to()).collect::<Vec<_>>())
856 }
857}
858impl<T: LiftTo<Sql>> LiftTo<Sql> for Vec<T> {
859 fn lift_to(&self) -> Arc<QTerm> {
860 self.as_slice().lift_to()
861 }
862}
863impl LiftTo<MySql> for u8 {
864 fn lift_to(&self) -> Arc<QTerm> {
865 sql_number_term(false, &self.to_string())
866 }
867}
868impl LiftTo<MySql> for u16 {
869 fn lift_to(&self) -> Arc<QTerm> {
870 sql_number_term(false, &self.to_string())
871 }
872}
873impl LiftTo<MySql> for u32 {
874 fn lift_to(&self) -> Arc<QTerm> {
875 sql_number_term(false, &self.to_string())
876 }
877}
878impl LiftTo<MySql> for u64 {
879 fn lift_to(&self) -> Arc<QTerm> {
880 sql_number_term(false, &self.to_string())
881 }
882}
883impl LiftTo<MySql> for u128 {
884 fn lift_to(&self) -> Arc<QTerm> {
885 sql_number_term(false, &self.to_string())
886 }
887}
888impl LiftTo<MySql> for usize {
889 fn lift_to(&self) -> Arc<QTerm> {
890 sql_number_term(false, &self.to_string())
891 }
892}
893impl LiftTo<MySql> for i8 {
894 fn lift_to(&self) -> Arc<QTerm> {
895 sql_number_term(*self < 0, &self.unsigned_abs().to_string())
896 }
897}
898impl LiftTo<MySql> for i16 {
899 fn lift_to(&self) -> Arc<QTerm> {
900 sql_number_term(*self < 0, &self.unsigned_abs().to_string())
901 }
902}
903impl LiftTo<MySql> for i32 {
904 fn lift_to(&self) -> Arc<QTerm> {
905 sql_number_term(*self < 0, &self.unsigned_abs().to_string())
906 }
907}
908impl LiftTo<MySql> for i64 {
909 fn lift_to(&self) -> Arc<QTerm> {
910 sql_number_term(*self < 0, &self.unsigned_abs().to_string())
911 }
912}
913impl LiftTo<MySql> for i128 {
914 fn lift_to(&self) -> Arc<QTerm> {
915 sql_number_term(*self < 0, &self.unsigned_abs().to_string())
916 }
917}
918impl LiftTo<MySql> for isize {
919 fn lift_to(&self) -> Arc<QTerm> {
920 sql_number_term(*self < 0, &self.unsigned_abs().to_string())
921 }
922}
923impl LiftTo<MySql> for f32 {
924 fn lift_to(&self) -> Arc<QTerm> {
925 {
926 let s = format!("{self:?}");
927 match s.strip_prefix('-') {
928 Some(mag) => sql_number_term(true, mag),
929 None => sql_number_term(false, &s),
930 }
931 }
932 }
933}
934impl LiftTo<MySql> for f64 {
935 fn lift_to(&self) -> Arc<QTerm> {
936 {
937 let s = format!("{self:?}");
938 match s.strip_prefix('-') {
939 Some(mag) => sql_number_term(true, mag),
940 None => sql_number_term(false, &s),
941 }
942 }
943 }
944}
945impl LiftTo<MySql> for bool {
946 fn lift_to(&self) -> Arc<QTerm> {
947 sql_bool_term(*self)
948 }
949}
950pub fn mysql_string_term(s: &str) -> Arc<QTerm> {
954 leaf("literal", &format!("'{}'", mysql_squote_escape(s)))
955}
956impl LiftTo<MySql> for str {
957 fn lift_to(&self) -> Arc<QTerm> {
958 mysql_string_term(self)
959 }
960}
961impl LiftTo<MySql> for String {
962 fn lift_to(&self) -> Arc<QTerm> {
963 mysql_string_term(self)
964 }
965}
966impl<T: LiftTo<MySql>> LiftTo<MySql> for [T] {
967 fn lift_to(&self) -> Arc<QTerm> {
968 sql_list_term(&self.iter().map(|x| x.lift_to()).collect::<Vec<_>>())
969 }
970}
971impl<T: LiftTo<MySql>> LiftTo<MySql> for Vec<T> {
972 fn lift_to(&self) -> Arc<QTerm> {
973 self.as_slice().lift_to()
974 }
975}
976pub fn ts_number_term(negative: bool, text: &str) -> Arc<QTerm> {
983 if negative {
984 tb("unary_expression")
985 .c(&sym("-"))
986 .c(&leaf("number", text))
987 .b()
988 } else {
989 leaf("number", text)
990 }
991}
992impl LiftTo<TypeScript> for u8 {
993 fn lift_to(&self) -> Arc<QTerm> {
994 ts_number_term(false, &self.to_string())
995 }
996}
997impl LiftTo<TypeScript> for u16 {
998 fn lift_to(&self) -> Arc<QTerm> {
999 ts_number_term(false, &self.to_string())
1000 }
1001}
1002impl LiftTo<TypeScript> for u32 {
1003 fn lift_to(&self) -> Arc<QTerm> {
1004 ts_number_term(false, &self.to_string())
1005 }
1006}
1007impl LiftTo<TypeScript> for u64 {
1008 fn lift_to(&self) -> Arc<QTerm> {
1009 ts_number_term(false, &self.to_string())
1010 }
1011}
1012impl LiftTo<TypeScript> for u128 {
1013 fn lift_to(&self) -> Arc<QTerm> {
1014 ts_number_term(false, &self.to_string())
1015 }
1016}
1017impl LiftTo<TypeScript> for usize {
1018 fn lift_to(&self) -> Arc<QTerm> {
1019 ts_number_term(false, &self.to_string())
1020 }
1021}
1022impl LiftTo<TypeScript> for i8 {
1023 fn lift_to(&self) -> Arc<QTerm> {
1024 ts_number_term(*self < 0, &self.unsigned_abs().to_string())
1025 }
1026}
1027impl LiftTo<TypeScript> for i16 {
1028 fn lift_to(&self) -> Arc<QTerm> {
1029 ts_number_term(*self < 0, &self.unsigned_abs().to_string())
1030 }
1031}
1032impl LiftTo<TypeScript> for i32 {
1033 fn lift_to(&self) -> Arc<QTerm> {
1034 ts_number_term(*self < 0, &self.unsigned_abs().to_string())
1035 }
1036}
1037impl LiftTo<TypeScript> for i64 {
1038 fn lift_to(&self) -> Arc<QTerm> {
1039 ts_number_term(*self < 0, &self.unsigned_abs().to_string())
1040 }
1041}
1042impl LiftTo<TypeScript> for i128 {
1043 fn lift_to(&self) -> Arc<QTerm> {
1044 ts_number_term(*self < 0, &self.unsigned_abs().to_string())
1045 }
1046}
1047impl LiftTo<TypeScript> for isize {
1048 fn lift_to(&self) -> Arc<QTerm> {
1049 ts_number_term(*self < 0, &self.unsigned_abs().to_string())
1050 }
1051}
1052impl LiftTo<TypeScript> for f32 {
1053 fn lift_to(&self) -> Arc<QTerm> {
1054 {
1055 let s = format!("{self:?}");
1056 match s.strip_prefix('-') {
1057 Some(mag) => ts_number_term(true, mag),
1058 None => ts_number_term(false, &s),
1059 }
1060 }
1061 }
1062}
1063impl LiftTo<TypeScript> for f64 {
1064 fn lift_to(&self) -> Arc<QTerm> {
1065 {
1066 let s = format!("{self:?}");
1067 match s.strip_prefix('-') {
1068 Some(mag) => ts_number_term(true, mag),
1069 None => ts_number_term(false, &s),
1070 }
1071 }
1072 }
1073}
1074pub fn ts_bool_term(b: bool) -> Arc<QTerm> {
1076 if b {
1077 sym("true")
1078 } else {
1079 sym("false")
1080 }
1081}
1082impl LiftTo<TypeScript> for bool {
1083 fn lift_to(&self) -> Arc<QTerm> {
1084 ts_bool_term(*self)
1085 }
1086}
1087pub fn ts_string_term(s: &str) -> Arc<QTerm> {
1090 if s.is_empty() {
1091 tb("string").c(&sym("\"")).c(&sym("\"")).b()
1092 } else {
1093 tb("string")
1094 .c(&sym("\""))
1095 .c(&leaf("string_fragment", &ts_dquote_escape(s)))
1096 .c(&sym("\""))
1097 .b()
1098 }
1099}
1100impl LiftTo<TypeScript> for str {
1101 fn lift_to(&self) -> Arc<QTerm> {
1102 ts_string_term(self)
1103 }
1104}
1105impl LiftTo<TypeScript> for String {
1106 fn lift_to(&self) -> Arc<QTerm> {
1107 ts_string_term(self)
1108 }
1109}
1110pub fn ts_list_term(items: &[Arc<QTerm>]) -> Arc<QTerm> {
1115 let mut b = tb("array");
1116 b.child(&sym("["));
1117 for (i, x) in items.iter().enumerate() {
1118 if i > 0 {
1119 b.child(&sym(","));
1120 b.write(" ");
1121 }
1122 b.child(x);
1123 }
1124 b.child(&sym("]"));
1125 b.b()
1126}
1127impl<T: LiftTo<TypeScript>> LiftTo<TypeScript> for [T] {
1128 fn lift_to(&self) -> Arc<QTerm> {
1129 ts_list_term(&self.iter().map(|x| x.lift_to()).collect::<Vec<_>>())
1130 }
1131}
1132impl<T: LiftTo<TypeScript>> LiftTo<TypeScript> for Vec<T> {
1133 fn lift_to(&self) -> Arc<QTerm> {
1134 self.as_slice().lift_to()
1135 }
1136}
1137pub fn html_text_term(text: &str) -> Arc<QTerm> {
1142 leaf("text", text)
1143}
1144impl LiftTo<Html> for u8 {
1145 fn lift_to(&self) -> Arc<QTerm> {
1146 html_text_term(&self.to_string())
1147 }
1148}
1149impl LiftTo<Html> for u16 {
1150 fn lift_to(&self) -> Arc<QTerm> {
1151 html_text_term(&self.to_string())
1152 }
1153}
1154impl LiftTo<Html> for u32 {
1155 fn lift_to(&self) -> Arc<QTerm> {
1156 html_text_term(&self.to_string())
1157 }
1158}
1159impl LiftTo<Html> for u64 {
1160 fn lift_to(&self) -> Arc<QTerm> {
1161 html_text_term(&self.to_string())
1162 }
1163}
1164impl LiftTo<Html> for u128 {
1165 fn lift_to(&self) -> Arc<QTerm> {
1166 html_text_term(&self.to_string())
1167 }
1168}
1169impl LiftTo<Html> for usize {
1170 fn lift_to(&self) -> Arc<QTerm> {
1171 html_text_term(&self.to_string())
1172 }
1173}
1174impl LiftTo<Html> for i8 {
1175 fn lift_to(&self) -> Arc<QTerm> {
1176 html_text_term(&self.to_string())
1177 }
1178}
1179impl LiftTo<Html> for i16 {
1180 fn lift_to(&self) -> Arc<QTerm> {
1181 html_text_term(&self.to_string())
1182 }
1183}
1184impl LiftTo<Html> for i32 {
1185 fn lift_to(&self) -> Arc<QTerm> {
1186 html_text_term(&self.to_string())
1187 }
1188}
1189impl LiftTo<Html> for i64 {
1190 fn lift_to(&self) -> Arc<QTerm> {
1191 html_text_term(&self.to_string())
1192 }
1193}
1194impl LiftTo<Html> for i128 {
1195 fn lift_to(&self) -> Arc<QTerm> {
1196 html_text_term(&self.to_string())
1197 }
1198}
1199impl LiftTo<Html> for isize {
1200 fn lift_to(&self) -> Arc<QTerm> {
1201 html_text_term(&self.to_string())
1202 }
1203}
1204impl LiftTo<Html> for bool {
1205 fn lift_to(&self) -> Arc<QTerm> {
1206 html_text_term(&self.to_string())
1207 }
1208}
1209impl LiftTo<Html> for f32 {
1210 fn lift_to(&self) -> Arc<QTerm> {
1211 html_text_term(&format!("{self:?}"))
1212 }
1213}
1214impl LiftTo<Html> for f64 {
1215 fn lift_to(&self) -> Arc<QTerm> {
1216 html_text_term(&format!("{self:?}"))
1217 }
1218}
1219impl LiftTo<Html> for str {
1220 fn lift_to(&self) -> Arc<QTerm> {
1221 html_text_term(&escape_html(self))
1222 }
1223}
1224impl LiftTo<Html> for String {
1225 fn lift_to(&self) -> Arc<QTerm> {
1226 html_text_term(&escape_html(self))
1227 }
1228}
1229impl<T: LiftTo<Lean>> LiftTo<Lean> for [T] {
1230 fn lift_to(&self) -> Arc<QTerm> {
1231 lean_list_term(&self.iter().map(|x| x.lift_to()).collect::<Vec<_>>())
1232 }
1233}
1234impl<T: LiftTo<Lean>> LiftTo<Lean> for Vec<T> {
1235 fn lift_to(&self) -> Arc<QTerm> {
1236 self.as_slice().lift_to()
1237 }
1238}