use crate::{
error::{Error, Result},
Bravia, RequestBodyBuilder, RequestBuilder,
};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::cmp::Ordering;
const ENDPOINT: &str = "guide";
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Versions {
pub version: String,
pub protocols: Option<Vec<String>>,
pub auth_level: Option<String>,
}
impl Ord for Versions {
fn cmp(&self, other: &Self) -> Ordering {
self.version.cmp(&other.version)
}
}
impl PartialOrd for Versions {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Api {
pub name: String,
pub versions: Vec<Versions>,
}
impl Ord for Api {
fn cmp(&self, other: &Self) -> Ordering {
let self_max = self.versions.iter().max();
let other_max = other.versions.iter().max();
self_max.cmp(&other_max)
}
}
impl PartialOrd for Api {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Notifications {
pub name: String,
pub versions: Vec<Versions>,
}
impl Ord for Notifications {
fn cmp(&self, other: &Self) -> Ordering {
let self_max = self.versions.iter().max();
let other_max = other.versions.iter().max();
self_max.cmp(&other_max)
}
}
impl PartialOrd for Notifications {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct ServiceData {
pub apis: Vec<Api>,
pub notifications: Option<Vec<Notifications>>,
pub service: String,
pub protocols: Vec<String>,
}
pub struct GuideService<'a>(&'a Bravia);
impl<'a> GuideService<'a> {
pub fn new(bravia: &'a Bravia) -> Self {
Self(bravia)
}
pub async fn get_supported_api_info(
&self,
services: Option<Vec<String>>,
) -> Result<Vec<ServiceData>> {
let mut params = Map::new();
if let Some(services) = services {
params.insert(String::from("services"), Value::from(services));
}
let body = RequestBodyBuilder::default()
.id(5)
.method("getSupportedApiInfo")
.params(Value::from(params))
.build()?;
let req = RequestBuilder::default()
.endpoint(ENDPOINT)
.body(body)
.has_result()
.make(self.0)
.await?;
let parsed: Vec<ServiceData> = serde_json::from_value(req)?;
if parsed.is_empty() {
Err(Error::MissingValue("apis"))
} else {
Ok(parsed)
}
}
}