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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//! Factory binding from configuration keys to runtime child specs.
//!
//! The functions in this module keep executable task factories out of raw YAML
//! loading while still providing a single startup-time binding step.
use crateSupervisorError;
use crate;
use crateTaskFactoryRegistry;
/// Binds all worker child factory keys to executable task factories.
///
/// # Arguments
///
/// - `children`: Child specifications converted from configuration.
/// - `registry`: Registry that owns the executable factories.
///
/// # Returns
///
/// Returns `Ok(())` when every worker child has a usable factory.
///
/// # Errors
///
/// Returns [`SupervisorError`] when a worker is missing `factory_key`, a key is
/// unknown, a key does not support the declared task kind, or a supervisor child
/// declares a factory key.
///
/// # Examples
///
/// ```
/// use rust_supervisor::config::factory_binding::bind_task_factories;
/// use rust_supervisor::id::types::ChildId;
/// use rust_supervisor::spec::child_builder::ChildSpecBuilder;
/// use rust_supervisor::task::factory::{TaskResult, service_fn};
/// use rust_supervisor::task::factory_registry::{
/// TaskFactoryDescriptor, TaskFactoryRegistry,
/// };
/// use rust_supervisor::spec::child::TaskKind;
/// use std::sync::Arc;
///
/// # fn example() -> Result<(), rust_supervisor::error::types::SupervisorError> {
/// let mut registry = TaskFactoryRegistry::new();
/// registry.register(TaskFactoryDescriptor::new(
/// "worker",
/// "Worker",
/// "Runs one worker.",
/// [TaskKind::AsyncWorker],
/// Arc::new(service_fn(|_ctx| async { TaskResult::Succeeded })),
/// ))?;
/// let child = ChildSpecBuilder::new(ChildId::new("worker"), "worker")
/// .kind(TaskKind::AsyncWorker)
/// .factory_key("worker")
/// .factory(Arc::new(service_fn(|_ctx| async { TaskResult::Succeeded })))
/// .build()?;
/// let mut children = vec![child];
/// bind_task_factories(&mut children, ®istry)?;
/// assert!(children[0].factory.is_some());
/// # Ok(())
/// # }
/// ```
/// Binds one child factory key to an executable task factory.
///
/// # Arguments
///
/// - `child`: Child specification converted from configuration.
/// - `registry`: Registry that owns executable task factories.
///
/// # Returns
///
/// Returns `Ok(())` after the child has a valid factory assignment or does not
/// require one.
///
/// # Errors
///
/// Returns [`SupervisorError`] when the child factory declaration is invalid.