sails_idl_meta/
lib.rs

1#![no_std]
2
3use scale_info::{MetaType, StaticTypeInfo, prelude::vec::Vec};
4
5pub type AnyServiceMetaFn = fn() -> AnyServiceMeta;
6
7pub trait ServiceMeta {
8    type CommandsMeta: StaticTypeInfo;
9    type QueriesMeta: StaticTypeInfo;
10    type EventsMeta: StaticTypeInfo;
11    const BASE_SERVICES: &'static [AnyServiceMetaFn];
12    const ASYNC: bool;
13
14    fn commands() -> MetaType {
15        MetaType::new::<Self::CommandsMeta>()
16    }
17
18    fn queries() -> MetaType {
19        MetaType::new::<Self::QueriesMeta>()
20    }
21
22    fn events() -> MetaType {
23        MetaType::new::<Self::EventsMeta>()
24    }
25
26    fn base_services() -> impl Iterator<Item = AnyServiceMeta> {
27        Self::BASE_SERVICES.iter().map(|f| f())
28    }
29}
30
31pub struct AnyServiceMeta {
32    commands: MetaType,
33    queries: MetaType,
34    events: MetaType,
35    base_services: Vec<AnyServiceMeta>,
36}
37
38impl AnyServiceMeta {
39    pub fn new<S: ServiceMeta>() -> Self {
40        Self {
41            commands: S::commands(),
42            queries: S::queries(),
43            events: S::events(),
44            base_services: S::base_services().collect(),
45        }
46    }
47
48    pub fn commands(&self) -> &MetaType {
49        &self.commands
50    }
51
52    pub fn queries(&self) -> &MetaType {
53        &self.queries
54    }
55
56    pub fn events(&self) -> &MetaType {
57        &self.events
58    }
59
60    pub fn base_services(&self) -> impl Iterator<Item = &AnyServiceMeta> {
61        self.base_services.iter()
62    }
63}
64
65pub trait ProgramMeta {
66    type ConstructorsMeta: StaticTypeInfo;
67    const SERVICES: &'static [(&'static str, AnyServiceMetaFn)];
68    const ASYNC: bool;
69
70    fn constructors() -> MetaType {
71        MetaType::new::<Self::ConstructorsMeta>()
72    }
73
74    fn services() -> impl Iterator<Item = (&'static str, AnyServiceMeta)> {
75        Self::SERVICES.iter().map(|(s, f)| (*s, f()))
76    }
77}