oxirs_wasm/
lib.rs

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