Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Adds conversions from json string to generic Results #1464

Merged
1 change: 1 addition & 0 deletions CHANGELOG-UNRELEASED.md
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- **Breaking change** - renames `emit_trace_signals` to `signals.trace` in conductor config [#1431](https://github.com/holochain/holochain-rust/pull/1431)
- "Consistency" signals added, which aid determinism in end-to-end tests, configurable through `signals.consistency` conductor config [#1431](https://github.com/holochain/holochain-rust/pull/1431)
- Adds TryInto implementation from JsonString to generic result types. This makes bridge calls much easier to implement safely [#1464](https://github.com/holochain/holochain-rust/pull/1464)

### Deprecated

Expand Down
82 changes: 77 additions & 5 deletions core_types/src/json.rs
Expand Up @@ -5,7 +5,7 @@ use crate::error::{HcResult, HolochainError};
use serde::{de::DeserializeOwned, Serialize};
use serde_json;
use std::{
convert::TryFrom,
convert::{TryFrom, TryInto},
fmt::{Debug, Display, Formatter, Result as FmtResult},
};

Expand Down Expand Up @@ -147,6 +147,8 @@ pub trait JsonError {}

impl JsonError for HolochainError {}

// conversions from result types

fn result_to_json_string<T: Into<JsonString>, E: Into<JsonString>>(
result: Result<T, E>,
) -> JsonString {
Expand All @@ -163,19 +165,29 @@ fn result_to_json_string<T: Into<JsonString>, E: Into<JsonString>>(
))
}

impl<T: Into<JsonString>, E: Into<JsonString> + JsonError> From<Result<T, E>> for JsonString {
impl<T, E> From<Result<T, E>> for JsonString
where
T: Into<JsonString>,
E: Into<JsonString>,
{
fn from(result: Result<T, E>) -> JsonString {
result_to_json_string(result)
}
}

impl<T: Into<JsonString>> From<Result<T, String>> for JsonString {
impl<T> From<Result<T, String>> for JsonString
where
T: Into<JsonString>,
{
fn from(result: Result<T, String>) -> JsonString {
result_to_json_string(result.map_err(|e| RawString::from(e)))
}
}

impl<E: Into<JsonString>> From<Result<String, E>> for JsonString {
impl<E> From<Result<String, E>> for JsonString
where
E: Into<JsonString>,
{
fn from(result: Result<String, E>) -> JsonString {
result_to_json_string(result.map(|v| RawString::from(v)))
}
Expand All @@ -191,6 +203,46 @@ impl From<Result<String, String>> for JsonString {
}
}

// conversions to result types

impl<T, E> TryInto<Result<T, E>> for JsonString
where
T: Into<JsonString> + DeserializeOwned,
E: Into<JsonString> + DeserializeOwned,
{
type Error = HolochainError;
fn try_into(self) -> Result<Result<T, E>, Self::Error> {
default_try_from_json(self)
}
}

impl<T> TryInto<Result<T, String>> for JsonString
where
T: Into<JsonString> + DeserializeOwned,
{
type Error = HolochainError;
fn try_into(self) -> Result<Result<T, String>, Self::Error> {
default_try_from_json(self)
}
}

impl<E> TryInto<Result<String, E>> for JsonString
where
E: Into<JsonString> + DeserializeOwned,
{
type Error = HolochainError;
fn try_into(self) -> Result<Result<String, E>, Self::Error> {
default_try_from_json(self)
}
}

impl TryInto<Result<String, String>> for JsonString {
type Error = HolochainError;
fn try_into(self) -> Result<Result<String, String>, Self::Error> {
default_try_from_json(self)
}
}

pub type JsonResult = Result<JsonString, HolochainError>;

impl From<()> for JsonString {
Expand Down Expand Up @@ -321,7 +373,7 @@ pub mod tests {
json::{JsonString, RawString},
};
use serde_json;
use std::convert::TryFrom;
use std::convert::{TryFrom, TryInto};

#[derive(Serialize, Deserialize, Debug, DefaultJson, PartialEq, Clone)]
struct DeriveTest {
Expand Down Expand Up @@ -449,4 +501,24 @@ pub mod tests {
String::from(JsonString::from(RawString::from(1))),
);
}

#[test]
fn result_from_json_string() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@philipbeadle this should be very similar to how you want to use it in P&P

let j = JsonString::from_json(r#"{"Ok":"raw-string-content"}"#);
let r: Result<RawString, HolochainError> = j
.try_into()
.expect("Could not convert json string to result type");

assert_eq!(r.unwrap(), RawString::from("raw-string-content"),);
}

#[test]
fn result_from_json_string_with_strings() {
let j = JsonString::from_json(r#"{"Ok":"string-content"}"#);
let r: Result<String, String> = j
.try_into()
.expect("Could not convert json string to result type");

assert_eq!(r.unwrap(), String::from("string-content"),);
}
}