summary refs log blame commit diff stats
path: root/src/lib.rs
blob: 08609ab05337f376f2481d1267d0e68d9e090794 (plain) (tree)
1
2
3
4
5
6
7






                         

























                                                                                                                            

                                                           
                        
                                     

                       
 
                              


                                               




                                                                             
                                                                








                                                                               

                                               



                                                                               

                                                                                  

                                                                            



                                                 


       
                                                                                
pub mod errors;
mod parser;
mod pattern;
mod vm;

pub use pattern::Pattern;

// TODO replace with GATs
/// The key returned from pairs and get.
pub enum Key<'b, T, U> {
    /// Reference.
    Ref(&'b T),
    /// Borrowed string.
    Str(&'b str),
    /// Owned types.
    Own(U),
}

impl<'b, T, U: Copy> Copy for Key<'b, T, U> {
}

impl<'b, T, U: Clone> Clone for Key<'b, T, U> {
    fn clone(&self) -> Self {
        match self {
            Key::Ref(r) => Key::Ref(r),
            Key::Str(r) => Key::Str(r),
            Key::Own(v) => Key::Own(v.clone()),
        }
    }
}

pub type KVPair<'b, T> = (Key<'b, <T as PatternTypes>::Value, <T as PatternTypes>::OwnKey>, &'b <T as PatternTypes>::Value);

// TODO investigate if this should be PatternTypes: Default
/// Defines the types and operations used for matching.
pub trait PatternTypes {
    // TODO investigate Value: ?Sized
    /// The value type.
    type Value;

    // TODO replace with GATs.
    // TODO potentially relax with Clone?
    /// The owned key type. May be uninhabited.
    type OwnKey: Copy + 'static;

    /// Returns an iterator over key-value pairs contained within an item, or
    /// None if this operation is unsupported for the given value.
    fn pairs<'b>(
        item: &'b Self::Value
    ) -> Option<Box<dyn Iterator<Item=KVPair<'b, Self>> + 'b>> {
        // TODO remove these default impls that only exist for testing purposes
        let x = None;
        Some(Box::new(x.into_iter()))
    }

    /// Returns an optional key-value pair keyed by the given key, or None if
    /// this operation is unsupported for the given value.
    fn get<'a, 'b>(
        item: &'b Self::Value,
        key: Key<'a, Self::Value, Self::OwnKey>
    ) -> Option<Option<KVPair<'b, Self>>> {
        // TODO remove these default impls that only exist for testing purposes
        Some(None)
    }

    // TODO replace with GATs + newtypes
    /// Returns whether two keys/values are the same/equivalent. This must provide
    /// the same guarantees as PartialEq. In fact, this is a replacement for
    /// PartialEq for cases where it's not possible to just use PartialEq.
    fn matches(
        left: Key<'_, Self::Value, Self::OwnKey>,
        right: Key<'_, Self::Value, Self::OwnKey>
    ) -> bool;
}

// TODO
type Predicate<T> = dyn (Fn(&<T as PatternTypes>::Value) -> bool) + Send + Sync;