Skip to content

Commit

Permalink
feat: Allows serialization and deserialization to json
Browse files Browse the repository at this point in the history
  • Loading branch information
andyquinterom committed May 24, 2024
1 parent 58c8d8c commit 051a6b5
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 24 deletions.
48 changes: 38 additions & 10 deletions LICENSE.note
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ the licenses are the same as listed here.

===============================

Name: either
Files: vendor/either/*
Authors: bluss
License: MIT OR Apache-2.0

------------------------------

Name: extendr-api
Files: vendor/extendr-api/*
Authors: andy-thomason, Thomas Down, Mossa Merhi Reimert, Claus O. Wilke, Hiroaki Yutani, Ilia A. Kosenkov, Michael Milton
Expand All @@ -36,9 +29,9 @@ License: MIT

------------------------------

Name: itertools
Files: vendor/itertools/*
Authors: bluss
Name: itoa
Files: vendor/itoa/*
Authors: David Tolnay
License: MIT OR Apache-2.0

------------------------------
Expand All @@ -57,6 +50,13 @@ License: MIT OR Apache-2.0

------------------------------

Name: orbweaver
Files: vendor/orbweaver/*
Authors: ixpantia, Andrés F. Quintero
License: MIT

------------------------------

Name: paste
Files: vendor/paste/*
Authors: David Tolnay
Expand All @@ -78,6 +78,34 @@ License: MIT OR Apache-2.0

------------------------------

Name: ryu
Files: vendor/ryu/*
Authors: David Tolnay
License: Apache-2.0 OR BSL-1.0

------------------------------

Name: serde_derive
Files: vendor/serde_derive/*
Authors: Erick Tryzelaar, David Tolnay
License: MIT OR Apache-2.0

------------------------------

Name: serde_json
Files: vendor/serde_json/*
Authors: Erick Tryzelaar, David Tolnay
License: MIT OR Apache-2.0

------------------------------

Name: serde
Files: vendor/serde/*
Authors: Erick Tryzelaar, David Tolnay
License: MIT OR Apache-2.0

------------------------------

Name: syn
Files: vendor/syn/*
Authors: David Tolnay
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ S3method(get_nodes,DirectedAcyclicGraph)
S3method(get_nodes,DirectedGraph)
S3method(get_parents,DirectedAcyclicGraph)
S3method(get_parents,DirectedGraph)
S3method(graph_to_json,DirectedAcyclicGraph)
S3method(graph_to_json,DirectedGraph)
S3method(has_children,DirectedAcyclicGraph)
S3method(has_children,DirectedGraph)
S3method(has_parents,DirectedAcyclicGraph)
Expand Down Expand Up @@ -65,6 +67,8 @@ export(get_node)
export(get_node_data)
export(get_nodes)
export(get_parents)
export(graph_from_json)
export(graph_to_json)
export(has_children)
export(has_parents)
export(into_dag)
Expand Down
4 changes: 2 additions & 2 deletions R/add_node.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ add_node <- function(graph, node_id, data = NULL) {
}

#' @export
add_node.DirectedGraph <- function(graph, node_id, data) {
add_node.DirectedGraph <- function(graph, node_id, data = NULL) {
graph$add_node(node_id, data)
return(graph)
}

#' @export
add_node.DirectedAcyclicGraph <- function(graph, node_id, data) {
add_node.DirectedAcyclicGraph <- function(graph, node_id, data = NULL) {
rlang::abort(err_unable_to_modify_dag)
}
8 changes: 8 additions & 0 deletions R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ rs_populate_edges_directed_graph <- function(graph, parent_iter, child_iter) .Ca

rs_populate_nodes_directed_graph <- function(graph, node_ids, data_iter) .Call(wrap__rs_populate_nodes_directed_graph, graph, node_ids, data_iter)

rs_directed_graph_from_json <- function(text) .Call(wrap__rs_directed_graph_from_json, text)

rs_directed_graph_to_json <- function(directed_graph, pretty) .Call(wrap__rs_directed_graph_to_json, directed_graph, pretty)

rs_dag_to_json <- function(dag, pretty) .Call(wrap__rs_dag_to_json, dag, pretty)

rs_dag_from_json <- function(text) .Call(wrap__rs_dag_from_json, text)

Node <- new.env(parent = emptyenv())

Node$get_data <- function() .Call(wrap__Node__get_data, self)
Expand Down
34 changes: 34 additions & 0 deletions R/json.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#' @title Serialize the graph into a JSON string
#' @param graph A graph object
#' @param pretty If the JSON string should be prettified
#' @return A JSON string
#' @export
graph_to_json <- function(graph, pretty = FALSE) {
UseMethod("graph_to_json")
}

#' @export
graph_to_json.DirectedGraph <- function(graph, pretty = FALSE) {
rs_directed_graph_to_json(graph, pretty)
}

#' @export
graph_to_json.DirectedAcyclicGraph <- function(graph, pretty = FALSE) {
rs_dag_to_json(graph, pretty)
}

#' @title Get a graph from a JSON string
#' @param text The JSON string to be deserialized
#' @param type The type of graph the JSON represents
#' @return A graph object
#' @export
graph_from_json <- function(text, type = c("directed", "dag")) {
if (!type %in% c("directed", "dag")) {
rlang::abort("Invalid argument `type`")
}
switch(
type[1],
"directed" = rs_directed_graph_from_json(text),
"dag" = rs_dag_from_json(text)
)
}
36 changes: 26 additions & 10 deletions inst/AUTHORS
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
The authors of the dependency Rust crates:

either (version 1.9.0):
bluss

extendr-api (version 0.6.0):
andy-thomason
Thomas Down
Expand All @@ -20,8 +17,8 @@ extendr-macros (version 0.6.0):
Hiroaki Yutani
Ilia A. Kosenkov

itertools (version 0.11.0):
bluss
itoa (version 1.0.11):
David Tolnay

libR-sys (version 0.6.0):
andy-thomason
Expand All @@ -31,20 +28,39 @@ libR-sys (version 0.6.0):
Ilia A. Kosenkov
Hiroaki Yutani

once_cell (version 1.18.0):
once_cell (version 1.19.0):
Aleksey Kladov

paste (version 1.0.14):
orbweaver (version 0.2.1):
ixpantia
Andrés F. Quintero

paste (version 1.0.15):
David Tolnay

proc-macro2 (version 1.0.69):
proc-macro2 (version 1.0.83):
David Tolnay
Alex Crichton

quote (version 1.0.33):
quote (version 1.0.36):
David Tolnay

ryu (version 1.0.18):
David Tolnay

serde_derive (version 1.0.202):
Erick Tryzelaar
David Tolnay

serde_json (version 1.0.117):
Erick Tryzelaar
David Tolnay

serde (version 1.0.202):
Erick Tryzelaar
David Tolnay

syn (version 2.0.39):
syn (version 2.0.66):
David Tolnay

unicode-ident (version 1.0.12):
Expand Down
19 changes: 19 additions & 0 deletions man/graph_from_json.Rd

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

19 changes: 19 additions & 0 deletions man/graph_to_json.Rd

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

25 changes: 25 additions & 0 deletions src/rust/Cargo.lock

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

3 changes: 2 additions & 1 deletion src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ crate-type = [ 'staticlib' ]
name = 'orbweaver'

[dependencies]
extendr-api = '*'
extendr-api = { version = "0.6", features = ["serde"] }
orbweaver = { version = "0.2.1" }
serde_json = "1.0.117"
4 changes: 3 additions & 1 deletion src/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use extendr_api::prelude::*;
use orbweaver::prelude as ow;

pub mod from_dataframe;
pub mod to_json;

struct Node(ow::Node<Robj>);

Expand Down Expand Up @@ -30,7 +31,7 @@ impl Node {
pub struct DirectedGraph(ow::DirectedGraph<Robj>);
pub struct DirectedAcyclicGraph(ow::DirectedAcyclicGraph<Robj>);

fn to_r_error(err: impl std::error::Error) -> String {
pub fn to_r_error(err: impl std::error::Error) -> String {
err.to_string()
}

Expand Down Expand Up @@ -291,4 +292,5 @@ extendr_module! {
impl DirectedGraph;
impl DirectedAcyclicGraph;
use from_dataframe;
use to_json;
}
42 changes: 42 additions & 0 deletions src/rust/src/to_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use super::{to_r_error, DirectedAcyclicGraph, DirectedGraph};
use extendr_api::prelude::*;
use orbweaver::prelude as ow;

#[extendr]
fn rs_directed_graph_to_json(directed_graph: &DirectedGraph, pretty: bool) -> Result<String> {
if pretty {
Ok(serde_json::to_string_pretty(&directed_graph.0).map_err(to_r_error)?)
} else {
Ok(serde_json::to_string(&directed_graph.0).map_err(to_r_error)?)
}
}

#[extendr]
fn rs_directed_graph_from_json(text: &str) -> Result<DirectedGraph> {
let dg: ow::DirectedGraph<Robj> = serde_json::from_str(text).map_err(|e| e.to_string())?;
Ok(DirectedGraph(dg))
}

#[extendr]
fn rs_dag_to_json(dag: &DirectedAcyclicGraph, pretty: bool) -> Result<String> {
if pretty {
Ok(serde_json::to_string_pretty(&dag.0).map_err(to_r_error)?)
} else {
Ok(serde_json::to_string(&dag.0).map_err(to_r_error)?)
}
}

#[extendr]
fn rs_dag_from_json(text: &str) -> Result<DirectedAcyclicGraph> {
let dag: ow::DirectedAcyclicGraph<Robj> =
serde_json::from_str(text).map_err(|e| e.to_string())?;
Ok(DirectedAcyclicGraph(dag))
}

extendr_module! {
mod to_json;
fn rs_directed_graph_from_json;
fn rs_directed_graph_to_json;
fn rs_dag_to_json;
fn rs_dag_from_json;
}
Binary file modified src/rust/vendor.tar.xz
Binary file not shown.

0 comments on commit 051a6b5

Please sign in to comment.