Add convenient functions and operators for Module and module resolvers.

This commit is contained in:
Stephen Chung 2020-10-07 15:40:36 +08:00
parent a56859c2ac
commit d7bfe13b3e
4 changed files with 86 additions and 10 deletions

View File

@ -33,7 +33,7 @@ use crate::stdlib::{
fmt, format, fmt, format,
iter::empty, iter::empty,
num::NonZeroUsize, num::NonZeroUsize,
ops::{Deref, DerefMut}, ops::{Add, AddAssign, Deref, DerefMut},
string::{String, ToString}, string::{String, ToString},
vec::Vec, vec::Vec,
}; };
@ -1546,6 +1546,38 @@ impl From<StaticVec<(String, Position)>> for ModuleRef {
} }
} }
impl Add<Module> for Module {
type Output = Self;
fn add(self, rhs: Module) -> Self::Output {
let mut module = self.clone();
module.merge(&rhs);
module
}
}
impl Add<&Module> for Module {
type Output = Self;
fn add(self, rhs: &Module) -> Self::Output {
let mut module = self.clone();
module.merge(rhs);
module
}
}
impl AddAssign<Module> for Module {
fn add_assign(&mut self, rhs: Module) {
self.combine(rhs);
}
}
impl AddAssign<&Module> for Module {
fn add_assign(&mut self, rhs: &Module) {
self.merge(rhs);
}
}
impl ModuleRef { impl ModuleRef {
pub(crate) fn index(&self) -> Option<NonZeroUsize> { pub(crate) fn index(&self) -> Option<NonZeroUsize> {
self.1 self.1

View File

@ -3,7 +3,7 @@ use crate::module::{Module, ModuleResolver};
use crate::result::EvalAltResult; use crate::result::EvalAltResult;
use crate::token::Position; use crate::token::Position;
use crate::stdlib::{boxed::Box, vec::Vec}; use crate::stdlib::{boxed::Box, ops::AddAssign, vec::Vec};
/// Module resolution service that holds a collection of module resolves, /// Module resolution service that holds a collection of module resolves,
/// to be searched in sequential order. /// to be searched in sequential order.
@ -45,9 +45,6 @@ impl ModuleResolversCollection {
pub fn new() -> Self { pub fn new() -> Self {
Default::default() Default::default()
} }
}
impl ModuleResolversCollection {
/// Add a module keyed by its path. /// Add a module keyed by its path.
pub fn push(&mut self, resolver: impl ModuleResolver + 'static) { pub fn push(&mut self, resolver: impl ModuleResolver + 'static) {
self.0.push(Box::new(resolver)); self.0.push(Box::new(resolver));
@ -56,10 +53,29 @@ impl ModuleResolversCollection {
pub fn iter(&self) -> impl Iterator<Item = &dyn ModuleResolver> { pub fn iter(&self) -> impl Iterator<Item = &dyn ModuleResolver> {
self.0.iter().map(|v| v.as_ref()) self.0.iter().map(|v| v.as_ref())
} }
/// Get a mutable iterator of all the modules.
pub fn into_iter(self) -> impl Iterator<Item = Box<dyn ModuleResolver>> {
self.0.into_iter()
}
/// Remove all module resolvers. /// Remove all module resolvers.
pub fn clear(&mut self) { pub fn clear(&mut self) {
self.0.clear(); self.0.clear();
} }
/// Is this `ModuleResolversCollection` empty?
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Get the number of module resolvers in this `ModuleResolversCollection`.
pub fn len(&self) -> usize {
self.0.len()
}
/// Add another `ModuleResolversCollection` to the end of this collection.
/// The other `ModuleResolversCollection` is consumed.
pub fn append(&mut self, other: Self) {
if !other.is_empty() {
self.0.extend(other.0.into_iter());
}
}
} }
impl ModuleResolver for ModuleResolversCollection { impl ModuleResolver for ModuleResolversCollection {
@ -83,3 +99,9 @@ impl ModuleResolver for ModuleResolversCollection {
EvalAltResult::ErrorModuleNotFound(path.into(), pos).into() EvalAltResult::ErrorModuleNotFound(path.into(), pos).into()
} }
} }
impl<M: ModuleResolver + 'static> AddAssign<M> for ModuleResolversCollection {
fn add_assign(&mut self, rhs: M) {
self.push(rhs);
}
}

View File

@ -3,7 +3,7 @@ use crate::module::{Module, ModuleResolver};
use crate::result::EvalAltResult; use crate::result::EvalAltResult;
use crate::token::Position; use crate::token::Position;
use crate::stdlib::{boxed::Box, collections::HashMap, string::String}; use crate::stdlib::{boxed::Box, collections::HashMap, ops::AddAssign, string::String};
/// Module resolution service that serves modules added into it. /// Module resolution service that serves modules added into it.
/// ///
@ -45,9 +45,6 @@ impl StaticModuleResolver {
pub fn new() -> Self { pub fn new() -> Self {
Default::default() Default::default()
} }
}
impl StaticModuleResolver {
/// Add a module keyed by its path. /// Add a module keyed by its path.
pub fn insert<S: Into<String>>(&mut self, path: S, module: Module) { pub fn insert<S: Into<String>>(&mut self, path: S, module: Module) {
self.0.insert(path.into(), module); self.0.insert(path.into(), module);
@ -68,6 +65,10 @@ impl StaticModuleResolver {
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &mut Module)> { pub fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &mut Module)> {
self.0.iter_mut().map(|(k, v)| (k.as_str(), v)) self.0.iter_mut().map(|(k, v)| (k.as_str(), v))
} }
/// Get a mutable iterator of all the modules.
pub fn into_iter(self) -> impl Iterator<Item = (String, Module)> {
self.0.into_iter()
}
/// Get an iterator of all the module paths. /// Get an iterator of all the module paths.
pub fn paths(&self) -> impl Iterator<Item = &str> { pub fn paths(&self) -> impl Iterator<Item = &str> {
self.0.keys().map(String::as_str) self.0.keys().map(String::as_str)
@ -84,6 +85,21 @@ impl StaticModuleResolver {
pub fn clear(&mut self) { pub fn clear(&mut self) {
self.0.clear(); self.0.clear();
} }
/// Is this `StaticModuleResolver` empty?
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Get the number of modules in this `StaticModuleResolver`.
pub fn len(&self) -> usize {
self.0.len()
}
/// Merge another `StaticModuleResolver` into this.
/// The other `StaticModuleResolver` is consumed.
pub fn merge(&mut self, other: Self) {
if !other.is_empty() {
self.0.extend(other.0.into_iter());
}
}
} }
impl ModuleResolver for StaticModuleResolver { impl ModuleResolver for StaticModuleResolver {
@ -94,3 +110,9 @@ impl ModuleResolver for StaticModuleResolver {
.ok_or_else(|| EvalAltResult::ErrorModuleNotFound(path.into(), pos).into()) .ok_or_else(|| EvalAltResult::ErrorModuleNotFound(path.into(), pos).into())
} }
} }
impl AddAssign<Self> for StaticModuleResolver {
fn add_assign(&mut self, rhs: Self) {
self.merge(rhs);
}
}

View File

@ -459,7 +459,7 @@ impl Add<&Self> for &AST {
} }
} }
impl AddAssign<AST> for &mut AST { impl AddAssign<AST> for AST {
fn add_assign(&mut self, rhs: AST) { fn add_assign(&mut self, rhs: AST) {
self.combine(rhs); self.combine(rhs);
} }