Skip to content

Commit

Permalink
JSON support for promises
Browse files Browse the repository at this point in the history
  • Loading branch information
vigoo committed Jul 26, 2024
1 parent a4b2c34 commit f11cb54
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
45 changes: 45 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion golem-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
golem-rust-macro = { path = "../golem-rust-macro", version = "0.0.0", optional = true }
serde = { version = "1.0.204", optional = true }
serde_json = { version = "1.0.120", optional = true }
uuid = { version = "1.10.0", features = ["v4"], optional = true }
wit-bindgen-rt = { version = "0.26.0", features = ["bitflags"] }

[features]
default = ["macro", "uuid"]
default = ["json", "macro", "uuid"]
json = ["dep:serde", "dep:serde_json"]
macro = ["dep:golem-rust-macro"]
uuid = ["dep:uuid"]

Expand Down
34 changes: 34 additions & 0 deletions golem-rust/src/json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2024 Golem Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::bindings::golem::api::host::PromiseId;
use serde::de::DeserializeOwned;
use serde::Serialize;

pub fn await_promise_json<T: DeserializeOwned>(
promise_id: &PromiseId,
) -> Result<T, serde_json::Error> {
let bytes = crate::bindings::golem::api::host::await_promise(promise_id);
serde_json::from_slice(&bytes)
}

pub fn complete_promise_json<T: Serialize>(
promise_id: &PromiseId,
value: T,
) -> Result<bool, serde_json::Error> {
let bytes = serde_json::to_vec(&value)?;
Ok(crate::bindings::golem::api::host::complete_promise(
promise_id, &bytes,
))
}
6 changes: 6 additions & 0 deletions golem-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
#[rustfmt::skip]
pub mod bindings;

#[cfg(feature = "json")]
mod json;

#[cfg(feature = "json")]
pub use json::*;

#[cfg(feature = "uuid")]
mod uuid;

Expand Down

0 comments on commit f11cb54

Please sign in to comment.