Diffstat (limited to 'src/impls/core/bool.rs')
| -rw-r--r-- | src/impls/core/bool.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/impls/core/bool.rs b/src/impls/core/bool.rs new file mode 100644 index 0000000..2e3aba9 --- /dev/null +++ b/src/impls/core/bool.rs @@ -0,0 +1,61 @@ +use crate::{ + build::{Build, Builder}, + implementer, + protocol::{ImplementerExt, Implementer}, + protocols::{bool, ControlFlow}, + walk::WalkOnce, +}; + +#[derive(thiserror::Error, Debug)] +#[error("The value is complete.")] +pub struct IncompleteValue; + +#[derive(Default)] +pub struct BoolBuilder(Option<bool>); + +impl<'ctx> Build<'ctx> for bool { + type Builder = BoolBuilder; +} + +impl<'ctx> Builder<'ctx> for BoolBuilder { + type Error = IncompleteValue; + + type Value = bool; + + fn as_visitor(&mut self) -> &mut dyn crate::protocol::Implementer<'ctx> { + self + } + + fn build(self) -> Result<Self::Value, Self::Error> { + match self.0 { + Some(value) => Ok(value), + None => Err(IncompleteValue), + } + } +} + +implementer! { + impl['ctx] BoolBuilder = [bool::Bool]; +} + +impl<'ctx> bool::Object<'ctx> for BoolBuilder { + fn visit(&mut self, value: bool) -> crate::protocols::ControlFlow { + self.0 = Some(value); + ControlFlow::Done + } +} + +impl<'ctx> WalkOnce<'ctx> for bool { + type Error = (); + + type Value = (); + + #[inline] + fn walk_once(self, visitor: &mut dyn Implementer<'ctx>) -> Result<Self::Value, Self::Error> { + if let Some(interface) = visitor.interface_for::<bool::Bool>() { + interface.as_object().visit(self); + } + + Ok(()) + } +} |