ctxbuilder/lib.rs
1#![warn(missing_docs)]
2//! # Context-Based Builders
3//!
4//! Build Rust objects based on shared [`Context`]. This is useful when you need to generate
5//! multiple objects based on a set of similar properties, such as in preparation for unit
6//! tests.
7
8use std::{
9 any::{Any, TypeId},
10 collections::HashMap,
11};
12
13mod context;
14pub use context::{Context, MainContext, SubContext};
15mod entry;
16pub use entry::Entry;
17mod impls;
18pub mod prelude;
19
20/// Trait to build an object based on a shared [`Context`]
21pub trait Builder: Sized {
22 /// Build a new object based on the [`Context`]
23 fn build<C: Context>(ctx: &mut C) -> Self;
24}
25
26/// Trait to build an object based on a shared [`Context`] and name
27pub trait NamedBuilder: Sized {
28 /// Build a new object based on a static name and the [`Context`]
29 fn build_with_name<C: Context>(ctx: &mut C, name: &'static str) -> Self;
30}
31
32type AnyMap = HashMap<(TypeId, Option<&'static str>), Box<dyn Any + Send + Sync>>;
33
34/// Create a new [`MainContext`]
35pub fn ctx() -> MainContext {
36 MainContext::new()
37}