46 lines
935 B
Rust
46 lines
935 B
Rust
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<Option<TomlExtractor>>,
|
|
}
|
|
|
|
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) -> Result<Option<PileValue>, std::io::Error> {
|
|
match self
|
|
.output
|
|
.get_or_init(|| self.item.sidecar().map(TomlExtractor::new))
|
|
{
|
|
Some(x) => Ok(x.field(name).await?),
|
|
None => Ok(Some(PileValue::Null)),
|
|
}
|
|
}
|
|
|
|
async fn fields(&self) -> Result<Vec<Label>, 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()),
|
|
}
|
|
}
|
|
}
|