pub mod abi;
pub mod columns;
pub mod connector;
pub mod json;
pub mod series;
pub use json::Json;
use std::fmt;
pub const SDK_VERSION: &str = env!("CARGO_PKG_VERSION");
pub mod prelude {
pub use crate::connector::{Context, Piped, Request, Response, RulesetRun};
pub use crate::series;
pub use crate::{Error, Frame, Host, Json, Load, Result, View};
}
#[derive(Debug, Clone, PartialEq)]
pub struct Error(pub String);
impl Error {
pub fn new(msg: impl Into<String>) -> Self {
Error(msg.into())
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for Error {}
impl From<String> for Error {
fn from(s: String) -> Self {
Error(s)
}
}
impl From<&str> for Error {
fn from(s: &str) -> Self {
Error(s.to_string())
}
}
pub type Result<T> = std::result::Result<T, Error>;
pub struct Host {
raw: *const abi::WnHost,
}
impl Host {
pub unsafe fn from_raw(raw: *const abi::WnHost) -> Result<Host> {
if raw.is_null() {
return Err("no host".into());
}
let v = (*raw).abi_version;
if v != abi::ABI_VERSION {
return Err(Error(format!("host ABI {v}, plugin ABI {}", abi::ABI_VERSION)));
}
Ok(Host { raw })
}
pub fn call(&self, method: &str, request: &Json) -> Result<Json> {
let req = request.to_string();
let mut out = abi::WnBuf::EMPTY;
let (code, text) = unsafe {
let h = &*self.raw;
let code = (h.call)(h.ctx, abi::WnStr::new(method), abi::WnStr::new(&req), &mut out);
let text =
if out.ptr.is_null() { String::new() } else { String::from_utf8_lossy(std::slice::from_raw_parts(out.ptr, out.len)).into_owned() };
(h.free)(h.ctx, out);
(code, text)
};
if code != abi::CALL_OK {
return Err(Error(text));
}
if text.is_empty() {
return Ok(Json::Null);
}
Json::parse(&text).map_err(|e| Error(format!("bad reply from {method}: {e}")))
}
pub fn methods(&self) -> Result<Vec<String>> {
Ok(self.call("methods", &Json::Null)?.as_array().unwrap_or(&[]).iter().filter_map(|m| m.as_str().map(str::to_string)).collect())
}
pub fn input(&self) -> Result<Frame> {
Frame::from_json(&self.call("input", &Json::Null)?)
}
pub fn input_json(&self) -> Result<Json> {
self.call("input", &Json::Null)
}
pub fn call_code(&self, alias: &str, input: Option<&Json>) -> Result<Frame> {
let mut req = Json::obj().with("alias", alias);
if let Some(i) = input {
req = req.with("input", i.clone());
}
Frame::from_json(&self.call("call", &req)?)
}
pub fn call_space(&self, name: &str, input: Option<&Json>) -> Result<Frame> {
let mut req = Json::obj().with("name", name);
if let Some(i) = input {
req = req.with("input", i.clone());
}
Frame::from_json(&self.call("call_space", &req)?)
}
pub fn inputs(&self) -> Result<Vec<String>> {
Ok(self.call("inputs", &Json::Null)?.as_array().unwrap_or(&[]).iter().filter_map(|m| m.as_str().map(str::to_string)).collect())
}
pub fn input_named(&self, name: &str) -> Result<Frame> {
Frame::from_json(&self.call("input_named", &Json::obj().with("name", name))?)
}
pub fn options(&self) -> Vec<(String, String)> {
let o = self.call("options", &Json::Null).unwrap_or(Json::Null);
o.as_object().unwrap_or(&[]).iter().map(|(k, v)| (k.clone(), v.as_str().map(str::to_string).unwrap_or_else(|| v.to_string()))).collect()
}
pub fn option(&self, name: &str) -> Option<String> {
self.options().into_iter().find(|(k, _)| k == name).map(|(_, v)| v)
}
pub fn input_request(&self) -> Result<Json> {
self.call("input_request", &Json::Null)
}
pub fn models(&self) -> Result<Json> {
self.call("models", &Json::Null)
}
pub fn load(&self, q: &Load) -> Result<Frame> {
Frame::from_json(&self.call("load", &q.to_json())?)
}
pub fn update(&self, model: &str, id: &str, values: Json) -> Result<()> {
self.call("update", &Json::obj().with("model", model).with("id", id).with("values", values)).map(|_| ())
}
pub fn update_where(&self, model: &str, conditions: &[&str], values: Json) -> Result<usize> {
let w: Vec<Json> = conditions.iter().map(|c| Json::from(*c)).collect();
let r = self.call("update_where", &Json::obj().with("model", model).with("where", Json::Arr(w)).with("values", values))?;
Ok(r.get("count").and_then(Json::as_f64).unwrap_or(0.0) as usize)
}
pub fn update_each(&self, model: &str, updates: Vec<(String, Json)>) -> Result<usize> {
let u: Vec<Json> = updates.into_iter().map(|(id, v)| Json::obj().with("id", id).with("values", v)).collect();
let r = self.call("update_each", &Json::obj().with("model", model).with("updates", Json::Arr(u)))?;
Ok(r.get("count").and_then(Json::as_f64).unwrap_or(0.0) as usize)
}
pub fn create(&self, model: &str, values: Json) -> Result<String> {
let r = self.call("create", &Json::obj().with("model", model).with("values", values))?;
Ok(r.get("id").and_then(Json::as_str).unwrap_or_default().to_string())
}
pub fn create_many(&self, model: &str, records: Vec<Json>) -> Result<Vec<String>> {
let r = self.call("create_many", &Json::obj().with("model", model).with("records", Json::Arr(records)))?;
Ok(r.get("ids").and_then(Json::as_array).unwrap_or(&[]).iter().filter_map(|i| i.as_str().map(str::to_string)).collect())
}
pub fn create_model(&self, name: &str, fields: &str, about: &str) -> Result<bool> {
let r = self.call("model_create", &Json::obj().with("name", name).with("fields", fields).with("about", about))?;
Ok(r.get("created").and_then(Json::as_bool).unwrap_or(false))
}
pub fn log(&self, text: impl Into<String>) {
let _ = self.call("log", &Json::obj().with("text", text.into()));
}
}
#[derive(Debug, Clone, Default)]
pub struct Load {
pub model: String,
pub conditions: Vec<String>,
pub sort: Vec<String>,
pub group: Vec<String>,
pub text: String,
pub limit: Option<usize>,
}
impl Load {
pub fn model(m: &str) -> Self {
Load { model: m.into(), ..Default::default() }
}
pub fn filter(mut self, c: &str) -> Self {
self.conditions.push(c.into());
self
}
pub fn sort(mut self, f: &str) -> Self {
self.sort.push(f.into());
self
}
pub fn group(mut self, f: &str) -> Self {
self.group.push(f.into());
self
}
pub fn text(mut self, t: &str) -> Self {
self.text = t.into();
self
}
pub fn limit(mut self, n: usize) -> Self {
self.limit = Some(n);
self
}
pub fn to_json(&self) -> Json {
let s = |v: &[String]| Json::Arr(v.iter().map(|x| Json::from(x.as_str())).collect());
Json::obj()
.with("model", self.model.as_str())
.with("where", s(&self.conditions))
.with("sort", s(&self.sort))
.with("group", s(&self.group))
.with("text", self.text.as_str())
.with("limit", self.limit)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct FieldInfo {
pub name: String,
pub ty: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Group {
pub key: Vec<(String, Json)>,
pub start: usize,
pub len: usize,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Frame {
pub model: String,
pub fields: Vec<FieldInfo>,
pub ids: Vec<String>,
pub columns: Vec<(String, Vec<Json>)>,
pub groups: Vec<Group>,
}
impl Frame {
pub fn from_json(j: &Json) -> Result<Frame> {
let model = j.get("model").and_then(Json::as_str).unwrap_or_default().to_string();
let fields = j
.get("fields")
.and_then(Json::as_array)
.unwrap_or(&[])
.iter()
.map(|f| FieldInfo {
name: f.get("name").and_then(Json::as_str).unwrap_or_default().into(),
ty: f.get("type").and_then(Json::as_str).unwrap_or_default().into(),
})
.collect();
let ids: Vec<String> = j.get("ids").and_then(Json::as_array).unwrap_or(&[]).iter().map(Json::text).collect();
let columns: Vec<(String, Vec<Json>)> = j
.get("columns")
.and_then(Json::as_object)
.unwrap_or(&[])
.iter()
.map(|(k, v)| (k.clone(), v.as_array().unwrap_or(&[]).to_vec()))
.collect();
if let Some((name, c)) = columns.iter().find(|(_, c)| c.len() != ids.len()) {
return Err(Error(format!("column {name} has {} values for {} records", c.len(), ids.len())));
}
let groups = j
.get("groups")
.and_then(Json::as_array)
.unwrap_or(&[])
.iter()
.map(|g| Group {
key: g.get("key").and_then(Json::as_object).unwrap_or(&[]).to_vec(),
start: g.get("start").and_then(Json::as_f64).unwrap_or(0.0) as usize,
len: g.get("len").and_then(Json::as_f64).unwrap_or(0.0) as usize,
})
.collect();
Ok(Frame { model, fields, ids, columns, groups })
}
pub fn len(&self) -> usize {
self.ids.len()
}
pub fn is_empty(&self) -> bool {
self.ids.is_empty()
}
pub fn has(&self, name: &str) -> bool {
name == "id" || self.columns.iter().any(|(n, _)| n == name)
}
pub fn column(&self, name: &str) -> Result<&[Json]> {
self.columns.iter().find(|(n, _)| n == name).map(|(_, c)| c.as_slice()).ok_or_else(|| {
Error(format!(
"{} has no field `{name}` (fields: {})",
self.model,
self.columns.iter().map(|(n, _)| n.as_str()).collect::<Vec<_>>().join(", ")
))
})
}
pub fn f64(&self, name: &str) -> Result<Vec<f64>> {
if name == "row" && !self.has("row") {
return Ok(series::row(self.len()));
}
Ok(self.column(name)?.iter().map(|v| v.as_f64().unwrap_or(f64::NAN)).collect())
}
pub fn text(&self, name: &str) -> Result<Vec<String>> {
if name == "id" {
return Ok(self.ids.clone());
}
Ok(self.column(name)?.iter().map(Json::text).collect())
}
pub fn get(&self, row: usize, name: &str) -> Json {
self.column(name).ok().and_then(|c| c.get(row).cloned()).unwrap_or_default()
}
pub fn slice(&self, start: usize, len: usize) -> Frame {
let end = (start + len).min(self.len());
let start = start.min(end);
Frame {
model: self.model.clone(),
fields: self.fields.clone(),
ids: self.ids[start..end].to_vec(),
columns: self.columns.iter().map(|(n, c)| (n.clone(), c[start..end].to_vec())).collect(),
groups: vec![],
}
}
pub fn take(&self, rows: &[usize]) -> Frame {
let rows: Vec<usize> = rows.iter().copied().filter(|r| *r < self.len()).collect();
Frame {
model: self.model.clone(),
fields: self.fields.clone(),
ids: rows.iter().map(|r| self.ids[*r].clone()).collect(),
columns: self.columns.iter().map(|(n, c)| (n.clone(), rows.iter().map(|r| c[*r].clone()).collect())).collect(),
groups: vec![],
}
}
pub fn groups(&self) -> Vec<Group> {
if self.groups.is_empty() {
vec![Group { key: vec![], start: 0, len: self.len() }]
} else {
self.groups.clone()
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum View {
Table { title: String, columns: Vec<String>, rows: Vec<Vec<Json>> },
Chart { title: String, x_name: String, x: Vec<f64>, x_time: bool, series: Vec<(String, Vec<f64>)> },
Text { title: String, text: String },
Tiles { title: String, below: bool, tiles: Vec<View> },
}
impl View {
pub fn table(title: &str, columns: &[&str]) -> View {
View::Table { title: title.into(), columns: columns.iter().map(|c| c.to_string()).collect(), rows: vec![] }
}
pub fn row(mut self, values: Vec<Json>) -> View {
if let View::Table { rows, .. } = &mut self {
rows.push(values);
}
self
}
pub fn chart(title: &str) -> View {
View::Chart { title: title.into(), x_name: "row".into(), x: vec![], x_time: false, series: vec![] }
}
pub fn x(mut self, name: &str, values: Vec<f64>, time: bool) -> View {
if let View::Chart { x_name, x, x_time, .. } = &mut self {
*x_name = name.into();
*x = values;
*x_time = time;
}
self
}
pub fn line(mut self, name: &str, values: Vec<f64>) -> View {
if let View::Chart { series, .. } = &mut self {
series.push((name.into(), values));
}
self
}
pub fn text(title: &str, text: &str) -> View {
View::Text { title: title.into(), text: text.into() }
}
pub fn tiles(title: &str, below: bool, tiles: Vec<View>) -> View {
View::Tiles { title: title.into(), below, tiles }
}
pub fn to_json(&self) -> Json {
match self {
View::Table { title, columns, rows } => Json::obj()
.with("kind", "table")
.with("title", title.as_str())
.with("columns", Json::Arr(columns.iter().map(|c| Json::from(c.as_str())).collect()))
.with("rows", Json::Arr(rows.iter().map(|r| Json::Arr(r.clone())).collect())),
View::Chart { title, x_name, x, x_time, series } => Json::obj()
.with("kind", "chart")
.with("title", title.as_str())
.with("x", Json::obj().with("name", x_name.as_str()).with("time", *x_time).with("values", nums(x)))
.with("series", Json::Arr(series.iter().map(|(n, v)| Json::obj().with("name", n.as_str()).with("values", nums(v))).collect())),
View::Text { title, text } => Json::obj().with("kind", "text").with("title", title.as_str()).with("text", text.as_str()),
View::Tiles { title, below, tiles } => Json::obj()
.with("kind", "tiles")
.with("title", title.as_str())
.with("dir", if *below { "below" } else { "right" })
.with("tiles", Json::Arr(tiles.iter().map(View::to_json).collect())),
}
}
}
fn nums(v: &[f64]) -> Json {
Json::Arr(v.iter().map(|x| if x.is_finite() { Json::Num(*x) } else { Json::Null }).collect())
}
fn panic_text(p: &(dyn std::any::Any + Send)) -> String {
p.downcast_ref::<&str>().map(|s| s.to_string()).or_else(|| p.downcast_ref::<String>().cloned()).unwrap_or_else(|| "panic".into())
}
#[doc(hidden)]
pub unsafe fn __run(raw: *const abi::WnHost, f: fn(&Host) -> Result<View>) -> i32 {
let host = match Host::from_raw(raw) {
Ok(h) => h,
Err(_) => return abi::RUN_ERROR,
};
std::panic::set_hook(Box::new(|_| {}));
let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(&host)));
let _ = std::panic::take_hook();
match out {
Ok(Ok(view)) => match host.call("result", &view.to_json()) {
Ok(_) => abi::RUN_OK,
Err(e) => {
let _ = host.call("error", &Json::obj().with("message", format!("the host refused the result: {e}")));
abi::RUN_ERROR
}
},
Ok(Err(e)) => {
let _ = host.call("error", &Json::obj().with("message", e.0));
abi::RUN_ERROR
}
Err(p) => {
let _ = host.call("error", &Json::obj().with("message", format!("panicked: {}", panic_text(&*p))));
abi::RUN_PANIC
}
}
}
#[macro_export]
macro_rules! plugin {
($run:path) => {
$crate::plugin!($run, "rust");
};
($run:path, kind = $kind:literal) => {
$crate::plugin!(@export $run, concat!("{\"sdk\":\"0.1.0\",\"lang\":\"rust\",\"kind\":\"", $kind, "\"}"));
};
($run:path, $lang:literal, kind = $kind:literal) => {
$crate::plugin!(@export $run, concat!("{\"sdk\":\"0.1.0\",\"lang\":\"", $lang, "\",\"kind\":\"", $kind, "\"}"));
};
($run:path, $lang:literal) => {
$crate::plugin!(@export $run, concat!("{\"sdk\":\"0.1.0\",\"lang\":\"", $lang, "\"}"));
};
(@export $run:path, $info:expr) => {
#[no_mangle]
pub extern "C" fn weirdnet_plugin_abi() -> u32 {
$crate::abi::ABI_VERSION
}
#[no_mangle]
pub extern "C" fn weirdnet_plugin_info() -> $crate::abi::WnStr {
const INFO: &str = $info;
$crate::abi::WnStr::new(INFO)
}
#[no_mangle]
pub unsafe extern "C" fn weirdnet_plugin_run(host: *const $crate::abi::WnHost) -> i32 {
$crate::__run(host, $run)
}
};
}