maplibre/tcs/system/
mod.rs

1use std::borrow::Cow;
2
3use thiserror::Error;
4
5use crate::{context::MapContext, tcs::system::function::IntoSystem};
6
7mod function;
8pub mod stage;
9
10#[derive(Error, Debug)]
11pub enum SystemError {
12    #[error("renderer was setup wrong")]
13    Setup,
14    #[error("dependencies were not resolvable")]
15    Dependencies,
16}
17
18pub type SystemResult = Result<(), SystemError>;
19
20/// An system that can be added to a [`Schedule`](crate::schedule::Schedule)
21pub trait System: 'static {
22    /// Returns the system's name.
23    fn name(&self) -> Cow<'static, str>;
24
25    fn run(&mut self, context: &mut MapContext) -> SystemResult;
26}
27
28/// A convenience type alias for a boxed [`System`] trait object.
29pub type BoxedSystem = Box<dyn System>;
30
31pub struct SystemContainer {
32    system: BoxedSystem,
33}
34
35impl SystemContainer {
36    pub fn new<S: System>(system: S) -> Self {
37        Self {
38            system: Box::new(system),
39        }
40    }
41}
42
43pub trait IntoSystemContainer {
44    fn into_container(self) -> SystemContainer;
45}
46
47impl<S> IntoSystemContainer for S
48where
49    S: IntoSystem,
50{
51    fn into_container(self) -> SystemContainer {
52        SystemContainer {
53            system: Box::new(IntoSystem::into_system(self)),
54        }
55    }
56}
57
58impl IntoSystemContainer for SystemContainer {
59    fn into_container(self) -> SystemContainer {
60        self
61    }
62}