#![recursion_limit = "1024"]
#![feature(const_type_name)]
#![feature(iter_intersperse)]
#![feature(stmt_expr_attributes)]
#![feature(try_blocks)]
#![allow(async_fn_in_trait)]
#![warn(
clippy::all,
clippy::panic_in_result_fn,
clippy::fallible_impl_from,
clippy::unneeded_field_pattern
)]
#![deny(clippy::await_holding_refcell_ref)]
#[cfg(feature = "llm-agent")]
mod agent;
pub mod components;
pub mod config;
pub mod custom_elements;
mod custom_events;
mod derived;
pub mod exprtk;
mod js;
mod presentation;
mod root;
#[doc(hidden)]
pub mod queries;
mod renderer;
mod session;
#[doc(hidden)]
pub mod tasks;
pub mod ui;
pub mod utils;
mod workspace;
#[macro_use]
extern crate macro_rules_attribute;
extern crate alloc;
use std::cell::RefCell;
use perspective_js::utils::*;
use wasm_bindgen::prelude::*;
use crate::custom_elements::debug_plugin::PerspectiveDebugPluginElement;
use crate::custom_elements::viewer::PerspectiveViewerElement;
use crate::utils::define_web_component;
#[wasm_bindgen(typescript_custom_section)]
const TS_APPEND_CONTENT: &'static str = r#"
import type {
ColumnType,
ColumnWindow,
DeleteOptions,
Features,
JoinOptions,
OnRemoveData,
OnUpdateData,
OnUpdateOptions,
Scalar,
SystemInfo,
TableInitOptions,
TypedArrayWindow,
UpdateOptions,
ViewConfig,
ViewConfigUpdate,
ViewWindow,
} from "@perspective-dev/client";
export type * from "../ts-rs/ViewerConfig.d.ts";
export type * from "../ts-rs/ViewerConfigUpdate.d.ts";
export type * from "../ts-rs/ViewerConfigInitial.d.ts";
export type * from "../ts-rs/PluginStaticConfig.d.ts";
export type * from "../ts-rs/WorkspaceConfig.d.ts";
export type * from "../ts-rs/TableRef.d.ts";
export type * from "../ts-rs/DerivedTableConfig.d.ts";
export type * from "../ts-rs/WorkspaceConfigUpdate.d.ts";
export type * from "../ts-rs/ExportMethod.d.ts";
export type * from "../ts-rs/PanelOptions.d.ts";
export type * from "../ts-rs/RestoreOptions.d.ts";
export type * from "../ts-rs/RestoreWorkspaceOptions.d.ts";
export type * from "../ts-rs/AddPanelOptions.d.ts";
export type * from "../ts-rs/SaveWorkspaceOptions.d.ts";
export type * from "../ts-rs/ClientOptions.d.ts";
export type * from "../ts-rs/ExportOptions.d.ts";
export type * from "../ts-rs/GetTableOptions.d.ts";
export type * from "../ts-rs/GetClientOptions.d.ts";
export type * from "../ts-rs/GetViewOptions.d.ts";
export type * from "../ts-rs/GetViewMode.d.ts";
export type * from "../ts-rs/CustomNumberFormatConfig.d.ts";
export type * from "../ts-rs/NumberFormatStyle.d.ts";
export type * from "../ts-rs/Notation.d.ts";
export type * from "../ts-rs/DatetimeFormat.d.ts";
export type * from "../ts-rs/ConfigDefaultsOptions.d.ts";
export type * from "../ts-rs/ColumnConfigDefaultsOptions.d.ts";
export type * from "../ts-rs/CommandSchema.d.ts";
export type * from "../ts-rs/CommandSpec.d.ts";
export type * from "../ts-rs/CommandLocation.d.ts";
export type * from "../ts-rs/CommandSchemaOptions.d.ts";
export type * from "../ts-rs/PerspectiveHit.d.ts";
export type * from "../ts-rs/PerspectiveHitRef.d.ts";
export type ExportData =
| string
| ArrayBuffer
| Blob
| Record<string, unknown[]>
| ViewerConfig;
import type {GetTableOptions} from "../ts-rs/GetTableOptions.d.ts";
import type {PanelOptions} from "../ts-rs/PanelOptions.d.ts";
import type {RestoreOptions} from "../ts-rs/RestoreOptions.d.ts";
import type {RestoreWorkspaceOptions} from "../ts-rs/RestoreWorkspaceOptions.d.ts";
import type {AddPanelOptions} from "../ts-rs/AddPanelOptions.d.ts";
import type {SaveWorkspaceOptions} from "../ts-rs/SaveWorkspaceOptions.d.ts";
import type {ConfigDefaultsOptions} from "../ts-rs/ConfigDefaultsOptions.d.ts";
import type {ColumnConfigDefaultsOptions} from "../ts-rs/ColumnConfigDefaultsOptions.d.ts";
import type {ExportOptions} from "../ts-rs/ExportOptions.d.ts";
import type {GetClientOptions} from "../ts-rs/GetClientOptions.d.ts";
import type {GetViewOptions} from "../ts-rs/GetViewOptions.d.ts";
import type {ClientOptions} from "../ts-rs/ClientOptions.d.ts";
import type {ViewerConfig} from "../ts-rs/ViewerConfig.d.ts";
import type {ViewerConfigUpdate} from "../ts-rs/ViewerConfigUpdate.d.ts";
import type {ViewerConfigInitial} from "../ts-rs/ViewerConfigInitial.d.ts";
import type {WorkspaceConfig} from "../ts-rs/WorkspaceConfig.d.ts";
import type {WorkspaceConfigUpdate} from "../ts-rs/WorkspaceConfigUpdate.d.ts";
import type {CommandSchema} from "../ts-rs/CommandSchema.d.ts";
import type {CommandSpec} from "../ts-rs/CommandSpec.d.ts";
import type {CommandLocation} from "../ts-rs/CommandLocation.d.ts";
import type {CommandSchemaOptions} from "../ts-rs/CommandSchemaOptions.d.ts";
import type {PerspectiveHit} from "../ts-rs/PerspectiveHit.d.ts";
import type {PerspectiveHitRef} from "../ts-rs/PerspectiveHitRef.d.ts";
/** A surface a command schema is evaluated for. */
export type PerspectiveCommandLocation = CommandLocation;
/**
* A window of a plugin's viewport, in the shape of the parsed
* `to_columns_string({ id: true })` result and the `column_paths()` area.
*/
export interface PerspectiveViewFrame {
/** The view row index of `data`'s first entry. */
start_row: number;
/** The view column index of `paths[0]`. */
start_col: number;
/** The `data` key of each column from `start_col`. */
paths: string[];
/** One level per `split_by` plus the column name, each over `paths`. */
column_paths: Scalar[][];
/** Columns keyed by path, with `__ROW_PATH__` and `__ID__` when present. */
data: Record<string, unknown[]>;
}
/** What a plugin rendered at a viewport point, before the viewer resolves it. */
export interface PerspectiveHitTest {
frame: PerspectiveViewFrame;
ref: PerspectiveHitRef;
}
/** One `queryCommands()` result: the context the schema saw and what it returned. */
export interface PerspectiveCommands {
context: PerspectiveCommandContext;
commands: CommandSpec[];
}
/** What a command schema is evaluated against. */
export interface PerspectiveCommandContext {
location: PerspectiveCommandLocation;
/** The targeted panel, absent only at zero panels. */
panel?: string;
/** The name of the panel's `Table`. */
table?: string;
/** The name of the panel's plugin. */
plugin?: string;
/** The panel's view config, as `getViewConfig()` reports it. */
view_config?: ViewConfig;
/** Viewport coordinates of the pointer that initiated the command, for the context menu. */
point?: { x: number; y: number };
/** What the plugin rendered at the location, absent over tabs and chrome. */
hit?: PerspectiveHit;
}
/** A synchronous, effect-free function declaring the commands for a context. */
export type PerspectiveCommandSchema = (
context: PerspectiveCommandContext,
) => CommandSchema;
/** The `detail` of a `perspective-command` event. */
export interface PerspectiveCommandEventDetail {
key: string;
context: PerspectiveCommandContext;
}
"#;
#[wasm_bindgen]
#[allow(non_snake_case)]
pub fn registerPlugin(name: &str) {
use crate::renderer::*;
PLUGIN_REGISTRY.register_plugin(name);
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "Record<string, unknown> | null")]
pub type JsConfigDefaults;
#[wasm_bindgen(typescript_type = "ConfigDefaultsOptions")]
pub type JsConfigDefaultsOptions;
#[wasm_bindgen(typescript_type = "ColumnConfigDefaultsOptions")]
pub type JsColumnConfigDefaultsOptions;
#[wasm_bindgen(
typescript_type = "Array<{ plugin?: string; defaults: Record<string, unknown> }>"
)]
pub type JsPluginConfigDefaultsSlots;
#[wasm_bindgen(
typescript_type = "Array<{ plugin?: string; type?: ColumnType; defaults: Record<string, \
unknown> }>"
)]
pub type JsColumnConfigDefaultsSlots;
}
fn defaults_options<T: Default + serde::de::DeserializeOwned>(
options: Option<impl Into<JsValue>>,
) -> ApiResult<T> {
match options {
None => Ok(T::default()),
Some(options) => Ok(options.into().into_serde_ext()?),
}
}
#[wasm_bindgen]
#[allow(non_snake_case)]
pub fn setPluginConfigDefaults(
defaults: JsConfigDefaults,
options: Option<JsConfigDefaultsOptions>,
) -> ApiResult<()> {
let config::ConfigDefaultsOptions { plugin } = defaults_options(options)?;
renderer::set_plugin_config_defaults(&defaults, plugin.as_deref())
}
#[wasm_bindgen]
#[allow(non_snake_case)]
pub fn setColumnConfigDefaults(
defaults: JsConfigDefaults,
options: Option<JsColumnConfigDefaultsOptions>,
) -> ApiResult<()> {
let config::ColumnConfigDefaultsOptions { plugin, ty } = defaults_options(options)?;
renderer::set_column_config_defaults(&defaults, plugin.as_deref(), ty.as_deref())
}
#[wasm_bindgen]
#[allow(non_snake_case)]
pub fn getPluginConfigDefaults() -> ApiResult<JsPluginConfigDefaultsSlots> {
Ok(renderer::get_plugin_config_defaults()?.unchecked_into())
}
#[wasm_bindgen]
#[allow(non_snake_case)]
pub fn getColumnConfigDefaults() -> ApiResult<JsColumnConfigDefaultsSlots> {
Ok(renderer::get_column_config_defaults()?.unchecked_into())
}
#[cfg(not(feature = "external-bootstrap"))]
#[wasm_bindgen(js_name = "init")]
pub fn js_init(module: js_sys::WebAssembly::Module, url: web_sys::Url) {
console_error_panic_hook::set_once();
perspective_js::utils::set_global_logging();
define_web_components!("export * as psp from '../../perspective-viewer.js'");
MODULE.with_borrow_mut(|f| {
*f = Some((module, url));
});
tracing::info!("Perspective initialized.");
}
thread_local! {
static MODULE: RefCell<Option<(js_sys::WebAssembly::Module, web_sys::Url)>> = RefCell::default();
}
#[cfg(not(feature = "external-bootstrap"))]
#[wasm_bindgen(js_name = "get_wasm_module")]
pub fn js_get_module() -> Result<js_sys::WebAssembly::Module, JsValue> {
MODULE
.with_borrow(|f| f.clone().map(|x| x.0))
.ok_or_else(|| "Uninited module".into())
}
#[cfg(not(feature = "external-bootstrap"))]
#[wasm_bindgen(js_name = "get_worker_url")]
pub fn js_get_worker_url() -> Result<web_sys::Url, JsValue> {
MODULE
.with_borrow(|f| f.clone().map(|x| x.1))
.ok_or_else(|| "Uninited module".into())
}
pub fn bootstrap_web_components(psp: &JsValue) {
define_web_component::<PerspectiveViewerElement>(psp);
define_web_component::<PerspectiveDebugPluginElement>(psp);
}
#[macro_export]
macro_rules! define_web_components {
($x:expr) => {{
#[wasm_bindgen::prelude::wasm_bindgen(inline_js = $x)]
extern "C" {
#[wasm_bindgen::prelude::wasm_bindgen(js_name = "psp")]
#[wasm_bindgen::prelude::wasm_bindgen(thread_local_v2)]
pub static PSP: wasm_bindgen::prelude::JsValue;
}
PSP.with(|x| $crate::bootstrap_web_components(x));
}};
}