|
| 1 | +//! Reusable collection of JNI type bindings. |
| 2 | +//! |
| 3 | +//! [`JniTypeBinding`] aggregates a set of [`TypeBinding`]s plus callback |
| 4 | +//! registrations into a single value that can be defined once and ingested |
| 5 | +//! into many [`crate::jni_converter::Builder`] instances. Useful when a |
| 6 | +//! project emits several `JniConverter` outputs (e.g. one per JNI class) that |
| 7 | +//! share a common vocabulary of types. |
| 8 | +//! |
| 9 | +//! ```ignore |
| 10 | +//! use zenoh_flat::jni_converter::{ArgDecode, JniConverter, JniForm, TypeBinding}; |
| 11 | +//! use zenoh_flat::jni_type_binding::JniTypeBinding; |
| 12 | +//! |
| 13 | +//! let common = JniTypeBinding::new() |
| 14 | +//! .type_binding( |
| 15 | +//! TypeBinding::new("KeyExpr").consume( |
| 16 | +//! JniForm::new( |
| 17 | +//! "*const zenoh::key_expr::KeyExpr<'static>", |
| 18 | +//! "Long", |
| 19 | +//! ArgDecode::ConsumeArc, |
| 20 | +//! ) |
| 21 | +//! .pointer_param(true), |
| 22 | +//! ), |
| 23 | +//! ) |
| 24 | +//! .callback_decoder( |
| 25 | +//! "Sample", |
| 26 | +//! "crate::sample_callback::process_kotlin_sample_callback", |
| 27 | +//! "io.zenoh.jni.callbacks.JNISubscriberCallback", |
| 28 | +//! ); |
| 29 | +//! |
| 30 | +//! let session_converter = JniConverter::builder() |
| 31 | +//! .class_prefix("Java_io_zenoh_jni_JNISessionNative_") |
| 32 | +//! .jni_type_binding(common.clone()) |
| 33 | +//! // ...other builder calls... |
| 34 | +//! .build(); |
| 35 | +//! |
| 36 | +//! let publisher_converter = JniConverter::builder() |
| 37 | +//! .class_prefix("Java_io_zenoh_jni_JNIPublisherNative_") |
| 38 | +//! .jni_type_binding(common) |
| 39 | +//! // ...other builder calls... |
| 40 | +//! .build(); |
| 41 | +//! ``` |
| 42 | +
|
| 43 | +use std::collections::HashMap; |
| 44 | + |
| 45 | +use crate::jni_converter::TypeBinding; |
| 46 | + |
| 47 | +/// Reusable collection of [`TypeBinding`]s and callback registrations. |
| 48 | +/// |
| 49 | +/// Built fluently and consumed by |
| 50 | +/// [`crate::jni_converter::Builder::jni_type_binding`]. `Clone` so the same |
| 51 | +/// set can be ingested into multiple builders. |
| 52 | +#[derive(Default, Clone)] |
| 53 | +pub struct JniTypeBinding { |
| 54 | + pub(crate) types: HashMap<String, TypeBinding>, |
| 55 | + pub(crate) callback_decoders: HashMap<String, syn::Path>, |
| 56 | + pub(crate) callback_kotlin_types: HashMap<String, String>, |
| 57 | +} |
| 58 | + |
| 59 | +impl JniTypeBinding { |
| 60 | + pub fn new() -> Self { |
| 61 | + Self::default() |
| 62 | + } |
| 63 | + |
| 64 | + /// Add (or replace) a [`TypeBinding`] in this collection. |
| 65 | + pub fn type_binding(mut self, binding: TypeBinding) -> Self { |
| 66 | + self.types.insert(binding.name().to_string(), binding); |
| 67 | + self |
| 68 | + } |
| 69 | + |
| 70 | + /// Register a callback decoder. `element_type_name` is the last path |
| 71 | + /// segment of the callback's argument type (e.g. `"Sample"`); use |
| 72 | + /// `"()"` for zero-arg callbacks. The decoder must have signature |
| 73 | + /// `fn(&mut JNIEnv, JObject) -> ZResult<impl Fn(T) + Send + Sync + 'static>`. |
| 74 | + pub fn callback_decoder( |
| 75 | + mut self, |
| 76 | + element_type_name: impl Into<String>, |
| 77 | + decoder: impl AsRef<str>, |
| 78 | + kotlin_type: impl Into<String>, |
| 79 | + ) -> Self { |
| 80 | + let path: syn::Path = syn::parse_str(decoder.as_ref()) |
| 81 | + .expect("invalid callback_decoder path"); |
| 82 | + let name = element_type_name.into(); |
| 83 | + let kt = kotlin_type.into(); |
| 84 | + self.callback_decoders.insert(name.clone(), path); |
| 85 | + self.callback_kotlin_types.insert(name, kt); |
| 86 | + self |
| 87 | + } |
| 88 | + |
| 89 | + /// Merge another [`JniTypeBinding`] into this one. Entries in `other` |
| 90 | + /// override entries with the same key in `self`. |
| 91 | + pub fn merge(mut self, other: JniTypeBinding) -> Self { |
| 92 | + self.types.extend(other.types); |
| 93 | + self.callback_decoders.extend(other.callback_decoders); |
| 94 | + self.callback_kotlin_types.extend(other.callback_kotlin_types); |
| 95 | + self |
| 96 | + } |
| 97 | +} |
0 commit comments