|
1 |
| -mod natural; |
2 | 1 | mod term;
|
3 | 2 | mod text;
|
4 | 3 |
|
5 | 4 | use std::num::NonZeroUsize;
|
6 | 5 |
|
7 |
| -use crate::token::variance::bound::{BoundedVariantRange, Boundedness, OpenedUpperBound}; |
8 |
| -use crate::token::variance::ops::{Conjunction, Disjunction, Product}; |
9 |
| -use crate::token::variance::TokenVariance; |
| 6 | +use crate::token::variance::natural::{ |
| 7 | + define_natural_invariant, BoundedVariantRange, OpenedUpperBound, |
| 8 | +}; |
| 9 | +use crate::token::variance::ops::{self, Conjunction, Disjunction, Product}; |
| 10 | +use crate::token::variance::{Boundedness, TokenVariance}; |
10 | 11 |
|
11 |
| -pub use crate::token::variance::invariant::natural::{Depth, Size}; |
12 | 12 | pub use crate::token::variance::invariant::term::{BoundaryTerm, SeparatedTerm, Termination};
|
13 | 13 | pub use crate::token::variance::invariant::text::{IntoNominalText, IntoStructuralText, Text};
|
14 | 14 |
|
@@ -38,6 +38,59 @@ pub trait Invariant: Sized + Zero {
|
38 | 38 | fn into_lower_bound(self) -> Boundedness<Self::Bound>;
|
39 | 39 | }
|
40 | 40 |
|
| 41 | +#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)] |
| 42 | +pub struct UnitBound; |
| 43 | + |
| 44 | +impl<T> Conjunction<T> for UnitBound { |
| 45 | + type Output = Self; |
| 46 | + |
| 47 | + fn conjunction(self, _: T) -> Self::Output { |
| 48 | + self |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl Disjunction for UnitBound { |
| 53 | + type Output = Boundedness<Self>; |
| 54 | + |
| 55 | + fn disjunction(self, _: Self) -> Self::Output { |
| 56 | + self.into() |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl From<()> for UnitBound { |
| 61 | + fn from(_: ()) -> Self { |
| 62 | + UnitBound |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl From<BoundedVariantRange> for UnitBound { |
| 67 | + fn from(_: BoundedVariantRange) -> Self { |
| 68 | + UnitBound |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl OpenedUpperBound for UnitBound { |
| 73 | + fn opened_upper_bound(self) -> Boundedness<Self> { |
| 74 | + UnitBound.into() |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl Product<BoundedVariantRange> for UnitBound { |
| 79 | + type Output = Boundedness<Self>; |
| 80 | + |
| 81 | + fn product(self, _: BoundedVariantRange) -> Self::Output { |
| 82 | + self.into() |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl Product<NonZeroUsize> for Boundedness<UnitBound> { |
| 87 | + type Output = Self; |
| 88 | + |
| 89 | + fn product(self, _: NonZeroUsize) -> Self::Output { |
| 90 | + self |
| 91 | + } |
| 92 | +} |
| 93 | + |
41 | 94 | // Breadth is the maximum size (UTF-8 bytes) of component text in a glob expression. For example,
|
42 | 95 | // the breadth of `{a/,a/b/}<c/d:2>` is two (bytes).
|
43 | 96 | //
|
@@ -82,55 +135,39 @@ impl Invariant for Breadth {
|
82 | 135 | }
|
83 | 136 | }
|
84 | 137 |
|
85 |
| -#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)] |
86 |
| -pub struct UnitBound; |
87 |
| - |
88 |
| -impl<T> Conjunction<T> for UnitBound { |
89 |
| - type Output = Self; |
90 |
| - |
91 |
| - fn conjunction(self, _: T) -> Self::Output { |
92 |
| - self |
93 |
| - } |
94 |
| -} |
95 |
| - |
96 |
| -impl Disjunction for UnitBound { |
97 |
| - type Output = Boundedness<Self>; |
98 |
| - |
99 |
| - fn disjunction(self, _: Self) -> Self::Output { |
100 |
| - self.into() |
101 |
| - } |
102 |
| -} |
| 138 | +define_natural_invariant!( |
| 139 | + /// Depth of a glob expression. |
| 140 | + Depth => BoundaryTerm, |
| 141 | +); |
103 | 142 |
|
104 |
| -impl From<()> for UnitBound { |
105 |
| - fn from(_: ()) -> Self { |
106 |
| - UnitBound |
| 143 | +impl TokenVariance<Depth> { |
| 144 | + pub fn is_exhaustive(&self) -> bool { |
| 145 | + !self.has_upper_bound() |
107 | 146 | }
|
108 | 147 | }
|
109 | 148 |
|
110 |
| -impl From<BoundedVariantRange> for UnitBound { |
111 |
| - fn from(_: BoundedVariantRange) -> Self { |
112 |
| - UnitBound |
| 149 | +impl BoundaryTerm<Depth> { |
| 150 | + pub fn is_exhaustive(&self) -> bool { |
| 151 | + self.clone().finalize().is_exhaustive() |
113 | 152 | }
|
114 | 153 | }
|
115 | 154 |
|
116 |
| -impl OpenedUpperBound for UnitBound { |
117 |
| - fn opened_upper_bound(self) -> Boundedness<Self> { |
118 |
| - UnitBound.into() |
119 |
| - } |
120 |
| -} |
| 155 | +impl Finalize for SeparatedTerm<TokenVariance<Depth>> { |
| 156 | + type Output = TokenVariance<Depth>; |
121 | 157 |
|
122 |
| -impl Product<BoundedVariantRange> for UnitBound { |
123 |
| - type Output = Boundedness<Self>; |
| 158 | + fn finalize(self) -> Self::Output { |
| 159 | + use Termination::{Closed, Open}; |
124 | 160 |
|
125 |
| - fn product(self, _: BoundedVariantRange) -> Self::Output { |
126 |
| - self.into() |
| 161 | + let SeparatedTerm(termination, term) = self; |
| 162 | + match termination { |
| 163 | + Open => ops::conjunction(term, One::one()), |
| 164 | + Closed => term.map_invariant(|term| term.map(|term| term.saturating_sub(1))), |
| 165 | + _ => term, |
| 166 | + } |
127 | 167 | }
|
128 | 168 | }
|
129 | 169 |
|
130 |
| -impl Product<NonZeroUsize> for Boundedness<UnitBound> { |
131 |
| - type Output = Self; |
132 |
| - |
133 |
| - fn product(self, _: NonZeroUsize) -> Self::Output { |
134 |
| - self |
135 |
| - } |
136 |
| -} |
| 170 | +define_natural_invariant!( |
| 171 | + /// Size of a glob expression. |
| 172 | + Size, |
| 173 | +); |
0 commit comments