use std::collections::BTreeSet;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "process", derive(serde::Serialize, serde::Deserialize))]
pub struct Requirements {
engine_apis: BTreeSet<String>,
locales: Vec<String>,
async_context: bool,
host_apis: BTreeSet<String>,
host_protocol: Option<u32>,
preemption: bool,
}
impl Requirements {
pub fn new() -> Self {
Self::default()
}
pub fn engine_api(mut self, api: impl Into<String>) -> Self {
self.engine_apis.insert(api.into());
self
}
pub fn locales<I, S>(mut self, locales: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
for locale in locales {
let locale = locale.into();
if !self.locales.contains(&locale) {
self.locales.push(locale);
}
}
self
}
pub fn async_context(mut self) -> Self {
self.async_context = true;
self
}
pub fn host_api(mut self, api: impl Into<String>) -> Self {
self.host_apis.insert(api.into());
self
}
pub fn host_protocol(mut self, version: u32) -> Self {
self.host_protocol = Some(version);
self
}
pub fn preemption(mut self) -> Self {
self.preemption = true;
self
}
pub fn is_empty(&self) -> bool {
self.engine_apis.is_empty()
&& !self.async_context
&& !self.preemption
&& self.host_apis.is_empty()
&& self.host_protocol.is_none()
}
}
#[cfg_attr(
not(any(feature = "boa", feature = "quickjs", feature = "v8")),
allow(dead_code)
)]
impl Requirements {
pub(crate) fn engine_apis(&self) -> impl Iterator<Item = &str> {
self.engine_apis.iter().map(String::as_str)
}
pub(crate) fn locale_list(&self) -> &[String] {
&self.locales
}
pub(crate) fn needs_async_context(&self) -> bool {
self.async_context
}
pub(crate) fn needs_preemption(&self) -> bool {
self.preemption
}
#[cfg(fetchlet_process)]
pub(crate) fn without_preemption(mut self) -> Self {
self.preemption = false;
self
}
pub(crate) fn host_apis(&self) -> impl Iterator<Item = &str> {
self.host_apis.iter().map(String::as_str)
}
pub(crate) fn protocol(&self) -> Option<u32> {
self.host_protocol
}
}