use pile_config::Label;
use std::sync::OnceLock;
use super::TomlExtractor;
use crate::{
extract::traits::ObjectExtractor,
value::{Item, PileValue},
};
pub struct SidecarExtractor {
item: Item,
output: OnceLock>,
}
impl SidecarExtractor {
pub fn new(item: &Item) -> Self {
Self {
item: item.clone(),
output: OnceLock::new(),
}
}
}
#[async_trait::async_trait]
impl ObjectExtractor for SidecarExtractor {
async fn field(
&self,
name: &Label,
args: Option<&str>,
) -> Result , std::io::Error> {
match self
.output
.get_or_init(|| self.item.sidecar().map(TomlExtractor::new))
{
Some(x) => Ok(x.field(name, args).await?),
None => Ok(Some(PileValue::Null)),
}
}
async fn fields(&self) -> Result, std::io::Error> {
match self
.output
.get_or_init(|| self.item.sidecar().map(TomlExtractor::new))
{
Some(x) => Ok(x.fields().await?),
None => Ok(Vec::new()),
}
}
}