#![deny( //These lint options are copied from https://pascalhertleif.de/artikel/good-practices-for-writing-rust-libraries/
missing_docs,
missing_debug_implementations,
missing_copy_implementations,
trivial_casts,
trivial_numeric_casts,
unsafe_code,
unstable_features,
unused_import_braces,
unused_qualifications
)]
extern crate reqwest;
extern crate serde;
extern crate serde_json;
use std::error;
use std::fmt::{self, Display, Formatter};
use std::result;
mod request;
mod response;
pub use request::*;
pub use response::*;
#[derive(Debug)]
pub struct DatamuseClient {
client: reqwest::Client,
}
impl DatamuseClient {
pub fn new() -> Self {
DatamuseClient {
client: reqwest::Client::new(),
}
}
pub fn new_query<'a>(
&'a self,
vocabulary: Vocabulary,
endpoint: EndPoint,
) -> RequestBuilder<'a> {
RequestBuilder::new(self, vocabulary, endpoint)
}
}
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
ReqwestError(reqwest::Error),
SerdeError(serde_json::Error),
VocabularyError((String, String)),
EndPointError((String, String)),
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::ReqwestError(err) => write!(f, "{}", err),
Self::SerdeError(err) => write!(f, "{}", err),
Self::VocabularyError((lang, param)) => write!(
f,
"Error: The parameter {} is not yet supported for {}",
param, lang
),
Self::EndPointError((endpoint, param)) => write!(
f,
"Error: The parameter {} is not supported for {}",
param, endpoint
),
}
}
}
impl error::Error for Error {}
impl From<reqwest::Error> for Error {
fn from(error: reqwest::Error) -> Self {
Error::ReqwestError(error)
}
}
impl From<serde_json::Error> for Error {
fn from(error: serde_json::Error) -> Self {
Error::SerdeError(error)
}
}