use serde::{Deserialize, Serialize};
use serde_json::json;
use std::error::Error;
use std::fmt;
pub use crate::api::API;
pub struct Extractor {
api: API
}
impl Extractor {
pub fn new() -> Extractor {
Extractor {
api: API::new()
}
}
pub fn with_url(url: &str) -> Extractor {
Extractor {
api: API::with_url(url)
}
}
pub fn with_url_token(url: &str, token: &str) -> Extractor {
Extractor {
api: API::with_url_token(url, token)
}
}
pub async fn extract(&self, queue: &Vec<Question>, texts: &Vec<&str>) -> Answers {
let params = json!({"queue": queue, "texts": texts});
Ok(self.api.post("extract", ¶ms).await?.json().await?)
}
}
pub type Answers = Result<Vec<Answer>, Box<dyn Error>>;
#[derive(Debug, Serialize)]
pub struct Question {
pub name: String,
pub query: String,
pub question: String,
pub snippet: bool
}
#[derive(Debug, Deserialize)]
pub struct Answer {
pub name: String,
pub answer: String
}
impl fmt::Display for Answer {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {}", self.name, self.answer)
}
}