use crate::{Error, Result};
use remote_settings::RemoteSettingsResponse;
use serde::Deserialize;
pub(crate) const REMOTE_SETTINGS_COLLECTION: &str = "content-relevance";
pub(crate) trait RelevancyRemoteSettingsClient {
fn get_records(&self) -> Result<RemoteSettingsResponse>;
fn get_attachment(&self, location: &str) -> Result<Vec<u8>>;
}
impl RelevancyRemoteSettingsClient for remote_settings::RemoteSettings {
fn get_records(&self) -> Result<RemoteSettingsResponse> {
Ok(remote_settings::RemoteSettings::get_records(self)?)
}
fn get_attachment(&self, location: &str) -> Result<Vec<u8>> {
Ok(remote_settings::RemoteSettings::get_attachment(
self, location,
)?)
}
}
#[derive(Clone, Debug, Deserialize)]
pub struct RelevancyRecord {
#[allow(dead_code)]
#[serde(rename = "type")]
pub record_type: String,
pub record_custom_details: RecordCustomDetails,
}
#[derive(Clone, Debug, Deserialize)]
pub struct RecordCustomDetails {
pub category_to_domains: CategoryToDomains,
}
#[derive(Clone, Debug, Deserialize)]
pub struct CategoryToDomains {
#[allow(dead_code)]
pub version: i32,
#[allow(dead_code)]
pub category: String,
pub category_code: i32,
}
#[derive(Clone, Debug, Deserialize)]
pub struct RelevancyAttachmentData {
pub domain: String,
}
pub fn from_json<T: serde::de::DeserializeOwned>(value: serde_json::Value) -> Result<T> {
serde_path_to_error::deserialize(value).map_err(|e| Error::RemoteSettingsParseError {
type_name: std::any::type_name::<T>().to_owned(),
path: e.path().to_string(),
error: e.into_inner(),
})
}
pub fn from_json_slice<T: serde::de::DeserializeOwned>(value: &[u8]) -> Result<T> {
let json_value =
serde_json::from_slice(value).map_err(|e| Error::RemoteSettingsParseError {
type_name: std::any::type_name::<T>().to_owned(),
path: "<while parsing JSON>".to_owned(),
error: e,
})?;
from_json(json_value)
}