feat: export hitta parser as a WASM component via wit-bindgen
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -12,5 +12,8 @@ regex = "1"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
|
||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||
wit-bindgen = "0.57"
|
||||
|
||||
[dev-dependencies]
|
||||
insta = { version = "1.47", features = ["yaml"] }
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
use crate::parser;
|
||||
|
||||
wit_bindgen::generate!({
|
||||
world: "provider",
|
||||
path: "../../../wit",
|
||||
});
|
||||
|
||||
use exports::whoareyou::provider::lookup::{
|
||||
Comment, Entry, Guest, LookupError, ProviderInfo, Request, Response,
|
||||
};
|
||||
|
||||
struct Component;
|
||||
|
||||
impl Guest for Component {
|
||||
fn metadata() -> ProviderInfo {
|
||||
ProviderInfo {
|
||||
name: "hitta.se".to_string(),
|
||||
version: env!("CARGO_PKG_VERSION").to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn requests(number: String) -> Vec<Request> {
|
||||
parser::request_urls(&number)
|
||||
.into_iter()
|
||||
.map(|url| Request { url })
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn parse(_number: String, responses: Vec<Response>) -> Result<Entry, LookupError> {
|
||||
let Some(first) = responses.first() else {
|
||||
return Err(LookupError::ParseFailed(
|
||||
"no responses provided".to_string(),
|
||||
));
|
||||
};
|
||||
|
||||
match parser::parse(&first.body) {
|
||||
Ok(entry) => Ok(Entry {
|
||||
messages: entry.messages,
|
||||
history: entry.history,
|
||||
comments: entry
|
||||
.comments
|
||||
.into_iter()
|
||||
.map(|c| Comment {
|
||||
timestamp: c.timestamp,
|
||||
title: c.title,
|
||||
message: c.message,
|
||||
})
|
||||
.collect(),
|
||||
}),
|
||||
Err(parser::ParseError::NoData) => Err(LookupError::NoData),
|
||||
Err(parser::ParseError::Failed(msg)) => Err(LookupError::ParseFailed(msg)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export!(Component);
|
||||
@@ -1,2 +1,4 @@
|
||||
// modules added as they are implemented
|
||||
pub mod parser;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
mod component;
|
||||
|
||||
Reference in New Issue
Block a user