summaryrefslogtreecommitdiffstats
path: root/src/failable/ops
diff options
context:
space:
mode:
Diffstat (limited to 'src/failable/ops')
-rw-r--r--src/failable/ops/and.rs35
-rw-r--r--src/failable/ops/bool.rs40
-rw-r--r--src/failable/ops/map.rs54
-rw-r--r--src/failable/ops/mod.rs12
-rw-r--r--src/failable/ops/not.rs33
-rw-r--r--src/failable/ops/or.rs35
-rw-r--r--src/failable/ops/xor.rs36
7 files changed, 245 insertions, 0 deletions
diff --git a/src/failable/ops/and.rs b/src/failable/ops/and.rs
new file mode 100644
index 0000000..1d195a6
--- /dev/null
+++ b/src/failable/ops/and.rs
@@ -0,0 +1,35 @@
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+//
+
+//! AND implementation.
+//!
+//! Will be automatically included when including `filter::Filter`, so importing this module
+//! shouldn't be necessary.
+//!
+
+use failable::filter::FailableFilter;
+
+#[must_use = "filters are lazy and do nothing unless consumed"]
+#[derive(Clone)]
+pub struct FailableAnd<T, U>(T, U);
+
+impl<T, U> FailableAnd<T, U> {
+
+ pub fn new(a: T, b: U) -> FailableAnd<T, U> {
+ FailableAnd(a, b)
+ }
+
+}
+
+impl<N, E, T, U> FailableFilter<N, E> for FailableAnd<T, U>
+ where T: FailableFilter<N, E>,
+ U: FailableFilter<N, E>
+{
+ fn filter(&self, e: &N) -> Result<bool, E> {
+ Ok(try!(self.0.filter(e)) && try!(self.1.filter(e)))
+ }
+}
+
diff --git a/src/failable/ops/bool.rs b/src/failable/ops/bool.rs
new file mode 100644
index 0000000..82c0ac9
--- /dev/null
+++ b/src/failable/ops/bool.rs
@@ -0,0 +1,40 @@
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+//
+
+//! FailableBool Filter implementation, so we can insert this in filter construction
+//!
+//! Will be automatically included when including `filter::Filter`, so importing this module
+//! shouldn't be necessary.
+//!
+
+use failable::filter::FailableFilter;
+
+#[must_use = "filters are lazy and do nothing unless consumed"]
+#[derive(Clone)]
+pub struct FailableBool(bool);
+
+impl FailableBool {
+
+ pub fn new(b: bool) -> FailableBool {
+ FailableBool(b)
+ }
+
+}
+
+impl From<bool> for FailableBool {
+
+ fn from(b: bool) -> FailableBool {
+ FailableBool::new(b)
+ }
+
+}
+
+impl<N, E> FailableFilter<N, E> for FailableBool {
+ fn filter(&self, _: &N) -> Result<bool, E> {
+ Ok(self.0)
+ }
+}
+
diff --git a/src/failable/ops/map.rs b/src/failable/ops/map.rs
new file mode 100644
index 0000000..ccec3d3
--- /dev/null
+++ b/src/failable/ops/map.rs
@@ -0,0 +1,54 @@
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+//
+
+//! Map implementation.
+//!
+//! Will be automatically included when including `filter::Filter`, so importing this module
+//! shouldn't be necessary.
+//!
+use std::marker::PhantomData;
+use std::borrow::Borrow;
+
+use failable::filter::FailableFilter;
+
+#[must_use = "filters are lazy and do nothing unless consumed"]
+#[derive(Clone)]
+pub struct FailableMapInput<F, M, FT, B>(F, M, PhantomData<FT>, PhantomData<B>);
+
+impl<F, M, FT, B> FailableMapInput<F, M, FT, B> {
+ pub fn new(a: F, m: M) -> FailableMapInput<F, M, FT, B> {
+ FailableMapInput(a, m, PhantomData, PhantomData)
+ }
+}
+
+impl<FT, E, F, T, B, M> FailableFilter<T, E> for FailableMapInput<F, M, FT, B>
+ where F: FailableFilter<FT, E>,
+ B: Borrow<FT> + Sized,
+ M: Fn(&T) -> B
+{
+ fn filter(&self, e: &T) -> Result<bool, E> {
+ self.0.filter(self.1(e).borrow())
+ }
+}
+
+#[must_use = "filters are lazy and do nothing unless consumed"]
+#[derive(Clone)]
+pub struct FailableMapErr<F, M, FE, E>(F, M, PhantomData<FE>, PhantomData<E>);
+
+impl<F, M, FE, E> FailableMapErr<F, M, FE, E> {
+ pub fn new(a: F, m: M) -> FailableMapErr<F, M, FE, E> {
+ FailableMapErr(a, m, PhantomData, PhantomData)
+ }
+}
+
+impl<FE, E, F, T, M> FailableFilter<T, E> for FailableMapErr<F, M, FE, E>
+ where F: FailableFilter<T, FE>,
+ M: Fn(FE) -> E
+{
+ fn filter(&self, e: &T) -> Result<bool, E> {
+ self.0.filter(e).map_err(&self.1)
+ }
+}
diff --git a/src/failable/ops/mod.rs b/src/failable/ops/mod.rs
new file mode 100644
index 0000000..618c0e8
--- /dev/null
+++ b/src/failable/ops/mod.rs
@@ -0,0 +1,12 @@
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+//
+
+pub mod and;
+pub mod bool;
+pub mod not;
+pub mod xor;
+pub mod or;
+pub mod map;
diff --git a/src/failable/ops/not.rs b/src/failable/ops/not.rs
new file mode 100644
index 0000000..25c3e3b
--- /dev/null
+++ b/src/failable/ops/not.rs
@@ -0,0 +1,33 @@
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+//
+
+//! NOT implementation.
+//!
+//! Will be automatically included when including `filter::Filter`, so importing this module
+//! shouldn't be necessary.
+//!
+
+use failable::filter::FailableFilter;
+
+#[must_use = "filters are lazy and do nothing unless consumed"]
+#[derive(Clone)]
+pub struct FailableNot<T>(T);
+
+impl<T> FailableNot<T> {
+
+ pub fn new(a: T) -> FailableNot<T> {
+ FailableNot(a)
+ }
+
+}
+
+impl<N, E, T> FailableFilter<N, E> for FailableNot<T>
+ where T: FailableFilter<N, E>
+{
+ fn filter(&self, e: &N) -> Result<bool, E> {
+ self.0.filter(e).map(|b| !b)
+ }
+}
diff --git a/src/failable/ops/or.rs b/src/failable/ops/or.rs
new file mode 100644
index 0000000..45289c3
--- /dev/null
+++ b/src/failable/ops/or.rs
@@ -0,0 +1,35 @@
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+//
+
+//! OR implementation.
+//!
+//! Will be automatically included when including `filter::Filter`, so importing this module
+//! shouldn't be necessary.
+//!
+
+use failable::filter::FailableFilter;
+
+#[must_use = "filters are lazy and do nothing unless consumed"]
+#[derive(Clone)]
+pub struct FailableOr<T, U>(T, U);
+
+impl<T, U> FailableOr<T, U> {
+
+ pub fn new(a: T, b: U) -> FailableOr<T, U> {
+ FailableOr(a, b)
+ }
+
+}
+
+impl<N, E, T, U> FailableFilter<N, E> for FailableOr<T, U>
+ where T: FailableFilter<N, E>,
+ U: FailableFilter<N, E>
+{
+ fn filter(&self, e: &N) -> Result<bool, E> {
+ Ok(try!(self.0.filter(e)) || try!(self.1.filter(e)))
+ }
+}
+
diff --git a/src/failable/ops/xor.rs b/src/failable/ops/xor.rs
new file mode 100644
index 0000000..ecc38ce
--- /dev/null
+++ b/src/failable/ops/xor.rs
@@ -0,0 +1,36 @@
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+//
+
+//! XOR implementation.
+//!
+//! Will be automatically included when including `filter::Filter`, so importing this module
+//! shouldn't be necessary.
+//!
+
+use failable::filter::FailableFilter;
+
+#[must_use = "filters are lazy and do nothing unless consumed"]
+#[derive(Clone)]
+pub struct FailableXOr<T, U>(T, U);
+
+impl<T, U> FailableXOr<T, U> {
+
+ pub fn new(a: T, b: U) -> FailableXOr<T, U> {
+ FailableXOr(a, b)
+ }
+
+}
+
+impl<N, E, T, U> FailableFilter<N, E> for FailableXOr<T, U>
+ where T: FailableFilter<N, E>,
+ U: FailableFilter<N, E>
+{
+ fn filter(&self, e: &N) -> Result<bool, E> {
+ Ok(try!(self.0.filter(e)) ^ try!(self.1.filter(e)))
+ }
+}
+
+