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
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::model::*;

/// the raw JNI type for non-primitives (except strings which have their own special type)
const JNI_SYS_JOBJECT: &str = "jni::sys::jobject";

/// Trait for types used in JNI function signatures
pub(crate) trait JniSignatureType {
    /// get the raw JNI type (from jni::sys::* module) used in
    /// the Rust JNI function signatures
    fn jni_signature_type(&self) -> &str;
}

impl JniSignatureType for Primitive {
    fn jni_signature_type(&self) -> &str {
        match self {
            Self::Bool => "jni::sys::jboolean",
            Self::U8 => JNI_SYS_JOBJECT,
            Self::S8 => "jni::sys::jbyte",
            Self::U16 => JNI_SYS_JOBJECT,
            Self::S16 => "jni::sys::jshort",
            Self::U32 => JNI_SYS_JOBJECT,
            Self::S32 => "jni::sys::jint",
            Self::U64 => JNI_SYS_JOBJECT,
            Self::S64 => "jni::sys::jlong",
            Self::Float => "jni::sys::jfloat",
            Self::Double => "jni::sys::jdouble",
        }
    }
}

impl JniSignatureType for DurationType {
    fn jni_signature_type(&self) -> &str {
        JNI_SYS_JOBJECT
    }
}

impl JniSignatureType for EnumHandle {
    fn jni_signature_type(&self) -> &str {
        JNI_SYS_JOBJECT
    }
}

impl JniSignatureType for BasicType {
    fn jni_signature_type(&self) -> &str {
        match self {
            BasicType::Primitive(x) => x.jni_signature_type(),
            BasicType::Duration(x) => x.jni_signature_type(),
            BasicType::Enum(x) => x.jni_signature_type(),
        }
    }
}

impl JniSignatureType for StringType {
    fn jni_signature_type(&self) -> &str {
        "jni::sys::jstring"
    }
}

impl JniSignatureType for CollectionHandle {
    fn jni_signature_type(&self) -> &str {
        JNI_SYS_JOBJECT
    }
}

impl<T> JniSignatureType for UniversalOr<T>
where
    T: StructFieldType,
{
    fn jni_signature_type(&self) -> &str {
        JNI_SYS_JOBJECT
    }
}

impl<T> JniSignatureType for TypedStructDeclaration<T> {
    fn jni_signature_type(&self) -> &str {
        JNI_SYS_JOBJECT
    }
}

impl<T> JniSignatureType for UniversalDeclarationOr<T>
where
    T: StructFieldType,
{
    fn jni_signature_type(&self) -> &str {
        JNI_SYS_JOBJECT
    }
}

impl JniSignatureType for ClassDeclarationHandle {
    fn jni_signature_type(&self) -> &str {
        JNI_SYS_JOBJECT
    }
}

impl JniSignatureType for InterfaceHandle {
    fn jni_signature_type(&self) -> &str {
        JNI_SYS_JOBJECT
    }
}

impl JniSignatureType for FunctionArgument {
    fn jni_signature_type(&self) -> &str {
        match self {
            FunctionArgument::Basic(x) => x.jni_signature_type(),
            FunctionArgument::String(x) => x.jni_signature_type(),
            FunctionArgument::Collection(x) => x.jni_signature_type(),
            FunctionArgument::Struct(x) => x.jni_signature_type(),
            FunctionArgument::StructRef(x) => x.jni_signature_type(),
            FunctionArgument::ClassRef(x) => x.jni_signature_type(),
            FunctionArgument::Interface(x) => x.jni_signature_type(),
        }
    }
}

impl JniSignatureType for PrimitiveRef {
    fn jni_signature_type(&self) -> &str {
        JNI_SYS_JOBJECT
    }
}

impl JniSignatureType for FunctionReturnValue {
    fn jni_signature_type(&self) -> &str {
        match self {
            FunctionReturnValue::Basic(x) => x.jni_signature_type(),
            FunctionReturnValue::String(x) => x.jni_signature_type(),
            FunctionReturnValue::ClassRef(x) => x.jni_signature_type(),
            FunctionReturnValue::Struct(x) => x.jni_signature_type(),
            FunctionReturnValue::StructRef(x) => x.jni_signature_type(),
            FunctionReturnValue::PrimitiveRef(x) => x.jni_signature_type(),
        }
    }
}