1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use core::fmt::{Debug, Display};
pub mod builders;

use effectful::environment::{DynBind, EnvConfig, Environment, NativeForm};

use crate::{
    protocol::{AsVisitor, DynVisitor},
};

/// A buildable type.
pub trait Build<'ctx, M, E: Environment>: Sized + DynBind<E> {
    /// The builder that can be used to build a value of `Self`.
    type Builder: Builder<'ctx, E, Value = Self>;
}

pub trait BuilderTypes<C: EnvConfig> {
    type Seed: DynBind<C>;

    /// Error that can happen during filling the builder with data.
    type Error: DynBind<C> + Debug + Display;

    /// Type to be built.
    type Value: DynBind<C>;
}

/// Builder for a type.
///
/// The `'ctx` lifetime is some lifetime that is longer than the walker.
/// As such, the built value can borrow from other data with a `'ctx` lifetimes.
///
/// A builder allows creating a value of a type [`Self::Value`][BuilderTypes::Value].
/// The way to use a builder is as follows.
/// - Call [`Default::default()`] to create an instance of the builder.
/// - Call [`Self::as_visitor()`] and give it to a walker's
///     [`walk()`][crate::walk::Walker::walk]. The walker will then fill
///     the builder with data from it's walk.
/// - Call [`Self::build()`] to finish building the value and get any errors
///     that happened during filling it with data.
pub trait Builder<'ctx, E: Environment>: DynBind<E> + AsVisitor<'ctx, E> + BuilderTypes<E> + Sized {
    fn from_seed<'a>(seed: Self::Seed) -> NativeForm<'a, Self, E>
    where
        Self: 'a;

    /// Finish the value.
    ///
    /// If an error happened with the builder during the walk
    /// it will be reported here.
    fn build<'a>(self) -> NativeForm<'a, Result<Self::Value, Self::Error>, E>
    where
        Self: 'a;
}