oo_bindgen/model/
constants.rs

1use std::rc::Rc;
2
3use crate::model::*;
4
5/// How to render a numeric constant
6#[non_exhaustive]
7#[derive(Copy, Clone, Debug)]
8pub enum Representation {
9    Hex,
10}
11
12/// Types of constants available
13#[non_exhaustive]
14#[derive(Copy, Clone, Debug)]
15pub enum ConstantValue {
16    U8(u8, Representation),
17}
18
19/// Constant belonging to a set of constants
20#[derive(Debug)]
21pub(crate) struct Constant<T>
22where
23    T: DocReference,
24{
25    pub(crate) name: Name,
26    pub(crate) value: ConstantValue,
27    pub(crate) doc: Doc<T>,
28}
29
30impl Constant<Unvalidated> {
31    pub(crate) fn validate(&self, lib: &LibraryFields) -> BindResult<Constant<Validated>> {
32        Ok(Constant {
33            name: self.name.clone(),
34            value: self.value,
35            doc: self.doc.validate(&self.name, lib)?,
36        })
37    }
38}
39
40/// Set of constants
41#[derive(Debug)]
42pub struct ConstantSet<T>
43where
44    T: DocReference,
45{
46    /// name of the set
47    pub(crate) name: Name,
48    /// common library settings
49    pub(crate) settings: Rc<LibrarySettings>,
50    /// values
51    pub(crate) values: Vec<Constant<T>>,
52    /// documentation
53    pub(crate) doc: Doc<T>,
54}
55
56impl ConstantSet<Unvalidated> {
57    pub(crate) fn validate(
58        &self,
59        lib: &LibraryFields,
60    ) -> BindResult<Handle<ConstantSet<Validated>>> {
61        let values: BindResult<Vec<Constant<Validated>>> =
62            self.values.iter().map(|x| x.validate(lib)).collect();
63
64        Ok(Handle::new(ConstantSet {
65            name: self.name.clone(),
66            settings: self.settings.clone(),
67            values: values?,
68            doc: self.doc.validate(&self.name, lib)?,
69        }))
70    }
71}
72
73pub type ConstantSetHandle = Handle<ConstantSet<Unvalidated>>;