oxirs_wasm/
lib.rs

1//! # OxiRS WASM
2//!
3//! WebAssembly bindings for OxiRS - Run RDF/SPARQL in the browser.
4//!
5//! ## Features
6//!
7//! - **RDF Parsing**: Turtle, N-Triples, JSON-LD
8//! - **In-Memory Store**: HashMap-based triple store
9//! - **SPARQL Queries**: SELECT, ASK, CONSTRUCT
10//! - **Validation**: Basic SHACL validation
11//!
12//! ## Example (JavaScript)
13//!
14//! ```javascript
15//! import init, { OxiRSStore } from 'oxirs-wasm';
16//!
17//! await init();
18//!
19//! const store = new OxiRSStore();
20//! await store.loadTurtle(`
21//!     @prefix : <http://example.org/> .
22//!     :alice :knows :bob .
23//!     :bob :name "Bob" .
24//! `);
25//!
26//! const results = store.query('SELECT ?name WHERE { ?person :name ?name }');
27//! console.log(results);
28//! ```
29
30mod error;
31mod parser;
32mod query;
33mod store;
34
35use wasm_bindgen::prelude::*;
36
37pub use error::WasmError;
38pub use store::OxiRSStore;
39
40// Initialize panic hook for better error messages
41#[wasm_bindgen(start)]
42pub fn init() {
43    console_error_panic_hook::set_once();
44}
45
46/// RDF Triple representation for JavaScript
47#[wasm_bindgen]
48#[derive(Debug, Clone)]
49pub struct Triple {
50    subject: String,
51    predicate: String,
52    object: String,
53}
54
55#[wasm_bindgen]
56impl Triple {
57    #[wasm_bindgen(constructor)]
58    pub fn new(subject: &str, predicate: &str, object: &str) -> Self {
59        Self {
60            subject: subject.to_string(),
61            predicate: predicate.to_string(),
62            object: object.to_string(),
63        }
64    }
65
66    #[wasm_bindgen(getter)]
67    pub fn subject(&self) -> String {
68        self.subject.clone()
69    }
70
71    #[wasm_bindgen(getter)]
72    pub fn predicate(&self) -> String {
73        self.predicate.clone()
74    }
75
76    #[wasm_bindgen(getter)]
77    pub fn object(&self) -> String {
78        self.object.clone()
79    }
80}
81
82/// Query result for JavaScript
83#[wasm_bindgen]
84pub struct QueryResult {
85    bindings: Vec<JsValue>,
86}
87
88#[wasm_bindgen]
89impl QueryResult {
90    #[wasm_bindgen(getter)]
91    pub fn bindings(&self) -> Vec<JsValue> {
92        self.bindings.clone()
93    }
94
95    #[wasm_bindgen(getter)]
96    pub fn length(&self) -> usize {
97        self.bindings.len()
98    }
99}
100
101/// Get OxiRS WASM version
102#[wasm_bindgen]
103pub fn version() -> String {
104    env!("CARGO_PKG_VERSION").to_string()
105}
106
107/// Log a message to the browser console
108#[wasm_bindgen]
109pub fn log(message: &str) {
110    web_sys::console::log_1(&message.into());
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116
117    #[test]
118    fn test_triple() {
119        let triple = Triple::new("http://a", "http://b", "http://c");
120        assert_eq!(triple.subject(), "http://a");
121        assert_eq!(triple.predicate(), "http://b");
122        assert_eq!(triple.object(), "http://c");
123    }
124}