must 0.2.0

assertion library for rust
Documentation
use errors::{ErrorKind, dump};
use lazy::SimpleAssert;
use std::fmt::Debug;


/// Extension for [PartialEq][]
///
/// [PartialEq]:https://doc.rust-lang.org/std/cmp/trait.PartialEq.html
pub trait PartialEqMust<T: Debug>: Sized + Debug + PartialEq<T> {
	/// # Examples
	///
	/// ```rust
	/// #[macro_use] extern crate must;
	/// use must::prelude::*;
	/// # extern crate env_logger;
	/// # fn main() {
	/// # let _ = env_logger::init();
	/// // String implements Deref<str>, so it implements PartialEq<str>
	/// String::from("abcde").must_eq("abcde");
	/// # }
	/// ```
	fn must_be(self, to: T) -> SimpleAssert<Self> {
		if self == to {
			self.into()
		} else {
			ErrorKind::MustEq { to: dump(&to) }.but_got(self)
		}
	}

	fn must_eq(self, to: T) -> SimpleAssert<Self> {
		self.must_be(to)
	}

	fn must_not_be(self, to: T) -> SimpleAssert<Self> {
		if self != to {
			self.into()
		} else {
			ErrorKind::MustNotEq { to: dump(&to) }.but_got(self)
		}
	}

	fn must_not_eq(self, to: T) -> SimpleAssert<Self> {
		self.must_not_be(to)
	}
}

impl<T: Debug, V: Sized> PartialEqMust<T> for V where V: Debug + PartialEq<T> {}
// impl<'a, T: Debug, V: ?Sized> PartialEqMust<T> for &'a V where V: Debug + PartialEq<T> {}


pub trait MustBeBool: PartialEqMust<bool> {
	/// # Examples
	///
	/// ```rust
	/// #[macro_use] extern crate must;
	/// use must::prelude::*;
	/// # extern crate env_logger;
	/// # fn main() {
	/// # let _ = env_logger::init();
	/// true.must_be_true();
	/// # }
	/// ```
	fn must_be_true(self) -> SimpleAssert<Self> {
		self.must_eq(true)
	}

	fn must_be_false(self) -> SimpleAssert<Self> {
		self.must_be(false)
	}
}

impl<V: ?Sized> MustBeBool for V where V: PartialEqMust<bool> {}

pub trait MustBeDefault: Debug + PartialEq + Default {
	fn must_be_default(self) -> SimpleAssert<Self> {
		self.must_be(Self::default())
	}

	fn must_not_be_default(self) -> SimpleAssert<Self> {
		self.must_not_be(Self::default())
	}
}

impl<V: ?Sized> MustBeDefault for V where V: Debug + PartialEq + Default {}


fn _assert() {
	fn partial_eq<T: PartialEqMust<To>, To: Debug>() {}
	fn be_default<T: MustBeDefault>() {}
	fn be_bool<T: MustBeBool>() {}

	partial_eq::<&str, &str>();
	partial_eq::<String, &str>();
	partial_eq::<Vec<u8>, Vec<u8>>();
	partial_eq::<[u8; 32], [u8; 32]>();
	partial_eq::<&[u8], &[u8]>();

	be_default::<Option<i64>>();
	be_default::<i64>();

	be_bool::<bool>();
}