#![deny(warnings, missing_docs, missing_copy_implementations, missing_debug_implementations)]
pub fn search(query: &str) -> Result<Vec<SearchResult>, Error> {
search_with_options(query, Options::default())
}
pub fn search_with_options(query: &str, options: Options) -> Result<Vec<SearchResult>, Error> {
let language = options.language.to_string();
Ok(ureq::post("https://learngaelic.scot/dictionary/search")
.send_form(&[
("abairt", query),
("slang", &language[..]),
("wholeword", if options.whole_word { "true" } else { "false" }),
])?
.into_json()?)
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Language {
English,
Gaelic,
Both,
}
impl std::fmt::Display for Language {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
use Language::*;
Ok(match self {
English => f.write_fmt(format_args!("en"))?,
Gaelic => f.write_fmt(format_args!("gd"))?,
Both => f.write_fmt(format_args!("both"))?,
})
}
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct SearchResult {
pub id: usize,
pub headword: String,
pub source: Option<String>,
pub translation: String,
pub grammar: String,
pub ipa: String,
pub wordclass: String,
#[serde(rename = "hasAudio")]
pub has_audio: bool,
#[serde(rename = "audioVersion")]
pub audio_version: usize,
#[serde(rename = "gdKeys")]
pub gd_keys: Option<String>,
#[serde(rename = "enKeys")]
pub en_keys: Option<String>,
#[serde(rename = "commitMessage")]
pub commit_message: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Options {
whole_word: bool,
language: Language,
}
impl Options {
pub fn whole_word(&mut self) -> &mut Options {
self.whole_word = true;
self
}
pub fn language(&mut self, language: Language) -> &mut Options {
self.language = language;
self
}
}
impl std::default::Default for Options {
fn default() -> Options {
Options {
whole_word: false,
language: Language::Gaelic,
}
}
}
#[derive(Debug, Display)]
pub enum Error {
Ureq(ureq::Error),
Io(std::io::Error),
}
impl std::error::Error for Error {}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Error {
Error::Io(e)
}
}
impl From<ureq::Error> for Error {
fn from(e: ureq::Error) -> Error {
Error::Ureq(e)
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = super::search("saor").expect("Problem when searching for 'saor'");
assert_eq!(result[0].translation, "1 free, liberate! 2 absolve! 3 exempt!");
}
}
use derive_more::Display;
use serde::{Deserialize, Serialize};