use core::marker::PhantomData; use crate::env::*; use crate::ream::*; /// A set of sketches. /// /// This struct doesn't provide any new functionallity; It just allows a nicer to read API for the /// user. #[derive(Debug)] pub struct Book { phantom_data: PhantomData, } /// Pages of a book. /// /// This exists for reams to add functionallity to it. pub struct Pages where R: Ream, { /// The ream for the pages. pub ream: R, /// The environment the pages are in. pub env: ::Env, } impl core::fmt::Debug for Pages where R: Ream + core::fmt::Debug, ::Env: core::fmt::Debug, { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.debug_struct("Pages") .field("ream", &self.ream) .field("env", &self.env) .finish() } } impl Pages where R: Ream + 'static, { /// Bind into a book. /// /// This runs the ream and all sketches it contains. /// /// Returns what the environment returns. pub fn bind(self) -> <::Env as Environment>::Return { let mut ream = self.ream; self.env.run(move |proxy| ream.update(proxy)) } } impl Book where R: Default + Ream + 'static, ::Env: Default, { /// Create pages to add sketches to. pub fn pages() -> Pages { Pages { ream: R::default(), env: ::Env::default(), } } /// Immediately bind a new book. /// /// This will start running the ream and therefore any sketches. pub fn bind() -> <::Env as Environment>::Return { Self::pages().bind() } } impl Book where R: Ream, ::Env: Default, { /// Create pages with input data. pub fn pages_with(input: I) -> Pages where R: From, { Pages { ream: R::from(input), env: ::Env::default(), } } /// Bind the book with some input value. pub fn bind_with(input: I) -> <::Env as Environment>::Return where R: From + 'static, { Self::pages_with(input).bind() } }