use crate::node::Node;
use serde::{Deserialize, Serialize};
fn is_false(value: &bool) -> bool {
!*value
}
pub const IMPORT_NAME: &str = "import";
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct EntryOptions {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(default)]
pub name: String,
#[serde(default, skip_serializing_if = "is_false")]
pub disabled: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub inject: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub group: Vec<EntryOptions>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub config: Option<Node>,
}
impl EntryOptions {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
..Self::default()
}
}
pub fn with_id(mut self, id: impl Into<String>) -> Self {
self.id = Some(id.into());
self
}
pub fn with_config(mut self, config: Node) -> Self {
self.config = Some(config);
self
}
pub fn with_group(mut self, group: Vec<EntryOptions>) -> Self {
self.group = group;
self
}
pub fn with_disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn import_url(&self) -> Option<&str> {
if self.name != IMPORT_NAME {
return None;
}
self.config.as_ref()?.as_object()?.get("url")?.as_str()
}
pub fn with_inject<I, S>(mut self, inject: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.inject = inject.into_iter().map(Into::into).collect();
self
}
}