Skip to content

Commit f6ee472

Browse files
committed
WIP #73 fp-bindgen
1 parent 3b90fdf commit f6ee472

File tree

17 files changed

+965
-29
lines changed

17 files changed

+965
-29
lines changed

Cargo.lock

-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[workspace]
2+
exclude = ["plugin-example"]
23
members = [
34
"server",
45
"cli",
56
"lib",
67
"desktop",
7-
"plugin-example",
88
]

lib/Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ name = "atomic_lib"
77
readme = "README.md"
88
repository = "https://github.com/joepio/atomic-data-rust"
99
version = "0.31.1"
10+
11+
[[bin]]
12+
name = "generate-bindings"
13+
path = "src/plugins/bindings.rs"
14+
1015
# Enables benchmarks to use the features, such as Db
1116
[[bench]]
1217
all-features = true

lib/bindings/rust-plugin/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "atomic-bindings"
3+
version = "0.0.1"
4+
authors = ["Joep Meindertsma <[email protected]>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
fp-bindgen-support = { path = "../../../../fp-bindgen-support", version = "1.0.0", features = ["async", "guest", "http"] }
9+
http = { version = "0.2" }
10+
once_cell = { version = "1.4" }
11+
rmp-serde = { version = "1.0" }
12+
serde = { version = "1.0", features = ["derive"] }
13+
serde_bytes = { version = "0.11" }
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use crate::types::*;
2+
3+
#[fp_bindgen_support::fp_export_signature]
4+
pub async fn fetch_data(url: String) -> String;
5+
6+
#[fp_bindgen_support::fp_export_signature]
7+
pub async fn my_async_exported_function() -> ComplexGuestToHost;
8+
9+
#[fp_bindgen_support::fp_export_signature]
10+
pub fn my_complex_exported_function(a: ComplexHostToGuest) -> ComplexAlias;
11+
12+
#[fp_bindgen_support::fp_export_signature]
13+
pub fn my_plain_exported_function(a: u32, b: u32) -> u32;
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use crate::types::*;
2+
3+
#[fp_bindgen_support::fp_import_signature]
4+
pub fn count_words(string: String) -> Result<u16, String>;
5+
6+
#[fp_bindgen_support::fp_import_signature]
7+
/// Logs a message to the (development) console.
8+
pub fn log(message: String);
9+
10+
#[fp_bindgen_support::fp_import_signature]
11+
pub async fn make_request(opts: RequestOptions) -> Result<Response, RequestError>;
12+
13+
#[fp_bindgen_support::fp_import_signature]
14+
pub async fn my_async_imported_function() -> ComplexHostToGuest;
15+
16+
#[fp_bindgen_support::fp_import_signature]
17+
/// This one passes complex data types. Things are getting interesting.
18+
pub fn my_complex_imported_function(a: ComplexAlias) -> ComplexHostToGuest;
19+
20+
#[fp_bindgen_support::fp_import_signature]
21+
/// This is a very simple function that only uses primitives. Our bindgen should have little
22+
/// trouble with this.
23+
pub fn my_plain_imported_function(a: u32, b: u32) -> u32;

lib/bindings/rust-plugin/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#[rustfmt::skip]
2+
mod export;
3+
#[rustfmt::skip]
4+
mod import;
5+
#[rustfmt::skip]
6+
mod types;
7+
8+
pub use export::*;
9+
pub use import::*;
10+
pub use types::*;
11+
12+
pub use fp_bindgen_support::*;

lib/bindings/rust-plugin/src/types.rs

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
use serde::{Deserialize, Serialize};
2+
use std::{collections::BTreeMap, collections::HashMap};
3+
4+
pub type Body = serde_bytes::ByteBuf;
5+
6+
pub type ComplexAlias = ComplexGuestToHost;
7+
8+
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
9+
pub struct ComplexGuestToHost {
10+
pub simple: Simple,
11+
pub map: BTreeMap<String, Simple>,
12+
}
13+
14+
/// Multi-line doc comment with complex characters
15+
/// & " , \ ! '
16+
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
17+
#[serde(rename_all = "camelCase")]
18+
pub struct ComplexHostToGuest {
19+
#[serde(flatten)]
20+
pub simple: Simple,
21+
pub list: Vec<f64>,
22+
pub points: Vec<Point<f64>>,
23+
pub recursive: Vec<Point<Point<f64>>>,
24+
#[serde(default, skip_serializing_if = "Option::is_none")]
25+
pub complex_nested: Option<BTreeMap<String, Vec<FloatingPoint>>>,
26+
27+
/// Raw identifiers are supported too.
28+
pub r#type: String,
29+
pub value: Value,
30+
}
31+
32+
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
33+
pub struct ExplicitedlyImportedType {
34+
pub you_will_see_this: bool,
35+
}
36+
37+
pub type FloatingPoint = Point<f64>;
38+
39+
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
40+
pub struct GroupImportedType1 {
41+
pub you_will_see_this: bool,
42+
}
43+
44+
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
45+
pub struct GroupImportedType2 {
46+
pub you_will_see_this: bool,
47+
}
48+
49+
/// Similar to the `RequestOptions` struct, but using types from the `http` crate.
50+
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
51+
#[serde(rename_all = "camelCase")]
52+
pub struct HttpRequestOptions {
53+
#[serde(deserialize_with = "fp_bindgen_support::http::deserialize_uri", serialize_with = "fp_bindgen_support::http::serialize_uri")]
54+
pub url: http::Uri,
55+
#[serde(deserialize_with = "fp_bindgen_support::http::deserialize_http_method", serialize_with = "fp_bindgen_support::http::serialize_http_method")]
56+
pub method: http::Method,
57+
pub headers: HashMap<String, String>,
58+
#[serde(default, skip_serializing_if = "Option::is_none")]
59+
pub body: Option<serde_bytes::ByteBuf>,
60+
}
61+
62+
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
63+
#[serde(rename_all = "PascalCase")]
64+
pub struct Point<T> {
65+
pub value: T,
66+
}
67+
68+
/// Represents an error with the request.
69+
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
70+
#[serde(tag = "type", rename_all = "snake_case")]
71+
pub enum RequestError {
72+
/// Used when we know we don't have an active network connection.
73+
Offline,
74+
NoRoute,
75+
ConnectionRefused,
76+
Timeout,
77+
#[serde(rename_all = "snake_case")]
78+
ServerError {
79+
/// HTTP status code.
80+
status_code: u16,
81+
82+
/// Response body.
83+
response: Body,
84+
},
85+
/// Misc.
86+
#[serde(rename = "other/misc")]
87+
Other { reason: String },
88+
}
89+
90+
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
91+
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
92+
pub enum RequestMethod {
93+
Delete,
94+
Get,
95+
Options,
96+
Post,
97+
Put,
98+
}
99+
100+
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
101+
#[serde(rename_all = "camelCase")]
102+
pub struct RequestOptions {
103+
pub url: String,
104+
pub method: RequestMethod,
105+
pub headers: HashMap<String, String>,
106+
#[serde(default, skip_serializing_if = "Option::is_none")]
107+
pub body: Option<serde_bytes::ByteBuf>,
108+
}
109+
110+
/// A response to a request.
111+
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
112+
#[serde(rename_all = "camelCase")]
113+
pub struct Response {
114+
/// Response headers, by name.
115+
pub headers: HashMap<String, String>,
116+
117+
/// Response body.
118+
pub body: Body,
119+
}
120+
121+
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
122+
pub struct Simple {
123+
pub foo: i32,
124+
pub bar: String,
125+
}
126+
127+
/// Tagged dynamic value.
128+
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
129+
pub enum Value {
130+
Integer(i64),
131+
Float(f64),
132+
List(Vec<Value>),
133+
Map(BTreeMap<String, Value>),
134+
}

0 commit comments

Comments
 (0)