use std::collections::{BTreeMap, HashMap};
#[cfg(feature = "regex")]
use regex::Regex;
use super::{Context, IntoKey, JSONGetTextBuilder, LookupKey};
use crate::{JSONGetTextBuildError, JSONGetTextValue, text_lookup};
#[derive(Debug)]
pub struct JSONGetText<'a> {
default_key: crate::Key,
context: Context<'a>,
}
impl<'a> JSONGetText<'a> {
#[inline]
pub fn build<K: IntoKey>(default_key: K) -> JSONGetTextBuilder<'a> {
JSONGetTextBuilder::new(default_key)
}
pub(crate) fn from_context_with_default_key(
default_key: crate::Key,
context: Context<'a>,
allow_extra_texts: bool,
) -> Result<JSONGetText<'a>, JSONGetTextBuildError> {
let context = text_lookup::finalize_context(&default_key, context, allow_extra_texts)?;
Ok(JSONGetText {
default_key,
context,
})
}
#[cfg(not(feature = "langid"))]
#[inline]
pub fn get_default_key(&self) -> &str {
self.default_key.as_str()
}
#[cfg(feature = "langid")]
#[inline]
pub fn get_default_key(&self) -> crate::Key {
self.default_key
}
#[cfg(not(feature = "langid"))]
pub fn get_keys(&self) -> Vec<&str> {
self.context.keys().map(|key| key.as_str()).collect()
}
#[cfg(feature = "langid")]
pub fn get_keys(&self) -> Vec<crate::Key> {
self.context.keys().copied().collect()
}
#[inline]
pub fn contains_key<K: LookupKey>(&self, key: K) -> bool {
key.lookup(&self.context).is_some()
}
#[inline]
pub fn get<K: LookupKey>(&self, key: K) -> &HashMap<String, JSONGetTextValue<'a>> {
key.lookup(&self.context).unwrap_or_else(|| self.default_map())
}
#[inline]
pub fn get_text<T: AsRef<str>>(&self, text: T) -> Option<JSONGetTextValue<'_>> {
let map = self.default_map();
text_lookup::get_text(map, map, text.as_ref())
}
#[inline]
pub fn get_text_with_key<K: LookupKey, T: AsRef<str>>(
&self,
key: K,
text: T,
) -> Option<JSONGetTextValue<'_>> {
let default_map = self.default_map();
let map = key.lookup(&self.context).unwrap_or(default_map);
text_lookup::get_text(map, default_map, text.as_ref())
}
pub fn get_multiple_text<'b, T: AsRef<str> + ?Sized>(
&self,
text_array: &[&'b T],
) -> Option<BTreeMap<&'b str, JSONGetTextValue<'_>>> {
let map = self.default_map();
text_lookup::get_multiple_text(map, map, text_array)
}
pub fn get_multiple_text_with_key<'b, K: LookupKey, T: AsRef<str> + ?Sized>(
&self,
key: K,
text_array: &[&'b T],
) -> Option<BTreeMap<&'b str, JSONGetTextValue<'_>>> {
let default_map = self.default_map();
let map = key.lookup(&self.context).unwrap_or(default_map);
text_lookup::get_multiple_text(map, default_map, text_array)
}
#[cfg(feature = "regex")]
pub fn get_filtered_text(&self, regex: &Regex) -> Option<BTreeMap<&str, JSONGetTextValue<'_>>> {
Some(text_lookup::get_filtered_text(self.default_map(), regex))
}
#[cfg(feature = "regex")]
pub fn get_filtered_text_with_key<K: LookupKey>(
&self,
key: K,
regex: &Regex,
) -> Option<BTreeMap<&str, JSONGetTextValue<'_>>> {
let map = key.lookup(&self.context).unwrap_or_else(|| self.default_map());
Some(text_lookup::get_filtered_text(map, regex))
}
#[inline]
fn default_map(&self) -> &HashMap<String, JSONGetTextValue<'a>> {
self.context.get(&self.default_key).unwrap()
}
}