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
use crate::model::*;

/// Some types have a C -> C++ conversion that is context independent
pub(crate) trait ToCpp {
    fn to_cpp(&self, expr: String) -> String;
}

impl ToCpp for DurationType {
    fn to_cpp(&self, expr: String) -> String {
        match self {
            DurationType::Milliseconds => format!("::convert::from_milli_sec_u64({expr})"),
            DurationType::Seconds => format!("::convert::from_sec_u64({expr})"),
        }
    }
}

impl<D> ToCpp for Handle<Enum<D>>
where
    D: DocReference,
{
    fn to_cpp(&self, expr: String) -> String {
        format!("::convert::to_cpp({expr})")
    }
}

impl ToCpp for Primitive {
    fn to_cpp(&self, expr: String) -> String {
        match self {
            Self::Bool => expr,
            Self::U8 => expr,
            Self::S8 => expr,
            Self::U16 => expr,
            Self::S16 => expr,
            Self::U32 => expr,
            Self::S32 => expr,
            Self::U64 => expr,
            Self::S64 => expr,
            Self::Float => expr,
            Self::Double => expr,
        }
    }
}

impl ToCpp for BasicType {
    fn to_cpp(&self, expr: String) -> String {
        match self {
            Self::Primitive(x) => x.to_cpp(expr),
            Self::Duration(x) => x.to_cpp(expr),
            Self::Enum(x) => x.to_cpp(expr),
        }
    }
}

impl ToCpp for StringType {
    fn to_cpp(&self, expr: String) -> String {
        format!("std::string({expr})")
    }
}

impl ToCpp for ClassDeclarationHandle {
    fn to_cpp(&self, expr: String) -> String {
        format!("::convert::to_cpp({expr})")
    }
}