2019-09-09 07:54:02 -05:00
|
|
|
use crate::MacroDef;
|
|
|
|
|
2019-03-16 09:17:50 -05:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub enum Namespace {
|
|
|
|
Types,
|
|
|
|
Values,
|
2019-09-09 12:21:29 -05:00
|
|
|
// Note that only type inference uses this enum, and it doesn't care about macros.
|
|
|
|
// Macro,
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct PerNs<T> {
|
|
|
|
pub types: Option<T>,
|
|
|
|
pub values: Option<T>,
|
2019-09-09 07:54:02 -05:00
|
|
|
/// Since macros has different type, many methods simply ignore it.
|
|
|
|
/// We can only use special method like `get_macros` to access it.
|
|
|
|
pub macros: Option<MacroDef>,
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Default for PerNs<T> {
|
|
|
|
fn default() -> Self {
|
2019-09-09 07:54:02 -05:00
|
|
|
PerNs { types: None, values: None, macros: None }
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> PerNs<T> {
|
|
|
|
pub fn none() -> PerNs<T> {
|
2019-09-09 07:54:02 -05:00
|
|
|
PerNs { types: None, values: None, macros: None }
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn values(t: T) -> PerNs<T> {
|
2019-09-09 07:54:02 -05:00
|
|
|
PerNs { types: None, values: Some(t), macros: None }
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn types(t: T) -> PerNs<T> {
|
2019-09-09 07:54:02 -05:00
|
|
|
PerNs { types: Some(t), values: None, macros: None }
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn both(types: T, values: T) -> PerNs<T> {
|
2019-09-09 07:54:02 -05:00
|
|
|
PerNs { types: Some(types), values: Some(values), macros: None }
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 07:54:02 -05:00
|
|
|
pub fn macros(macro_: MacroDef) -> PerNs<T> {
|
|
|
|
PerNs { types: None, values: None, macros: Some(macro_) }
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 07:54:02 -05:00
|
|
|
pub fn is_none(&self) -> bool {
|
|
|
|
self.types.is_none() && self.values.is_none() && self.macros.is_none()
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 07:54:02 -05:00
|
|
|
pub fn is_all(&self) -> bool {
|
|
|
|
self.types.is_some() && self.values.is_some() && self.macros.is_some()
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take_types(self) -> Option<T> {
|
2019-09-09 07:54:02 -05:00
|
|
|
self.types
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take_values(self) -> Option<T> {
|
2019-09-09 07:54:02 -05:00
|
|
|
self.values
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 07:54:02 -05:00
|
|
|
pub fn get_macros(&self) -> Option<MacroDef> {
|
|
|
|
self.macros
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn only_macros(&self) -> PerNs<T> {
|
|
|
|
PerNs { types: None, values: None, macros: self.macros }
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn or(self, other: PerNs<T>) -> PerNs<T> {
|
2019-09-09 07:54:02 -05:00
|
|
|
PerNs {
|
|
|
|
types: self.types.or(other.types),
|
|
|
|
values: self.values.or(other.values),
|
|
|
|
macros: self.macros.or(other.macros),
|
|
|
|
}
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
}
|