Skip to content

Commit 10a4bc4

Browse files
committed
Shared Binding Objects uniffi
1 parent 9eb77e2 commit 10a4bc4

File tree

10 files changed

+95
-38
lines changed

10 files changed

+95
-38
lines changed

bindings/lni_nodejs/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export interface PhoenixdConfig {
1010
url: string
1111
password: string
1212
}
13+
export interface PhoenixdNode {
14+
url: string
15+
password: string
16+
}
1317
export declare class Fetcher {
1418
constructor()
1519
getIpAddress(): Promise<Ip>

bindings/lni_uniffi/Cargo.toml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,21 @@ license = "MPL-2.0"
66
publish = false
77

88
[lib]
9-
crate-type = ["lib", "cdylib", "staticlib"]
9+
crate-type = ["cdylib", "staticlib"]
1010
name = "lni_uniffi"
1111

1212
[dependencies]
13-
lni = { path = "../../crates/lni" }
13+
lni = { path = "../../crates/lni", features = ["uniffi_rs"] }
1414
async-trait = "0.1"
15-
uniffi = { version = "0.28" }
15+
uniffi = { version = "0.28", features = ["cli"] }
1616
thiserror = "1.0"
1717
serde = { version = "1", features=["derive"] }
1818
serde_json = "1"
1919
reqwest = { version = "0.10", default-features = false, features = ["json", "rustls-tls", "blocking"] }
2020
tokio = { version = "1", features = ["full"] }
21-
napi = { version = "2.12.2", default-features = false, features = ["napi4"] }
22-
napi-derive = "2.12.2"
2321

2422
[build-dependencies]
2523
uniffi = { version = "0.28", features = ["build"] }
2624

2725
[dev-dependencies]
2826
uniffi = { version = "0.28", features = ["bindgen-tests"] }
29-
30-
[features]
31-
napi_rs = []
32-
default = []

bindings/lni_uniffi/build.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

bindings/lni_uniffi/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
uniffi::setup_scaffolding!("lni");
12
mod api_client;
3+
mod phoenixd;
24

3-
pub use lni::{ApiError, Ip, Result};
45
pub use api_client::Fetcher;
5-
uniffi::include_scaffolding!("lni");
6+
pub use lni::phoenixd::lib::PhoenixdNode;
7+
pub use lni::{ApiError, Ip, Result};

bindings/lni_uniffi/src/lni.udl

Lines changed: 0 additions & 21 deletions
This file was deleted.

bindings/lni_uniffi/src/phoenixd.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use lni::phoenixd::lib::PhoenixdConfig;
2+
use std::sync::Arc;
3+
4+
#[derive(uniffi::Object)]
5+
pub struct PhoenixdNode {
6+
inner: PhoenixdConfig,
7+
}
8+
9+
#[uniffi::export]
10+
impl PhoenixdNode {
11+
#[uniffi::constructor]
12+
pub fn new(config: PhoenixdConfig) -> Self {
13+
Self { inner: config }
14+
}
15+
16+
pub fn get_url(&self) -> String {
17+
self.inner.url.clone()
18+
}
19+
20+
pub fn get_password(&self) -> String {
21+
self.inner.password.clone()
22+
}
23+
24+
pub fn get_config(&self) -> PhoenixdConfig {
25+
PhoenixdConfig {
26+
url: self.inner.url.clone(),
27+
password: self.inner.password.clone(),
28+
}
29+
}
30+
31+
pub async fn get_offer(&self) {
32+
match lni::phoenixd::api::get_offer(self.inner.url.clone(), self.inner.password.clone())
33+
.await
34+
{
35+
Ok(offer) => Ok(offer),
36+
Err(e) => Err(e),
37+
};
38+
}
39+
}

crates/lni/Cargo.toml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,31 @@ name = "lni"
88
path = "lib.rs"
99

1010
[dependencies]
11-
reqwest = { version = "0.10", default-features = false, features = ["json", "rustls-tls", "blocking"] }
11+
reqwest = { version = "0.10", default-features = false, features = [
12+
"json",
13+
"rustls-tls",
14+
"blocking",
15+
] }
1216
async-trait = "0.1"
1317
thiserror = "1.0"
14-
serde = { version = "1", features=["derive"] }
18+
serde = { version = "1", features = ["derive"] }
1519
serde_json = "1"
1620
napi = { version = "2.12.2", default-features = false, features = ["napi4"] }
1721
napi-derive = "2.12.2"
22+
uniffi = { version = "0.28", optional = true }
23+
uniffi_macros = { version = "0.28", optional = true }
24+
25+
[build-dependencies]
26+
uniffi = { version = "0.28", features = ["build"], optional = true }
27+
28+
[dev-dependencies]
29+
uniffi = { version = "0.28", features = ["bindgen-tests"] }
1830

1931
[profile.release]
2032
lto = true
2133
strip = "symbols"
2234

2335
[features]
2436
napi_rs = []
25-
default = []
37+
uniffi_rs = ["uniffi", "uniffi_macros"]
38+
default = []

crates/lni/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use reqwest;
33
use napi_derive::napi;
44
#[cfg(feature = "napi_rs")]
55
use napi::bindgen_prelude::*;
6+
#[cfg(feature = "uniffi_rs")]
7+
uniffi::setup_scaffolding!();
68

79
#[derive(Debug, thiserror::Error)]
810
pub enum ApiError {

crates/lni/phoenixd/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
use napi_derive::napi;
33

44
#[cfg_attr(feature = "napi_rs", napi(object))]
5+
#[cfg_attr(feature = "uniffi_rs", derive(uniffi::Record))]
56
pub struct PhoenixdConfig {
67
pub url: String,
78
pub password: String,
89
}
910

11+
#[cfg_attr(feature = "napi_rs", napi(object))]
12+
#[cfg_attr(feature = "uniffi_rs", derive(uniffi::Record))]
1013
pub struct PhoenixdNode {
1114
pub url: String,
1215
pub password: String,

readme.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,30 @@ Bindings
129129
- https://mozilla.github.io/uniffi-rs/latest/
130130
- `cd bindings/lni_uniffi && cargo build`
131131
132+
Shared Binding Objects
133+
====================
134+
If you do not want to copy objects to the foreign language bindings we can simply use the features `napi_rs` or `uniffi_rs`
135+
to turn on or off language specific decorators and then implement them in their respective bindings project.
136+
137+
Example:
138+
```
139+
#[cfg(feature = "napi_rs")]
140+
use napi_derive::napi;
141+
142+
#[cfg_attr(feature = "napi_rs", napi(object))]
143+
#[cfg_attr(feature = "uniffi_rs", derive(uniffi::Record))]
144+
pub struct PhoenixdConfig {
145+
pub url: String,
146+
pub password: String,
147+
}
148+
149+
#[cfg_attr(feature = "napi_rs", napi(object))]
150+
#[cfg_attr(feature = "uniffi_rs", derive(uniffi::Record))]
151+
pub struct PhoenixdNode {
152+
pub url: String,
153+
pub password: String,
154+
}
155+
```
132156
133157
Tor
134158
===

0 commit comments

Comments
 (0)