spreadsheet_ods_formula/
lib.rs

1//!
2//! This library provides functions that map to ODS-functions.
3//!
4//! ```
5//! use spreadsheet_ods::{cell, CellRef};
6//! use spreadsheet_ods_formula::{ formula, of};
7//!
8//! let f = formula(of::sin(cell!(0,0)));
9//!
10//! assert_eq!(f, "of:=SIN([.A1])");
11//! ```
12//!
13//! All functions use traits to somewhat constrain the allowed parameter-types. There are quite a
14//! lot of traits to map the possible parameter types of ods-functions. By the nature of
15//! spreadsheets this is a rather loose mapping anyway.
16//!
17//! * Basic types ixx, uxx, bool, str, String and `Cow<str>` have the appropriate traits.
18//!   The common operators are overloaded too. With the caveat that the first parameter
19//!   must be created via the num() function.
20//!
21//! * Expressions in parentheses must use the p()-function.
22//!
23//! ```
24//! use spreadsheet_ods_formula::{formula, num, of, p};
25//!
26//! let f = formula(num(33) * p(of::sqrt(2) + of::sqrt(3)));
27//! ```
28//!
29//! * CellRef and CellRange are imported from spreadsheet-ods. There is also a cell!() macro.
30//!
31//! ```
32//! use spreadsheet_ods::cell;
33//! use spreadsheet_ods_formula::{formula};
34//!
35//! let f = formula(cell!(0, 0));
36//! println!("{}", f);
37//! let f = formula(cell!("Table1" => 4, 4));
38//! println!("{}", f);
39//! let f = formula(cell!(0, 0, 4, 4));
40//! println!("{}", f);
41//! ```
42//!
43//! * ODS operators are mapped with the AnyOp, TextOp, NumberOp, LogicalOp and ReferenceOp traits.
44//!
45//! ```
46//! use spreadsheet_ods::cell;
47//! use spreadsheet_ods_formula::{formula};
48//! use spreadsheet_ods_formula::{ReferenceOp};
49//!
50//! let f = formula(cell!(0, 0, 4, 4).refcat(cell!(8, 8, 12, 12)));
51//! println!("{}", f);
52//! ```
53//!
54//! * Tuples are used for Sequences. They are formatted as inline-arrays.
55//!
56//! ```
57//! use spreadsheet_ods::cell;
58//! use spreadsheet_ods_formula::{formula, of};
59//!
60//! let f = formula(of::networkdays__(
61//!      cell!(5, 5),
62//!      cell!(9, 9),
63//!      (9, 9, 9),
64//!      (0, 0, 0, 0, 0, 1, 0),
65//! ));
66//! ```
67//!
68//! * FMatrix and FArray for inline arrays and matrizes.
69//!
70//! ```
71//! use spreadsheet_ods::cell;
72//! use spreadsheet_ods_formula::{FArray, formula, of};
73//!
74//! let f = formula(of::networkdays__(
75//!      cell!(5, 5),
76//!      cell!(9, 9),
77//!      FArray([9, 9, 9]),
78//!      FArray([0, 0, 0, 0, 0, 1, 0]),
79//! ));
80//! ```
81
82#![allow(clippy::too_many_arguments)]
83#![warn(absolute_paths_not_starting_with_crate)]
84// NO #![warn(box_pointers)]
85#![warn(elided_lifetimes_in_paths)]
86#![warn(explicit_outlives_requirements)]
87#![warn(keyword_idents)]
88#![warn(macro_use_extern_crate)]
89#![warn(meta_variable_misuse)]
90#![warn(missing_abi)]
91// NOT_ACCURATE #![warn(missing_copy_implementations)]
92// #![warn(missing_debug_implementations)]
93// #![warn(missing_docs)]
94// NO #![warn(non_ascii_idents)]
95#![warn(noop_method_call)]
96// NO #![warn(or_patterns_back_compat)]
97#![warn(semicolon_in_expressions_from_macros)]
98// NOT_ACCURATE #![warn(single_use_lifetimes)]
99#![warn(trivial_casts)]
100#![warn(trivial_numeric_casts)]
101#![warn(unreachable_pub)]
102// #![warn(unsafe_code)]
103#![warn(unsafe_op_in_unsafe_fn)]
104#![warn(unstable_features)]
105// NO #![warn(unused_crate_dependencies)]
106// NO #![warn(unused_extern_crates)]
107#![warn(unused_import_braces)]
108#![warn(unused_lifetimes)]
109#![warn(unused_qualifications)]
110// NO #![warn(unused_results)]
111#![warn(variant_size_differences)]
112
113use spreadsheet_ods::{CellRange, CellRef};
114use std::borrow::Borrow;
115use std::borrow::Cow;
116use std::fmt::{Display, Formatter, Write};
117use std::ops::{Add, BitAnd, BitXor, Div, Mul, Neg, Sub};
118
119mod generated;
120
121pub mod cmp;
122pub mod op;
123
124pub mod bit;
125pub mod complex;
126pub mod conv;
127pub mod date;
128pub mod db;
129pub mod ext;
130pub mod fin;
131pub mod info;
132pub mod logic;
133pub mod lookup;
134pub mod math;
135pub mod matrix;
136pub mod round;
137pub mod stat;
138pub mod text;
139pub mod textb;
140
141/// The traits for this crate.
142/// And the function p() for parentheses.
143pub mod prelude {
144    pub use crate::Any;
145    pub use crate::{AnyOp, LogicalOp, NumberOp, ReferenceOp, TextOp};
146}
147
148/// All functions in one module, and also all families of functions.
149pub mod of {
150    #[doc(inline)]
151    pub use crate::cmp::*;
152    #[doc(inline)]
153    pub use crate::op::*;
154
155    #[doc(inline)]
156    pub use crate::bit::*;
157    #[doc(inline)]
158    pub use crate::complex::*;
159    #[doc(inline)]
160    pub use crate::conv::*;
161    #[doc(inline)]
162    pub use crate::date::*;
163    #[doc(inline)]
164    pub use crate::db::*;
165    #[doc(inline)]
166    pub use crate::ext::*;
167    #[doc(inline)]
168    pub use crate::fin::*;
169    #[doc(inline)]
170    pub use crate::info::*;
171    #[doc(inline)]
172    pub use crate::logic::*;
173    #[doc(inline)]
174    pub use crate::lookup::*;
175    #[doc(inline)]
176    pub use crate::math::*;
177    #[doc(inline)]
178    pub use crate::matrix::*;
179    #[doc(inline)]
180    pub use crate::round::*;
181    #[doc(inline)]
182    pub use crate::stat::*;
183    #[doc(inline)]
184    pub use crate::text::*;
185    #[doc(inline)]
186    pub use crate::textb::*;
187
188    #[doc(inline)]
189    pub use crate::{
190        bit, complex, conv, date, db, ext, fin, info, logic, lookup, math, matrix, round, stat,
191        text, textb,
192    };
193    #[doc(inline)]
194    pub use crate::{cmp, op};
195}
196
197// -----------------------------------------------------------------------
198
199/// Trait for any part of the formula.
200///
201/// This trait is used to create the string-repr of the formula.
202pub trait Any {
203    /// Output to a formula.
204    fn formula(&self, buf: &mut String);
205}
206
207/// Numeric parameter.
208pub trait Number: Any {}
209/// Text parameter.
210pub trait Text: Any {}
211/// Logical parameter.
212pub trait Logical: Any {}
213/// A date/time parameter.
214pub trait DateTime: Any {}
215/// Reference parameter.
216pub trait Reference: Any {}
217/// Matrix parameter.
218pub trait Matrix: Any {}
219/// Array parameter.
220pub trait Array: Any {}
221
222/// Alias for a cell reference. A cell range containing headers and a data set.
223pub trait Database: Any {}
224/// Filter/Search criterion.
225pub trait Criterion: Any {}
226/// A cell range containing headers and filters.
227pub trait Criteria: Any {}
228/// Sequence of values.
229pub trait Sequence: Any {}
230/// A single scalar value.
231pub trait Scalar: Any {}
232/// A field denominator for a db.
233pub trait Field: Any {}
234
235/// Text or Number parameter.
236pub trait TextOrNumber: Any {}
237/// Reference or Array parameter.
238pub trait ReferenceOrArray: Any {}
239/// Reference or Text parameter.
240pub trait TextOrReference: Any {}
241/// Number or Array parameter.
242pub trait NumberOrArray: Any {}
243
244// -----------------------------------------------------------------------
245
246/// Comparision operators
247pub trait AnyOp<T: Any> {
248    /// equal
249    fn eq<U: Any>(self, other: U) -> OpLogical<T, U>;
250    /// not equal
251    fn ne<U: Any>(self, other: U) -> OpLogical<T, U>;
252    /// less than
253    fn lt<U: Any>(self, other: U) -> OpLogical<T, U>;
254    /// less than or equal
255    fn le<U: Any>(self, other: U) -> OpLogical<T, U>;
256    /// greater than
257    fn gt<U: Any>(self, other: U) -> OpLogical<T, U>;
258    /// greater than or equal
259    fn ge<U: Any>(self, other: U) -> OpLogical<T, U>;
260}
261
262/// Operations on number-like values.
263pub trait NumberOp<T: Any> {
264    /// add
265    fn add<U: Number>(self, other: U) -> OpNumber<T, U>;
266    /// subtract
267    fn sub<U: Number>(self, other: U) -> OpNumber<T, U>;
268    /// multiply
269    fn mul<U: Number>(self, other: U) -> OpNumber<T, U>;
270    /// divide
271    fn div<U: Number>(self, other: U) -> OpNumber<T, U>;
272    /// exponential
273    fn pow<U: Number>(self, other: U) -> OpNumber<T, U>;
274    /// as percentage
275    fn percent(self) -> OpNumber<T, ()>;
276}
277
278/// Operations on text-like values.
279pub trait TextOp<T: Any> {
280    /// concat text
281    fn concat<U: Text>(self, other: U) -> OpText<T, U>;
282}
283
284/// Operations on boolean-like values.
285pub trait LogicalOp<T: Any> {
286    /// and
287    fn and<U: Logical>(self, other: U) -> FnLogical2<T, U>;
288    /// or
289    fn or<U: Logical>(self, other: U) -> FnLogical2<T, U>;
290    /// xor
291    fn xor<U: Logical>(self, other: U) -> FnLogical2<T, U>;
292}
293
294/// Operations on references.
295pub trait ReferenceOp<T: Any> {
296    /// intersection of references
297    fn intersect<U: Reference>(self, other: U) -> OpReference<T, U>;
298    /// concatenation of references
299    fn refcat<U: Reference>(self, other: U) -> OpReference<T, U>;
300}
301
302// -----------------------------------------------------------------------
303
304impl<T: Any> AnyOp<T> for T {
305    #[inline]
306    fn eq<U: Any>(self, other: U) -> OpLogical<T, U> {
307        OpLogical(self, "=", other)
308    }
309
310    #[inline]
311    fn ne<U: Any>(self, other: U) -> OpLogical<T, U> {
312        OpLogical(self, "<>", other)
313    }
314
315    #[inline]
316    fn lt<U: Any>(self, other: U) -> OpLogical<T, U> {
317        OpLogical(self, "<", other)
318    }
319
320    #[inline]
321    fn le<U: Any>(self, other: U) -> OpLogical<T, U> {
322        OpLogical(self, "<=", other)
323    }
324
325    #[inline]
326    fn gt<U: Any>(self, other: U) -> OpLogical<T, U> {
327        OpLogical(self, ">", other)
328    }
329
330    #[inline]
331    fn ge<U: Any>(self, other: U) -> OpLogical<T, U> {
332        OpLogical(self, ">=", other)
333    }
334}
335
336impl<T: Number> NumberOp<T> for T {
337    #[inline]
338    fn add<U: Number>(self, other: U) -> OpNumber<T, U> {
339        OpNumber(self, "+", other)
340    }
341
342    #[inline]
343    fn sub<U: Number>(self, other: U) -> OpNumber<T, U> {
344        OpNumber(self, "-", other)
345    }
346
347    #[inline]
348    fn mul<U: Number>(self, other: U) -> OpNumber<T, U> {
349        OpNumber(self, "*", other)
350    }
351
352    #[inline]
353    fn div<U: Number>(self, other: U) -> OpNumber<T, U> {
354        OpNumber(self, "/", other)
355    }
356
357    #[inline]
358    fn pow<U: Number>(self, other: U) -> OpNumber<T, U> {
359        OpNumber(self, "^", other)
360    }
361
362    #[inline]
363    fn percent(self) -> OpNumber<T, ()> {
364        OpNumber(self, "%", ())
365    }
366}
367
368impl<T: Text> TextOp<T> for T {
369    #[inline]
370    fn concat<U: Text>(self, other: U) -> OpText<T, U> {
371        OpText(self, "&", other)
372    }
373}
374
375impl<T: Logical> LogicalOp<T> for T {
376    #[inline]
377    fn and<U: Logical>(self, other: U) -> FnLogical2<T, U> {
378        FnLogical2("AND", self, other)
379    }
380
381    #[inline]
382    fn or<U: Logical>(self, other: U) -> FnLogical2<T, U> {
383        FnLogical2("OR", self, other)
384    }
385
386    #[inline]
387    fn xor<U: Logical>(self, other: U) -> FnLogical2<T, U> {
388        FnLogical2("XOR", self, other)
389    }
390}
391
392impl<T: Reference> ReferenceOp<T> for T {
393    #[inline]
394    fn intersect<U: Reference>(self, other: U) -> OpReference<T, U> {
395        OpReference(self, "!", other)
396    }
397
398    #[inline]
399    fn refcat<U: Reference>(self, other: U) -> OpReference<T, U> {
400        OpReference(self, "~", other)
401    }
402}
403
404// -----------------------------------------------------------------------
405
406macro_rules! any_struct {
407    (VAL $t:ident) => {
408
409        /// A newtype wrapper for a simple value.
410        /// Useful in combination with overloaded operators.
411        #[doc(hidden)]
412        #[derive(Debug)]
413        pub struct $t<A:Any>(pub A);
414
415        impl<A:Any> Any for $t<A> {
416             #[inline]
417             fn formula(&self, buf: &mut String) {
418                self.0.formula(buf);
419            }
420        }
421
422    };
423    (OP $t:ident) => {
424
425        /// Operator definition.
426        #[doc(hidden)]
427        #[derive(Debug)]
428        pub struct $t<A:Any, B:Any>(
429            pub A,
430            pub &'static str,
431            pub B
432        );
433
434        impl<A:Any, B: Any> Any for $t<A,B> {
435            #[inline]
436            fn formula(&self, buf: &mut String) {
437                self.0.formula(buf);
438                buf.push_str(self.1.as_ref());
439                self.2.formula(buf);
440            }
441        }
442
443    };
444    (VAR $t:ident) => {
445
446        /// Function with variable number of parameters.
447        #[doc(hidden)]
448        pub struct $t(
449            pub &'static str,
450            pub Vec<Box<dyn Any>>
451        );
452
453        impl Any for $t {
454            #[inline]
455            fn formula(&self, buf: &mut String) {
456                buf.push_str(self.0);
457                buf.push('(');
458                for (i, v) in self.1.iter().enumerate() {
459                    if i > 0 {
460                        buf.push(';');
461                    }
462                    let _ = v.formula(buf);
463                }
464                buf.push(')');
465            }
466        }
467
468    };
469    ($t:ident) => {
470
471        /// Parameterless function.
472        #[derive(Debug)]
473        #[doc(hidden)]
474        pub struct $t(
475            pub &'static str
476        );
477
478        impl Any for $t {
479            #[inline]
480            fn formula(&self, buf: &mut String) {
481                buf.push_str(self.0.as_ref());
482                buf.push('(');
483                buf.push(')');
484            }
485        }
486
487    };
488    ($t:ident : $tname0:tt $($tname:tt $tidx:tt)*) => {
489
490        /// Function with parameters.
491        #[derive(Debug)]
492        #[doc(hidden)]
493        pub struct $t<$tname0: Any+'static $(,$tname: Any+'static)*>(
494            pub &'static str,
495            pub $tname0
496            $(, pub $tname)*
497        );
498
499        impl <$tname0: Any+'static $(,$tname: Any+'static)*> Any for $t<$tname0 $(,$tname)*> {
500            #[inline]
501            fn formula(&self, buf: &mut String) {
502                buf.push_str(self.0.as_ref());
503                buf.push('(');
504                self.1.formula(buf);
505                $(
506                    buf.push(';');
507                    self.$tidx.formula(buf);
508                )*
509                buf.push(')');
510            }
511        }
512
513    }
514}
515
516macro_rules! fn_any {
517    (VAL $t:ident) => {
518        any_struct!(VAL $t);
519        fn_any!(__IMPL $t: A);
520    };
521    (OP $t:ident) => {
522        any_struct!(OP $t);
523        fn_any!(__IMPL $t: A B);
524    };
525    (VAR $t:ident) => {
526        any_struct!(VAR $t);
527        fn_any!(__IMPL $t:);
528    };
529    ($t:ident) => {
530        any_struct!($t);
531        fn_any!(__IMPL $t:);
532    };
533    ($t:ident : $tname0:tt $($tname:tt $tidx:tt)*) => {
534        any_struct!($t: $tname0 $($tname $tidx)*);
535        fn_any!(__IMPL $t: $tname0 $($tname)*);
536    };
537    (__IMPL $t:ident : $($l:lifetime)?) => {
538        impl $(<$l>)? Number for $t$(<$l>)?  {}
539        impl $(<$l>)? Text for $t$(<$l>)?  {}
540        impl $(<$l>)? Logical for $t$(<$l>)?  {}
541        impl $(<$l>)? Sequence for $t$(<$l>)?  {}
542        impl $(<$l>)? Scalar for $t$(<$l>)?  {}
543        impl $(<$l>)? Field for $t$(<$l>)?  {}
544        impl $(<$l>)? DateTime for $t$(<$l>)?  {}
545        impl $(<$l>)? TextOrNumber for $t$(<$l>)?  {}
546        impl $(<$l>)? TextOrReference for $t$(<$l>)?  {}
547        impl $(<$l>)? NumberOrArray for $t$(<$l>)?  {}
548    };
549    (__IMPL $t:ident : $($l:lifetime)? $tname0:tt $($tname:tt)*) => {
550        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Number for $t<$($l, )?$tname0 $(,$tname)*> {}
551        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Text for $t<$($l, )?$tname0 $(,$tname)*> {}
552        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Logical for $t<$($l, )?$tname0 $(,$tname)*> {}
553        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Sequence for $t<$($l, )?$tname0 $(,$tname)*> {}
554        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Scalar for $t<$($l, )?$tname0 $(,$tname)*> {}
555        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Field for $t<$($l, )?$tname0 $(,$tname)*> {}
556        impl <$($l, )?$tname0: Any $(,$tname: Any)*> DateTime for $t<$($l, )?$tname0 $(,$tname)*> {}
557        impl <$($l, )?$tname0: Any $(,$tname: Any)*> TextOrNumber for $t<$($l, )?$tname0 $(,$tname)*> {}
558        impl <$($l, )?$tname0: Any $(,$tname: Any)*> TextOrReference for $t<$($l, )?$tname0 $(,$tname)*> {}
559        impl <$($l, )?$tname0: Any $(,$tname: Any)*> NumberOrArray for $t<$($l, )?$tname0 $(,$tname)*> {}
560    };
561}
562
563// fn_any!(VAL ValAny);
564// fn_any!(OP OpAny);
565fn_any!(VAR FnAnyVar);
566fn_any!(FnAny0);
567fn_any!(FnAny1: A);
568fn_any!(FnAny2: A B 2);
569fn_any!(FnAny3: A B 2 C 3);
570fn_any!(FnAny4: A B 2 C 3 D 4);
571fn_any!(FnAny5: A B 2 C 3 D 4 E 5);
572
573macro_rules! fn_number {
574    (VAL $t:ident) => {
575        any_struct!(VAL $t);
576        fn_number!(__IMPL $t: A);
577    };
578    (OP $t:ident) => {
579        any_struct!(OP $t);
580        fn_number!(__IMPL $t: A B);
581    };
582    (VAR $t:ident) => {
583        any_struct!(VAR $t);
584        fn_number!(__IMPL $t:);
585    };
586    (REFVAR $t:ident) => {
587        any_struct!(REFVAR $t);
588        fn_number!(__IMPL $t: 'a);
589    };
590    ($t:ident) => {
591        any_struct!($t);
592        fn_number!(__IMPL $t:);
593    };
594    ($t:ident : $tname0:tt $($tname:tt $tidx:tt)*) => {
595        any_struct!($t: $tname0 $($tname $tidx)*);
596        fn_number!(__IMPL $t: $tname0 $($tname)*);
597    };
598    (__IMPL $t:ident : $($l:lifetime)?) => {
599        impl $(<$l>)? Number for $t$(<$l>)?  {}
600        impl $(<$l>)? Logical for $t$(<$l>)?  {}
601        impl $(<$l>)? Sequence for $t$(<$l>)?  {}
602        impl $(<$l>)? Scalar for $t$(<$l>)?  {}
603        impl $(<$l>)? Field for $t$(<$l>)?  {}
604        impl $(<$l>)? DateTime for $t$(<$l>)?  {}
605        impl $(<$l>)? TextOrNumber for $t$(<$l>)?  {}
606        impl $(<$l>)? NumberOrArray for $t$(<$l>)?  {}
607    };
608    (__IMPL $t:ident : $($l:lifetime)? $tname0:tt $($tname:tt)*) => {
609        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Number for $t<$($l, )?$tname0 $(,$tname)*> {}
610        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Logical for $t<$($l, )?$tname0 $(,$tname)*> {}
611        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Sequence for $t<$($l, )?$tname0 $(,$tname)*> {}
612        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Scalar for $t<$($l, )?$tname0 $(,$tname)*> {}
613        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Field for $t<$($l, )?$tname0 $(,$tname)*> {}
614        impl <$($l, )?$tname0: Any $(,$tname: Any)*> DateTime for $t<$($l, )?$tname0 $(,$tname)*> {}
615        impl <$($l, )?$tname0: Any $(,$tname: Any)*> TextOrNumber for $t<$($l, )?$tname0 $(,$tname)*> {}
616        impl <$($l, )?$tname0: Any $(,$tname: Any)*> NumberOrArray for $t<$($l, )?$tname0 $(,$tname)*> {}
617    };
618}
619
620fn_number!(VAL ValNumber);
621fn_number!(OP OpNumber);
622fn_number!(VAR FnNumberVar);
623fn_number!(FnNumber0);
624fn_number!(FnNumber1: A);
625fn_number!(FnNumber2: A B 2);
626fn_number!(FnNumber3: A B 2 C 3);
627fn_number!(FnNumber4: A B 2 C 3 D 4);
628fn_number!(FnNumber5: A B 2 C 3 D 4 E 5);
629fn_number!(FnNumber6: A B 2 C 3 D 4 E 5 F 6);
630fn_number!(FnNumber7: A B 2 C 3 D 4 E 5 F 6 G 7);
631fn_number!(FnNumber8: A B 2 C 3 D 4 E 5 F 6 G 7 H 8);
632fn_number!(FnNumber9: A B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9);
633
634macro_rules! fn_text {
635    (VAL $t:ident) => {
636        any_struct!(VAL $t);
637        fn_text!(__IMPL $t: A);
638    };
639    (OP $t:ident) => {
640        any_struct!(OP $t);
641        fn_text!(__IMPL $t: A B);
642    };
643    (VAR $t:ident) => {
644        any_struct!(VAR $t);
645        fn_text!(__IMPL $t:);
646    };
647    (REFVAR $t:ident) => {
648        any_struct!(REFVAR $t);
649        fn_text!(__IMPL $t: 'a);
650    };
651    ($t:ident) => {
652        any_struct!($t);
653        fn_text!(__IMPL $t:);
654    };
655    ($t:ident : $tname0:tt $($tname:tt $tidx:tt)*) => {
656        any_struct!($t: $tname0 $($tname $tidx)*);
657        fn_text!(__IMPL $t: $tname0 $($tname)*);
658    };
659    (__IMPL $t:ident : $($l:lifetime)?) => {
660        impl $(<$l>)? Text for $t$(<$l>)?  {}
661        impl $(<$l>)? Sequence for $t$(<$l>)?  {}
662        impl $(<$l>)? Scalar for $t$(<$l>)?  {}
663        impl $(<$l>)? Field for $t$(<$l>)?  {}
664        impl $(<$l>)? DateTime for $t$(<$l>)?  {}
665        impl $(<$l>)? TextOrNumber for $t$(<$l>)?  {}
666        impl $(<$l>)? TextOrReference for $t$(<$l>)?  {}
667    };
668    (__IMPL $t:ident : $($l:lifetime)? $tname0:tt $($tname:tt)*) => {
669        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Text for $t<$($l, )?$tname0 $(,$tname)*> {}
670        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Sequence for $t<$($l, )?$tname0 $(,$tname)*> {}
671        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Scalar for $t<$($l, )?$tname0 $(,$tname)*> {}
672        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Field for $t<$($l, )?$tname0 $(,$tname)*> {}
673        impl <$($l, )?$tname0: Any $(,$tname: Any)*> DateTime for $t<$($l, )?$tname0 $(,$tname)*> {}
674        impl <$($l, )?$tname0: Any $(,$tname: Any)*> TextOrNumber for $t<$($l, )?$tname0 $(,$tname)*> {}
675        impl <$($l, )?$tname0: Any $(,$tname: Any)*> TextOrReference for $t<$($l, )?$tname0 $(,$tname)*> {}
676    };
677}
678
679// fn_text!(VAL ValText);
680fn_text!(OP OpText);
681fn_text!(VAR FnTextVar);
682// fn_text!(FnText0);
683fn_text!(FnText1: A);
684fn_text!(FnText2: A B 2);
685fn_text!(FnText3: A B 2 C 3);
686fn_text!(FnText4: A B 2 C 3 D 4);
687fn_text!(FnText5: A B 2 C 3 D 4 E 5);
688
689macro_rules! fn_logical {
690    (VAL $t:ident) => {
691        any_struct!(VAL $t);
692        fn_logical!(__IMPL $t: A);
693    };
694    (OP $t:ident) => {
695        any_struct!(OP $t);
696        fn_logical!(__IMPL $t: A B);
697    };
698    (VAR $t:ident) => {
699        any_struct!(VAR $t);
700        fn_logical!(__IMPL $t:);
701    };
702    (REFVAR $t:ident) => {
703        any_struct!(REFVAR $t);
704        fn_logical!(__IMPL $t: 'a);
705    };
706    ($t:ident) => {
707        any_struct!($t);
708        fn_logical!(__IMPL $t:);
709    };
710    ($t:ident : $tname0:tt $($tname:tt $tidx:tt)*) => {
711        any_struct!($t: $tname0 $($tname $tidx)*);
712        fn_logical!(__IMPL $t: $tname0 $($tname)*);
713    };
714    (__IMPL $t:ident : $($l:lifetime)?) => {
715        impl $(<$l>)? Number for $t$(<$l>)?  {}
716        impl $(<$l>)? Logical for $t$(<$l>)?  {}
717        impl $(<$l>)? Sequence for $t$(<$l>)?  {}
718        impl $(<$l>)? Scalar for $t$(<$l>)?  {}
719        impl $(<$l>)? TextOrNumber for $t$(<$l>)?  {}
720        impl $(<$l>)? NumberOrArray for $t$(<$l>)?  {}
721    };
722    (__IMPL $t:ident : $($l:lifetime)? $tname0:tt $($tname:tt)*) => {
723        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Number for $t<$($l, )?$tname0 $(,$tname)*> {}
724        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Logical for $t<$($l, )?$tname0 $(,$tname)*> {}
725        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Sequence for $t<$($l, )?$tname0 $(,$tname)*> {}
726        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Scalar for $t<$($l, )?$tname0 $(,$tname)*> {}
727        impl <$($l, )?$tname0: Any $(,$tname: Any)*> TextOrNumber for $t<$($l, )?$tname0 $(,$tname)*> {}
728        impl <$($l, )?$tname0: Any $(,$tname: Any)*> NumberOrArray for $t<$($l, )?$tname0 $(,$tname)*> {}
729    };
730}
731
732// fn_logical!(VAL ValLogical);
733fn_logical!(OP OpLogical);
734// fn_logical!(VAR FnLogicalVar);
735fn_logical!(FnLogical0);
736fn_logical!(FnLogical1: A);
737fn_logical!(FnLogical2: A B 2);
738
739macro_rules! fn_matrix {
740    (VAL $t:ident) => {
741        any_struct!(VAL $t);
742        fn_matrix!(__IMPL $t: A);
743    };
744    (OP $t:ident) => {
745        any_struct!(OP $t);
746        fn_matrix!(__IMPL $t: A B);
747    };
748    (VAR $t:ident) => {
749        any_struct!(VAR $t);
750        fn_matrix!(__IMPL $t:);
751    };
752    (REFVAR $t:ident) => {
753        any_struct!(REFVAR $t);
754        fn_matrix!(__IMPL $t: 'a);
755    };
756    ($t:ident) => {
757        any_struct!($t);
758        fn_matrix!(__IMPL $t:);
759    };
760    ($t:ident : $tname0:tt $($tname:tt $tidx:tt)*) => {
761        any_struct!($t: $tname0 $($tname $tidx)*);
762        fn_matrix!(__IMPL $t: $tname0 $($tname)*);
763    };
764    (__IMPL $t:ident : $($l:lifetime)?) => {
765        impl $(<$l>)? Matrix for $t$(<$l>)?  {}
766    };
767    (__IMPL $t:ident : $($l:lifetime)? $tname0:tt $($tname:tt)*) => {
768        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Matrix for $t<$($l, )?$tname0 $(,$tname)*> {}
769    };
770}
771
772// fn_matrix!(VAL ValMatrix);
773// fn_matrix!(OP OpMatrix);
774// fn_matrix!(VAR FnMatrixVar);
775// fn_matrix!(FnMatrix0);
776fn_matrix!(FnMatrix1: A);
777fn_matrix!(FnMatrix2: A B 2);
778
779macro_rules! fn_reference {
780    (VAL $t:ident) => {
781        any_struct!(VAL $t);
782        fn_reference!(__IMPL $t: A);
783    };
784    (OP $t:ident) => {
785        any_struct!(OP $t);
786        fn_reference!(__IMPL $t: A B);
787    };
788    (VAR $t:ident) => {
789        any_struct!(VAR $t);
790        fn_reference!(__IMPL $t:);
791    };
792    (REFVAR $t:ident) => {
793        any_struct!(REFVAR $t);
794        fn_reference!(__IMPL $t: 'a);
795    };
796    ($t:ident) => {
797        any_struct!($t);
798        fn_reference!(__IMPL $t:);
799    };
800    ($t:ident : $tname0:tt $($tname:tt $tidx:tt)*) => {
801        any_struct!($t: $tname0 $($tname $tidx)*);
802        fn_reference!(__IMPL $t: $tname0 $($tname)*);
803    };
804    (__IMPL $t:ident : $($l:lifetime)?) => {
805        impl $(<$l>)? Number for $t$(<$l>)?  {}
806        impl $(<$l>)? Text for $t$(<$l>)?  {}
807        impl $(<$l>)? Logical for $t$(<$l>)?  {}
808        impl $(<$l>)? Reference for $t$(<$l>)?  {}
809        impl $(<$l>)? Matrix for $t$(<$l>)?  {}
810        impl $(<$l>)? Array for $t$(<$l>)?  {}
811        impl $(<$l>)? Database for $t$(<$l>)?  {}
812        impl $(<$l>)? Criteria for $t$(<$l>)?  {}
813        impl $(<$l>)? Sequence for $t$(<$l>)?  {}
814        impl $(<$l>)? Scalar for $t$(<$l>)?  {}
815        impl $(<$l>)? Field for $t$(<$l>)?  {}
816        impl $(<$l>)? DateTime for $t$(<$l>)?  {}
817        impl $(<$l>)? TextOrNumber for $t$(<$l>)?  {}
818        impl $(<$l>)? ReferenceOrArray for $t$(<$l>)?  {}
819        impl $(<$l>)? TextOrReference for $t$(<$l>)?  {}
820        impl $(<$l>)? NumberOrArray for $t$(<$l>)?  {}
821    };
822    (__IMPL $t:ident : $($l:lifetime)? $tname0:tt $($tname:tt)*) => {
823        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Number for $t<$($l, )?$tname0 $(,$tname)*> {}
824        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Text for $t<$($l, )?$tname0 $(,$tname)*> {}
825        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Logical for $t<$($l, )?$tname0 $(,$tname)*> {}
826        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Reference for $t<$($l, )?$tname0 $(,$tname)*> {}
827        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Matrix for $t<$($l, )?$tname0 $(,$tname)*> {}
828        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Array for $t<$($l, )?$tname0 $(,$tname)*> {}
829        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Database for $t<$($l, )?$tname0 $(,$tname)*> {}
830        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Criteria for $t<$($l, )?$tname0 $(,$tname)*> {}
831        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Sequence for $t<$($l, )?$tname0 $(,$tname)*> {}
832        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Scalar for $t<$($l, )?$tname0 $(,$tname)*> {}
833        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Field for $t<$($l, )?$tname0 $(,$tname)*> {}
834        impl <$($l, )?$tname0: Any $(,$tname: Any)*> DateTime for $t<$($l, )?$tname0 $(,$tname)*> {}
835        impl <$($l, )?$tname0: Any $(,$tname: Any)*> TextOrNumber for $t<$($l, )?$tname0 $(,$tname)*> {}
836        impl <$($l, )?$tname0: Any $(,$tname: Any)*> ReferenceOrArray for $t<$($l, )?$tname0 $(,$tname)*> {}
837        impl <$($l, )?$tname0: Any $(,$tname: Any)*> TextOrReference for $t<$($l, )?$tname0 $(,$tname)*> {}
838        impl <$($l, )?$tname0: Any $(,$tname: Any)*> NumberOrArray for $t<$($l, )?$tname0 $(,$tname)*> {}
839    };
840}
841
842// fn_reference!(VAL ValReference);
843fn_reference!(OP OpReference);
844// fn_reference!(VAR FnReferenceVar);
845// fn_reference!(FnReference0);
846fn_reference!(FnReference1: A);
847fn_reference!(FnReference2: A B 2);
848fn_reference!(FnReference3: A B 2 C 3);
849fn_reference!(FnReference4: A B 2 C 3 D 4);
850fn_reference!(FnReference5: A B 2 C 3 D 4 E 5);
851
852macro_rules! fn_array {
853    (VAL $t:ident) => {
854        any_struct!(VAL $t);
855        fn_array!(__IMPL $t: A);
856    };
857    (OP $t:ident) => {
858        any_struct!(OP $t);
859        fn_array!(__IMPL $t: A B);
860    };
861    (VAR $t:ident) => {
862        any_struct!(VAR $t);
863        fn_array!(__IMPL $t:);
864    };
865    (REFVAR $t:ident) => {
866        any_struct!(REFVAR $t);
867        fn_array!(__IMPL $t: 'a);
868    };
869    ($t:ident) => {
870        any_struct!($t);
871        fn_array!(__IMPL $t:);
872    };
873    ($t:ident : $tname0:tt $($tname:tt $tidx:tt)*) => {
874        any_struct!($t: $tname0 $($tname $tidx)*);
875        fn_array!(__IMPL $t: $tname0 $($tname)*);
876    };
877    (__IMPL $t:ident : $($l:lifetime)?) => {
878        impl $(<$l>)? Array for $t$(<$l>)?  {}
879        impl $(<$l>)? ReferenceOrArray for $t$(<$l>)?  {}
880        impl $(<$l>)? NumberOrArray for $t$(<$l>)?  {}
881    };
882    (__IMPL $t:ident : $($l:lifetime)? $tname0:tt $($tname:tt)*) => {
883        impl <$($l, )?$tname0: Any $(,$tname: Any)*> Array for $t<$($l, )?$tname0 $(,$tname)*> {}
884        impl <$($l, )?$tname0: Any $(,$tname: Any)*> ReferenceOrArray for $t<$($l, )?$tname0 $(,$tname)*> {}
885        impl <$($l, )?$tname0: Any $(,$tname: Any)*> NumberOrArray for $t<$($l, )?$tname0 $(,$tname)*> {}
886    };
887}
888
889// fn_array!(VAL ValArray);
890// fn_array!(OP OpArray);
891// fn_array!(VAR FnArrayVar);
892// fn_array!(FnArray0);
893fn_array!(FnArray1: A);
894fn_array!(FnArray2: A B 2);
895fn_array!(FnArray3: A B 2 C 3);
896fn_array!(FnArray4: A B 2 C 3 D 4);
897
898// -----------------------------------------------------------------------
899
900macro_rules! tup {
901    ( $tname0:ident $($tname:tt $tnum:tt)* ) => {
902
903        impl<$tname0: Any, $($tname: Any,)*> Any for ($tname0, $($tname,)*) {
904            #[inline]
905            fn formula(&self, buf: &mut String) {
906                buf.push('{');
907                self.0.formula(buf);
908                $(
909                    buf.push(';');
910                    self.$tnum.formula(buf);
911                )*
912                buf.push('}');
913            }
914        }
915
916        impl<$tname0: Any + 'static, $($tname: Any + 'static,)*> Sequence for ($tname0, $($tname,)*) {}
917
918    }
919}
920
921impl Any for () {
922    #[inline]
923    fn formula(&self, buf: &mut String) {
924        buf.push('{');
925        buf.push('}');
926    }
927}
928impl Sequence for () {}
929
930tup!(A);
931tup!(A B 1 );
932tup!(A B 1 C 2 );
933tup!(A B 1 C 2 D 3);
934tup!(A B 1 C 2 D 3 E 4);
935tup!(A B 1 C 2 D 3 E 4 F 5);
936tup!(A B 1 C 2 D 3 E 4 F 5 G 6);
937tup!(A B 1 C 2 D 3 E 4 F 5 G 6 H 7);
938tup!(A B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8);
939tup!(A B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8 J 9);
940tup!(A B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8 J 9 K 10);
941tup!(A B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8 J 9 K 10 L 11);
942tup!(A B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8 J 9 K 10 L 11 M 12);
943tup!(A B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8 J 9 K 10 L 11 M 12 N 13);
944tup!(A B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8 J 9 K 10 L 11 M 12 N 13 O 14);
945tup!(A B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8 J 9 K 10 L 11 M 12 N 13 O 14 P 15 );
946tup!(A B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8 J 9 K 10 L 11 M 12 N 13 O 14 P 15 Q 16);
947tup!(A B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8 J 9 K 10 L 11 M 12 N 13 O 14 P 15 Q 16 R 17);
948tup!(A B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8 J 9 K 10 L 11 M 12 N 13 O 14 P 15 Q 16 R 17 S 18);
949tup!(A B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8 J 9 K 10 L 11 M 12 N 13 O 14 P 15 Q 16 R 17 S 18 T 19);
950tup!(A B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8 J 9 K 10 L 11 M 12 N 13 O 14 P 15 Q 16 R 17 S 18 T 19 U 20);
951
952// -----------------------------------------------------------------------
953
954/// Filter criteria.
955#[derive(Debug)]
956pub enum CriterionCmp {
957    Cmp,
958    Eq,
959    Ne,
960    Lt,
961    Gt,
962    LtEq,
963    GtEq,
964}
965
966impl Display for CriterionCmp {
967    #[inline]
968    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
969        match self {
970            CriterionCmp::Cmp => write!(f, ""),
971            CriterionCmp::Eq => write!(f, "="),
972            CriterionCmp::Ne => write!(f, "<>"),
973            CriterionCmp::Lt => write!(f, "<"),
974            CriterionCmp::Gt => write!(f, ">"),
975            CriterionCmp::LtEq => write!(f, "<="),
976            CriterionCmp::GtEq => write!(f, ">="),
977        }
978    }
979}
980
981/// Filter/search
982#[derive(Debug)]
983pub struct FCriterion<A: Any>(CriterionCmp, A);
984impl<A: Any> FCriterion<A> {
985    #[inline]
986    pub fn cmp(f: A) -> Self {
987        Self(CriterionCmp::Cmp, f)
988    }
989
990    #[inline]
991    pub fn eq(f: A) -> Self {
992        Self(CriterionCmp::Eq, f)
993    }
994
995    #[inline]
996    pub fn ne(f: A) -> Self {
997        Self(CriterionCmp::Ne, f)
998    }
999
1000    #[inline]
1001    pub fn lt(f: A) -> Self {
1002        Self(CriterionCmp::Lt, f)
1003    }
1004
1005    #[inline]
1006    pub fn gt(f: A) -> Self {
1007        Self(CriterionCmp::Gt, f)
1008    }
1009
1010    #[inline]
1011    pub fn lte(f: A) -> Self {
1012        Self(CriterionCmp::LtEq, f)
1013    }
1014
1015    #[inline]
1016    pub fn gte(f: A) -> Self {
1017        Self(CriterionCmp::GtEq, f)
1018    }
1019}
1020
1021impl<A: Any> Any for FCriterion<A> {
1022    #[inline]
1023    fn formula(&self, buf: &mut String) {
1024        let _ = write!(buf, "\"{}\"", self.0);
1025        buf.push('&');
1026        self.1.formula(buf);
1027    }
1028}
1029impl<A: Any> Criterion for FCriterion<A> {}
1030
1031impl<A: Any> Any for (CriterionCmp, A) {
1032    #[inline]
1033    fn formula(&self, buf: &mut String) {
1034        let _ = write!(buf, "\"{}\"", self.0);
1035        buf.push('&');
1036        self.1.formula(buf);
1037    }
1038}
1039impl<A: Any> Criterion for (CriterionCmp, A) {}
1040
1041// -----------------------------------------------------------------------
1042
1043/// Matrix value.
1044#[derive(Debug)]
1045pub struct FMatrix<T: Any, const N: usize, const M: usize>(pub [[T; M]; N]);
1046
1047impl<T: Any, const N: usize, const M: usize> Any for FMatrix<T, N, M> {
1048    #[inline]
1049    fn formula(&self, buf: &mut String) {
1050        buf.push('{');
1051        for (i, r) in self.0.iter().enumerate() {
1052            if i > 0 {
1053                buf.push('|');
1054            }
1055            for (j, v) in r.iter().enumerate() {
1056                if j > 0 {
1057                    buf.push(';');
1058                }
1059                v.formula(buf);
1060            }
1061        }
1062        buf.push('}');
1063    }
1064}
1065
1066impl<T: Any, const N: usize, const M: usize> Matrix for FMatrix<T, N, M> {}
1067
1068/// Array.
1069#[derive(Debug)]
1070pub struct FArray<T: Any, const N: usize>(pub [T; N]);
1071
1072impl<T: Any, const N: usize> Any for FArray<T, N> {
1073    #[inline]
1074    fn formula(&self, buf: &mut String) {
1075        buf.push('{');
1076        for (i, v) in self.0.iter().enumerate() {
1077            if i > 0 {
1078                buf.push(';');
1079            }
1080            v.formula(buf);
1081        }
1082        buf.push('}');
1083    }
1084}
1085
1086impl<T: Any, const N: usize> Array for FArray<T, N> {}
1087impl<T: Any, const N: usize> Sequence for FArray<T, N> {}
1088impl<T: Any, const N: usize> ReferenceOrArray for FArray<T, N> {}
1089impl<T: Any, const N: usize> NumberOrArray for FArray<T, N> {}
1090
1091// -----------------------------------------------------------------------
1092
1093impl<T: Any + Sized> Any for Option<T> {
1094    #[inline]
1095    fn formula(&self, buf: &mut String) {
1096        if let Some(v) = self {
1097            v.formula(buf);
1098        }
1099    }
1100}
1101
1102impl<T: Number + Any + Sized> Number for Option<T> {}
1103impl<T: Text + Any + Sized> Text for Option<T> {}
1104impl<T: Logical + Any + Sized> Logical for Option<T> {}
1105impl<T: Reference + Any + Sized> Reference for Option<T> {}
1106impl<T: Matrix + Any + Sized> Matrix for Option<T> {}
1107impl<T: Array + Any + Sized> Array for Option<T> {}
1108impl<T: Database + Any + Sized> Database for Option<T> {}
1109impl<T: Criterion + Any + Sized> Criterion for Option<T> {}
1110impl<T: Criteria + Any + Sized> Criteria for Option<T> {}
1111impl<T: Sequence + Any + Sized> Sequence for Option<T> {}
1112impl<T: Scalar + Any + Sized> Scalar for Option<T> {}
1113impl<T: Field + Any + Sized> Field for Option<T> {}
1114impl<T: DateTime + Any + Sized> DateTime for Option<T> {}
1115impl<T: TextOrNumber + Any + Sized> TextOrNumber for Option<T> {}
1116impl<T: ReferenceOrArray + Any + Sized> ReferenceOrArray for Option<T> {}
1117impl<T: TextOrReference + Any + Sized> TextOrReference for Option<T> {}
1118impl<T: NumberOrArray + Any + Sized> NumberOrArray for Option<T> {}
1119
1120// -----------------------------------------------------------------------
1121
1122/// An expression in parentheses. Use p() to create one.
1123#[derive(Debug)]
1124pub struct FParentheses<A>(A);
1125impl<A: Any> Any for FParentheses<A> {
1126    #[inline]
1127    fn formula(&self, buf: &mut String) {
1128        buf.push('(');
1129        self.0.formula(buf);
1130        buf.push(')');
1131    }
1132}
1133impl<A: Number> Number for FParentheses<A> {}
1134impl<A: Text> Text for FParentheses<A> {}
1135impl<A: Logical> Logical for FParentheses<A> {}
1136impl<A: Reference> Reference for FParentheses<A> {}
1137impl<A: Matrix> Matrix for FParentheses<A> {}
1138impl<A: Array> Array for FParentheses<A> {}
1139impl<A: Database> Database for FParentheses<A> {}
1140impl<A: Criteria> Criteria for FParentheses<A> {}
1141impl<A: Sequence> Sequence for FParentheses<A> {}
1142impl<A: Scalar> Scalar for FParentheses<A> {}
1143impl<A: Field> Field for FParentheses<A> {}
1144impl<A: DateTime> DateTime for FParentheses<A> {}
1145impl<A: TextOrNumber> TextOrNumber for FParentheses<A> {}
1146impl<A: ReferenceOrArray> ReferenceOrArray for FParentheses<A> {}
1147impl<A: TextOrReference> TextOrReference for FParentheses<A> {}
1148impl<A: NumberOrArray> NumberOrArray for FParentheses<A> {}
1149
1150/// Creates an expression in parentheses.
1151#[inline]
1152pub fn p<A: Any>(a: A) -> FParentheses<A> {
1153    FParentheses(a)
1154}
1155
1156// -----------------------------------------------------------------------
1157
1158macro_rules! value_number {
1159    ($t:ty) => {
1160        impl Any for $t {
1161            #[inline]
1162            fn formula(&self, buf: &mut String) {
1163                let _ = write!(buf, "{}", self);
1164            }
1165        }
1166        impl Number for $t {}
1167        impl Logical for $t {}
1168        impl Sequence for $t {}
1169        impl Scalar for $t {}
1170        impl Field for $t {}
1171        impl DateTime for $t {}
1172        impl TextOrNumber for $t {}
1173        impl NumberOrArray for $t {}
1174    };
1175}
1176
1177value_number!(i8);
1178value_number!(i16);
1179value_number!(i32);
1180value_number!(i64);
1181value_number!(i128);
1182value_number!(isize);
1183value_number!(u8);
1184value_number!(u16);
1185value_number!(u32);
1186value_number!(u64);
1187value_number!(u128);
1188value_number!(usize);
1189value_number!(f32);
1190value_number!(f64);
1191
1192/// Creates a formula-number from a rust number literal.
1193#[inline]
1194pub fn num<A: Number>(n: A) -> ValNumber<A> {
1195    ValNumber(n)
1196}
1197
1198impl Any for bool {
1199    #[inline]
1200    fn formula(&self, buf: &mut String) {
1201        buf.push_str(if *self { "TRUE()" } else { "FALSE()" });
1202    }
1203}
1204impl Number for bool {}
1205impl Logical for bool {}
1206impl Sequence for bool {}
1207impl Scalar for bool {}
1208impl NumberOrArray for bool {}
1209
1210impl Any for &str {
1211    #[inline]
1212    fn formula(&self, buf: &mut String) {
1213        if self.contains('"') {
1214            buf.push('"');
1215            for (i, s) in self.split('"').enumerate() {
1216                if i > 0 {
1217                    buf.push_str("\"\"");
1218                }
1219                buf.push_str(s);
1220            }
1221            buf.push('"');
1222        } else {
1223            buf.push('"');
1224            buf.push_str(self);
1225            buf.push('"');
1226        }
1227    }
1228}
1229impl Text for &str {}
1230impl Sequence for &str {}
1231impl Scalar for &str {}
1232impl Field for &str {}
1233impl DateTime for &str {}
1234impl TextOrNumber for &str {}
1235impl TextOrReference for &str {}
1236
1237impl<'a> Any for Cow<'a, str> {
1238    #[inline]
1239    fn formula(&self, buf: &mut String) {
1240        let s: &str = self.borrow();
1241        s.formula(buf)
1242    }
1243}
1244impl<'a> Text for Cow<'a, str> {}
1245impl<'a> Sequence for Cow<'a, str> {}
1246impl<'a> Scalar for Cow<'a, str> {}
1247impl<'a> Field for Cow<'a, str> {}
1248impl<'a> DateTime for Cow<'a, str> {}
1249impl<'a> TextOrNumber for Cow<'a, str> {}
1250impl<'a> TextOrReference for Cow<'a, str> {}
1251
1252impl Any for String {
1253    #[inline]
1254    fn formula(&self, buf: &mut String) {
1255        self.as_str().formula(buf)
1256    }
1257}
1258impl Text for String {}
1259impl Sequence for String {}
1260impl Scalar for String {}
1261impl Field for String {}
1262impl DateTime for String {}
1263impl TextOrNumber for String {}
1264impl TextOrReference for String {}
1265
1266impl Any for CellRef {
1267    #[inline]
1268    fn formula(&self, buf: &mut String) {
1269        buf.push_str(self.to_formula().as_str())
1270    }
1271}
1272impl Number for CellRef {}
1273impl Text for CellRef {}
1274impl Logical for CellRef {}
1275impl Reference for CellRef {}
1276impl Matrix for CellRef {}
1277impl Array for CellRef {}
1278impl Database for CellRef {}
1279impl Criteria for CellRef {}
1280impl Sequence for CellRef {}
1281impl Scalar for CellRef {}
1282impl Field for CellRef {}
1283impl DateTime for CellRef {}
1284impl TextOrNumber for CellRef {}
1285impl ReferenceOrArray for CellRef {}
1286impl TextOrReference for CellRef {}
1287impl NumberOrArray for CellRef {}
1288
1289impl Any for CellRange {
1290    #[inline]
1291    fn formula(&self, buf: &mut String) {
1292        buf.push_str(self.to_formula().as_str())
1293    }
1294}
1295impl Number for CellRange {}
1296impl Text for CellRange {}
1297impl Logical for CellRange {}
1298impl Reference for CellRange {}
1299impl Matrix for CellRange {}
1300impl Array for CellRange {}
1301impl Database for CellRange {}
1302impl Criteria for CellRange {}
1303impl Sequence for CellRange {}
1304impl Scalar for CellRange {}
1305impl Field for CellRange {}
1306impl DateTime for CellRange {}
1307impl TextOrNumber for CellRange {}
1308impl ReferenceOrArray for CellRange {}
1309impl TextOrReference for CellRange {}
1310impl NumberOrArray for CellRange {}
1311
1312// -----------------------------------------------------------------------
1313
1314macro_rules! number_op {
1315    ($t:ident $(< $($l:lifetime $(,)? )? $($tname:ident $(,)?)* >)?) => {
1316        impl <$($($l,)? $($tname: Any,)*)? V: Number> Add<V> for $t $(< $($l,)? $($tname,)* >)? {
1317            type Output = OpNumber<Self, V>;
1318
1319            #[inline]
1320            fn add(self, rhs: V) -> Self::Output {
1321                OpNumber(self, "+", rhs)
1322            }
1323        }
1324
1325        impl <$($($l,)? $($tname: Any,)*)? V: Number> Sub<V> for $t $(< $($l,)? $($tname,)* >)? {
1326            type Output = OpNumber<Self, V>;
1327
1328            #[inline]
1329            fn sub(self, rhs: V) -> Self::Output {
1330                OpNumber(self, "-", rhs)
1331            }
1332        }
1333
1334        impl <$($($l,)? $($tname: Any,)*)? V: Number> Mul<V> for $t $(< $($l,)? $($tname,)* >)? {
1335            type Output = OpNumber<Self, V>;
1336
1337            #[inline]
1338            fn mul(self, rhs: V) -> Self::Output {
1339                OpNumber(self, "*", rhs)
1340            }
1341        }
1342
1343        impl <$($($l,)? $($tname: Any,)*)? V: Number> Div<V> for $t $(< $($l,)? $($tname,)* >)? {
1344            type Output = OpNumber<Self, V>;
1345
1346            #[inline]
1347            fn div(self, rhs: V) -> Self::Output {
1348                OpNumber(self, "/", rhs)
1349            }
1350        }
1351
1352        impl <$($($l,)? $($tname: Any,)*)? V: Number> BitXor<V> for $t $(< $($l,)? $($tname,)* >)? {
1353            type Output = OpNumber<Self, V>;
1354
1355            #[inline]
1356            fn bitxor(self, rhs: V) -> Self::Output {
1357                OpNumber(self, "^", rhs)
1358            }
1359        }
1360
1361        impl <$($($l,)? $($tname: Any,)*)?> Neg for $t $(< $($l,)? $($tname,)* >)? {
1362            type Output = OpNumber<(), Self>;
1363
1364            #[inline]
1365            fn neg(self) -> Self::Output {
1366                OpNumber((), "-", self)
1367            }
1368        }
1369
1370    };
1371}
1372
1373// number_op!(ValAny<A>);
1374// number_op!(OpAny<A, B>);
1375number_op!(FnAnyVar);
1376number_op!(FnAny0);
1377number_op!(FnAny1<A>);
1378number_op!(FnAny2<A, B>);
1379number_op!(FnAny3<A, B, C>);
1380number_op!(FnAny4<A, B, C, D>);
1381number_op!(FnAny5<A, B, C, D, E>);
1382
1383number_op!(ValNumber<A>);
1384number_op!(OpNumber<A, B>);
1385number_op!(FnNumberVar);
1386number_op!(FnNumber0);
1387number_op!(FnNumber1<A>);
1388number_op!(FnNumber2<A, B>);
1389number_op!(FnNumber3<A, B, C>);
1390number_op!(FnNumber4<A, B, C, D>);
1391number_op!(FnNumber5<A, B, C, D, E>);
1392number_op!(FnNumber6<A, B, C, D, E, F>);
1393number_op!(FnNumber7<A, B, C, D, E, F, G>);
1394number_op!(FnNumber8<A, B, C, D, E, F, G, H>);
1395number_op!(FnNumber9<A, B, C, D, E, F, G, H, I>);
1396
1397// number_op!(ValLogical<A>);
1398number_op!(OpLogical<A, B>);
1399// number_op!(FnLogicalVar);
1400number_op!(FnLogical0);
1401number_op!(FnLogical1<A>);
1402number_op!(FnLogical2<A, B>);
1403
1404// number_op!(ValReference<A>);
1405number_op!(OpReference<A, B>);
1406// number_op!(FnReferenceVar);
1407// number_op!(FnReference0);
1408number_op!(FnReference1<A>);
1409number_op!(FnReference2<A, B>);
1410number_op!(FnReference3<A, B, C>);
1411number_op!(FnReference4<A, B, C, D>);
1412number_op!(FnReference5<A, B, C, D, E>);
1413
1414number_op!(FParentheses<A>);
1415
1416// -----------------------------------------------------------------------
1417
1418macro_rules! text_op {
1419    ($t:ident $(< $($l:lifetime $(,)? )? $($tname:ident $(,)?)* >)?) => {
1420        impl <$($($l,)? $($tname: Any,)*)? V: Text> BitAnd<V> for $t $(< $($l,)? $($tname,)* >)? {
1421            type Output = OpText<Self, V>;
1422
1423            #[inline]
1424            fn bitand(self, rhs: V) -> Self::Output {
1425                OpText(self, "&", rhs)
1426            }
1427        }
1428    }
1429}
1430
1431// text_op!(ValText<A>);
1432text_op!(OpText<A, B>);
1433text_op!(FnTextVar);
1434// text_op!(FnText0);
1435text_op!(FnText1<A>);
1436text_op!(FnText2<A, B>);
1437text_op!(FnText3<A, B, C>);
1438text_op!(FnText4<A, B, C, D>);
1439text_op!(FnText5<A, B, C, D, E>);
1440
1441// -----------------------------------------------------------------------
1442
1443/// Creates a formula from any formula expression.
1444#[inline]
1445pub fn formula<T: Any>(f: T) -> String {
1446    let mut buf = String::new();
1447    buf.push_str("of:=");
1448    f.formula(&mut buf);
1449    buf
1450}