//! `GET /applist` (`docs/PROTOCOL.md` 1.3). use crate::Result; use crate::util::{cap, malformed, parse_u32}; /// Maximum size of an applist body, in bytes. pub const MAX_APPLIST_BYTES: usize = 1024 * 1024; /// Maximum number of apps accepted in one list. pub const MAX_APPS: usize = 4096; /// Maximum length of an app title, in bytes. pub const MAX_APP_TITLE_BYTES: usize = 512; /// One app the host can launch. #[derive(Clone, Debug, PartialEq, Eq)] pub struct App { /// `ID`: the value for `appid` in launch. pub id: u32, /// `AppTitle`, unescaped. pub title: String, /// `IsHdrSupported == 1`. pub hdr_supported: bool, } /// Parses an applist body. Apps keep the host's order; duplicate ids are refused; other elements are ignored. /// /// # Errors /// [`crate::Error::TooLarge`] over [`MAX_APPLIST_BYTES`], [`MAX_APPS`] or [`MAX_APP_TITLE_BYTES`], /// [`crate::Error::HostStatus`] for a non-200 answer, [`crate::Error::Malformed`] for an `App` without `ID` or /// `AppTitle`, a bad `ID`, or a duplicate `ID`. pub fn parse_app_list(body: &[u8]) -> Result> { let doc = super::document(body, MAX_APPLIST_BYTES, "applist")?; let count = doc.root.children.iter().filter(|c| c.name == "App").count(); cap("applist apps", MAX_APPS, count)?; let mut apps: Vec = Vec::with_capacity(count); let mut ids = std::collections::BTreeSet::new(); for e in doc.root.children.iter().filter(|c| c.name == "App") { let title = e.child("AppTitle").ok_or(malformed("applist: App without AppTitle"))?; cap("applist AppTitle", MAX_APP_TITLE_BYTES, title.text.len())?; let id = e.child("ID").ok_or(malformed("applist: App without ID"))?; let id = parse_u32("applist ID", &id.text)?; if !ids.insert(id) { return Err(malformed("applist: duplicate ID")); } let hdr_supported = e.child("IsHdrSupported").is_some_and(|h| h.text == "1"); apps.push(App { id, title: title.text.clone(), hdr_supported }); } Ok(apps) }