Skip to content

Commit

Permalink
Proxy bound types through simpler types in the public API.
Browse files Browse the repository at this point in the history
  • Loading branch information
olson-sean-k committed Mar 18, 2024
1 parent e8e03ab commit 08cab4e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 18 deletions.
88 changes: 72 additions & 16 deletions src/query.rs
@@ -1,21 +1,68 @@
//! [`Program`] diagnostic and inspection types.
//! [`Program`] diagnostics and inspection.
//!
//! [`Program`]: crate::Program

use std::borrow::Cow;

use crate::token::{Depth, Text, TokenVariance};
use crate::token::{self, Depth, Text, TokenVariance};

pub use crate::diagnostics::{LocatedError, Span};
pub use crate::token::{
BoundedVariantRange, Boundedness, NonZeroBound, NonZeroLower, NonZeroUpper, VariantRange,
};
pub use crate::token::Boundedness;

pub use Boundedness::{Bounded, Unbounded};

// TODO: `VariantRange` is a complex type composition that requires many re-exported types. Define
// a simpler local type if possible.
pub type DepthVariance = Variance<usize, VariantRange>;
pub type TextVariance<'t> = Variance<Cow<'t, str>, ()>;

pub type VariantRange = Boundedness<BoundedVariantRange>;

impl VariantRange {
pub fn lower(&self) -> Boundedness<usize> {
match self {
Bounded(ref range) => range.lower(),
_ => Unbounded,
}
}

pub fn upper(&self) -> Boundedness<usize> {
match self {
Bounded(ref range) => range.upper(),
_ => Unbounded,
}
}
}

impl From<token::VariantRange> for VariantRange {
fn from(range: token::VariantRange) -> Self {
range.map_bounded(From::from)
}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct BoundedVariantRange {
lower: Boundedness<usize>,
upper: Boundedness<usize>,
}

impl BoundedVariantRange {
pub fn lower(&self) -> Boundedness<usize> {
self.lower
}

pub fn upper(&self) -> Boundedness<usize> {
self.upper
}
}

impl From<token::BoundedVariantRange> for BoundedVariantRange {
fn from(range: token::BoundedVariantRange) -> Self {
BoundedVariantRange {
lower: range.lower().into_bound().map_bounded(From::from),
upper: range.upper().into_bound().map_bounded(From::from),
}
}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Variance<T, B> {
Invariant(T),
Expand Down Expand Up @@ -53,6 +100,24 @@ impl<T, B> Variance<T, B> {
}
}

impl From<DepthVariance> for (Option<usize>, Option<usize>) {
fn from(depth: DepthVariance) -> Self {
match depth {
DepthVariance::Invariant(depth) => (Some(depth), Some(depth)),
DepthVariance::Variant(bounds) => (bounds.lower().bounded(), bounds.upper().bounded()),
}
}
}

impl From<TokenVariance<Depth>> for DepthVariance {
fn from(depth: TokenVariance<Depth>) -> Self {
match depth {
TokenVariance::Invariant(depth) => DepthVariance::Invariant(depth.into()),
TokenVariance::Variant(bounds) => DepthVariance::Variant(bounds.into()),
}
}
}

impl<'t> From<TextVariance<'t>> for Option<Cow<'t, str>> {
fn from(text: TextVariance<'t>) -> Self {
match text {
Expand All @@ -71,15 +136,6 @@ impl<'t> From<TokenVariance<Text<'t>>> for TextVariance<'t> {
}
}

impl From<TokenVariance<Depth>> for DepthVariance {
fn from(depth: TokenVariance<Depth>) -> Self {
match depth {
TokenVariance::Invariant(depth) => DepthVariance::Invariant(depth.into()),
TokenVariance::Variant(bounds) => DepthVariance::Variant(bounds),
}
}
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum When {
Always,
Expand Down
3 changes: 1 addition & 2 deletions src/token/mod.rs
Expand Up @@ -22,8 +22,7 @@ use crate::{StrExt as _, PATHS_ARE_CASE_INSENSITIVE};

pub use crate::token::parse::{parse, ParseError, ROOT_SEPARATOR_EXPRESSION};
pub use crate::token::variance::bound::{
BoundedVariantRange, Boundedness, NaturalRange, NonZeroBound, NonZeroLower, NonZeroUpper,
VariantRange,
BoundedVariantRange, Boundedness, NaturalRange, VariantRange,
};
pub use crate::token::variance::invariant::{Breadth, Depth, Invariant, Size, Text};
pub use crate::token::variance::{TokenVariance, Variance};
Expand Down

0 comments on commit 08cab4e

Please sign in to comment.