Skip to content

Commit d48d7fe

Browse files
committed
Refine journal interface limits, naming, and error logging
1 parent e68495e commit d48d7fe

4 files changed

Lines changed: 58 additions & 35 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "journal-sdk"
3-
version = "1.1.1"
3+
version = "1.1.2"
44
edition = "2024"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ Here are other auxiliary actions that may be useful in the course of development
5454

5555
# Use
5656

57-
The `interface` endpoint exposes an evaluator for executing arbitrary code in a Lisp dialect.
57+
The `interface` endpoint exposes an evaluator for executing arbitrary code in a Scheme dialect.
5858
Once the service deploys, all interaction with the Journal SDK should take place through this interface.
5959
The evaluator itself is stateless; all variables and computations are cleared between each invocation of the endpoint.
6060
However, the SDK provides a controlled ability to read and write persistent data to the backend database.
6161
By leveraging the functionality and workflow specified below, it is possible to create arbitrarily complex stateful interfaces that benefit from the same core cryptographic verifiability afforded by the Journal.
6262

63-
## Lisp Evaluation
63+
## Scheme Evaluation
6464

65-
The Lisp dialect used for Synchronic Web code is a lightly modified version of s7 Scheme.
65+
The Scheme dialect used for Synchronic Web code is a lightly modified version of s7 Scheme.
6666
All source code and documentation is available in the [./external/s7](./external/s7) folder.
6767
Other basic modifcations include:
6868

@@ -91,7 +91,7 @@ There is only one structural constraint on the form of the record: for a record
9191
`(lambda (*sync-state* query) (cons ... *sync-state*))`
9292

9393
The `*sync-state*` parameter is the root node of the record while the `query` parameter is the expression provided through the `/interface` endpoint.
94-
The function returns a Lisp pair where the first item is the response to the `/inferface` call and the second item is the new root node.
94+
The function returns a Scheme pair where the first item is the response to the `/inferface` call and the second item is the new root node.
9595
For example, the default function is the following:
9696

9797
`(lambda (*sync-state* query) (cons (eval query) *sync-state*))`

src/lib.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::extensions::system::{primitive_s7_system_time_unix, primitive_s7_syst
99
use crate::persistor::{MemoryPersistor, PERSISTOR, Persistor, PersistorAccessError};
1010
pub use crate::persistor::{SIZE, Word};
1111
use libc;
12-
use log::{debug, info};
12+
use log::{debug, info, warn};
1313
use once_cell::sync::Lazy;
1414
use serde_json::Value;
1515
use std::collections::HashMap;
@@ -78,6 +78,25 @@ fn escape_scheme_string(value: &str) -> String {
7878
value.replace('\\', "\\\\").replace('\"', "\\\"")
7979
}
8080

81+
fn truncate_for_log(value: &str, limit: usize) -> String {
82+
let truncated: String = value.chars().take(limit).collect();
83+
if value.chars().count() > limit {
84+
format!("{truncated} ...")
85+
} else {
86+
truncated
87+
}
88+
}
89+
90+
fn warn_on_error_result(query: &str, output: &str) {
91+
if output.starts_with("(error ") {
92+
warn!(
93+
"Evaluation returned error form. Query: {} Result: {}",
94+
truncate_for_log(query, 256),
95+
truncate_for_log(output, 256),
96+
);
97+
}
98+
}
99+
81100
/// Journals are the primary way that application developers
82101
/// interact with the synchronic web.
83102
///
@@ -88,7 +107,7 @@ fn escape_scheme_string(value: &str) -> String {
88107
///
89108
/// * __Persistence__: managing bytes on the global hash graph
90109
///
91-
/// * __Evaluation__: executing code in the global Lisp environment
110+
/// * __Evaluation__: executing code in the global Scheme environment
92111
///
93112
/// __Records__ are the primary way that developers interface with
94113
/// Journals. A Record is a mapping between a constant identifier and
@@ -125,7 +144,7 @@ impl Journal {
125144
}
126145
}
127146

128-
/// Evaluate a Lisp expression within a Record
147+
/// Evaluate a Scheme expression within a Record
129148
///
130149
/// # Examples
131150
/// ```
@@ -173,17 +192,17 @@ impl Journal {
173192
}
174193
}
175194

176-
/// Convert a Lisp expression into its JSON representation without evaluation.
195+
/// Convert a Scheme expression into its JSON representation without evaluation.
177196
///
178197
/// # Examples
179198
/// ```
180199
/// use journal_sdk::JOURNAL;
181200
/// use serde_json::json;
182201
///
183-
/// let output = JOURNAL.lisp_to_json("(+ 1 2)");
202+
/// let output = JOURNAL.scheme_to_json("(+ 1 2)");
184203
/// assert_eq!(output, json!(["+", 1, 2]));
185204
/// ```
186-
pub fn lisp_to_json(&self, query: &str) -> Value {
205+
pub fn scheme_to_json(&self, query: &str) -> Value {
187206
match lisp2json(query) {
188207
Ok(json_result) => json_result,
189208
Err(_) => {
@@ -197,17 +216,17 @@ impl Journal {
197216
}
198217
}
199218

200-
/// Convert a JSON expression into its Lisp representation without evaluation.
219+
/// Convert a JSON expression into its Scheme representation without evaluation.
201220
///
202221
/// # Examples
203222
/// ```
204223
/// use journal_sdk::JOURNAL;
205224
/// use serde_json::json;
206225
///
207-
/// let output = JOURNAL.json_to_lisp(json!(["+", 1, 2]));
226+
/// let output = JOURNAL.json_to_scheme(json!(["+", 1, 2]));
208227
/// assert_eq!(output, "(+ 1 2)");
209228
/// ```
210-
pub fn json_to_lisp(&self, query: Value) -> String {
229+
pub fn json_to_scheme(&self, query: Value) -> String {
211230
match json2lisp(&query) {
212231
Ok(scheme_result) => scheme_result,
213232
Err(_) => {
@@ -370,6 +389,7 @@ impl Journal {
370389

371390
match state_old == state_new {
372391
true => {
392+
warn_on_error_result(query, output.as_str());
373393
debug!(
374394
"Completed ({:?}) {} -> {}",
375395
start.elapsed(),
@@ -433,6 +453,7 @@ impl Journal {
433453
match iterate(&persistor, state_new) {
434454
Ok(_) => match PERSISTOR.root_set(record, state_old, state_new) {
435455
Ok(_) => {
456+
warn_on_error_result(query, output.as_str());
436457
debug!(
437458
"Completed ({:?}) {} -> {}",
438459
start.elapsed(),

src/main.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ const INDEX_HTML: &str = r#"<!DOCTYPE html>
1818
</head>
1919
<body style="padding: 0 20px; font-family: 'Consolas'">
2020
<ul>
21-
<li><a href="/interface">LISP Interface</a></li>
21+
<li><a href="/interface">Scheme Interface</a></li>
2222
<li><a href="/interface/json">JSON Interface</a></li>
23-
<li><a href="/interface/lisp-to-json">LISP to JSON</a></li>
24-
<li><a href="/interface/json-to-lisp">JSON to LISP</a></li>
23+
<li><a href="/interface/scheme-to-json">Scheme to JSON</a></li>
24+
<li><a href="/interface/json-to-scheme">JSON to Scheme</a></li>
2525
</ul>
2626
</body>
2727
</html>
@@ -78,7 +78,7 @@ async fn index() -> RawHtml<String> {
7878
async fn inform_lisp() -> RawHtml<String> {
7979
RawHtml(
8080
INTERFACE_HTML
81-
.replace("__TITLE__", "LISP Interface")
81+
.replace("__TITLE__", "Scheme Interface")
8282
.replace("__HEADERS__", ""),
8383
)
8484
}
@@ -88,18 +88,18 @@ async fn evaluate_lisp(query: &str) -> String {
8888
JOURNAL.evaluate(query)
8989
}
9090

91-
#[get("/interface/lisp-to-json", format = "text/html")]
92-
async fn inform_lisp_to_json() -> RawHtml<String> {
91+
#[get("/interface/scheme-to-json", format = "text/html")]
92+
async fn inform_scheme_to_json() -> RawHtml<String> {
9393
RawHtml(
9494
INTERFACE_HTML
95-
.replace("__TITLE__", "LISP to JSON")
95+
.replace("__TITLE__", "Scheme to JSON")
9696
.replace("__HEADERS__", ""),
9797
)
9898
}
9999

100-
#[post("/interface/lisp-to-json", data = "<query>", rank = 1)]
101-
async fn lisp_to_json(query: &str) -> Json<Value> {
102-
let result = JOURNAL.lisp_to_json(query);
100+
#[post("/interface/scheme-to-json", data = "<query>", rank = 1)]
101+
async fn scheme_to_json(query: &str) -> Json<Value> {
102+
let result = JOURNAL.scheme_to_json(query);
103103
Json(result)
104104
}
105105

@@ -121,17 +121,17 @@ async fn evaluate_json(query: Json<Value>) -> Json<Value> {
121121
Json(result)
122122
}
123123

124-
#[get("/interface/json-to-lisp", format = "text/html")]
125-
async fn inform_json_to_lisp() -> RawHtml<String> {
126-
RawHtml(INTERFACE_HTML.replace("__TITLE__", "JSON to LISP").replace(
124+
#[get("/interface/json-to-scheme", format = "text/html")]
125+
async fn inform_json_to_scheme() -> RawHtml<String> {
126+
RawHtml(INTERFACE_HTML.replace("__TITLE__", "JSON to Scheme").replace(
127127
"__HEADERS__",
128128
"headers: { 'Content-Type': 'application/json' },",
129129
))
130130
}
131131

132-
#[post("/interface/json-to-lisp", data = "<query>", format = "json", rank = 1)]
133-
async fn json_to_lisp(query: Json<Value>) -> RawText<String> {
134-
RawText(JOURNAL.json_to_lisp(query.into_inner()))
132+
#[post("/interface/json-to-scheme", data = "<query>", format = "json", rank = 1)]
133+
async fn json_to_scheme(query: Json<Value>) -> RawText<String> {
134+
RawText(JOURNAL.json_to_scheme(query.into_inner()))
135135
}
136136

137137
#[rocket::main]
@@ -154,7 +154,9 @@ async fn main() {
154154
let mut rocket_config = RocketConfig::default();
155155
rocket_config.port = config.port;
156156
rocket_config.address = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
157-
rocket_config.limits = Limits::new().limit("string", 1_i32.mebibytes());
157+
rocket_config.limits = Limits::new()
158+
.limit("string", 64_i32.mebibytes())
159+
.limit("json", 64_i32.mebibytes());
158160

159161
if config.step != "" {
160162
tokio::spawn(async move {
@@ -195,12 +197,12 @@ async fn main() {
195197
index,
196198
inform_lisp,
197199
evaluate_lisp,
198-
inform_lisp_to_json,
199-
lisp_to_json,
200+
inform_scheme_to_json,
201+
scheme_to_json,
200202
inform_json,
201203
evaluate_json,
202-
inform_json_to_lisp,
203-
json_to_lisp
204+
inform_json_to_scheme,
205+
json_to_scheme
204206
],
205207
)
206208
.configure(rocket_config)

0 commit comments

Comments
 (0)