From 1af1451ad9bed7a5ce26093bd2e6fbcdb3c08632 Mon Sep 17 00:00:00 2001 From: yuma14 Date: Fri, 10 Nov 2023 17:09:56 +0900 Subject: [PATCH] add JsonLoader --- src/loader.rs | 2 ++ src/loader/json_loader.rs | 17 +++++++++++++++++ src/pipeline.rs | 12 ++++++------ src/pipeline/resource.rs | 6 +++--- 4 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 src/loader/json_loader.rs diff --git a/src/loader.rs b/src/loader.rs index 5cc29ec..b2e4773 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -3,11 +3,13 @@ use std::io::Read; use crate::value::Value; mod blob_loader; +mod json_loader; mod template_loader; mod text_loader; mod text_with_frontmatter_loader; pub use blob_loader::*; +pub use json_loader::*; pub use template_loader::*; pub use text_loader::*; pub use text_with_frontmatter_loader::*; diff --git a/src/loader/json_loader.rs b/src/loader/json_loader.rs new file mode 100644 index 0000000..2acc504 --- /dev/null +++ b/src/loader/json_loader.rs @@ -0,0 +1,17 @@ +use anyhow::Context; + +use crate::value::Value; + +use super::Loader; + +pub struct JsonLoader; + +impl Loader for JsonLoader { + #[tracing::instrument(err, skip_all)] + fn load(reader: impl std::io::Read) -> anyhow::Result { + let json = + serde_json::from_reader(reader).with_context(|| "Could not parse JSON".to_string())?; + + Ok(Value::JSON(json)) + } +} diff --git a/src/pipeline.rs b/src/pipeline.rs index 62e7f47..2f6af35 100644 --- a/src/pipeline.rs +++ b/src/pipeline.rs @@ -15,8 +15,8 @@ pub use step::*; use tracing::{debug, info, span, Level}; use crate::{ - directory, store::Store, transformer::Transformer, BlobLoader, Loader, TemplateLoader, - TextLoader, TextWithFrontmatterLoader, Value, + directory, store::Store, transformer::Transformer, BlobLoader, JsonLoader, Loader, + TemplateLoader, TextWithFrontmatterLoader, Value, }; #[derive(Debug, Deserialize, Serialize)] @@ -50,10 +50,10 @@ impl Pipeline { debug!("start loading entry with {:?} Loader", self.entry.type_); let value = match self.entry.type_ { EnumLoader::TextWithFrontmatter => TextWithFrontmatterLoader::load(&entry_bytes[..]), - EnumLoader::Json => todo!(), + EnumLoader::Json => JsonLoader::load(&entry_bytes[..]), EnumLoader::Blob => BlobLoader::load(&entry_bytes[..]), EnumLoader::Template => TemplateLoader::load(&entry_bytes[..]), - EnumLoader::Text => TextLoader::load(&entry_bytes[..]), + EnumLoader::Text => JsonLoader::load(&entry_bytes[..]), } .with_context(|| format!("failed to load entry with {:?} Loader", self.entry.type_))?; store.set("entry".to_string(), value); @@ -75,12 +75,12 @@ impl Pipeline { } else if let Some(bytes) = resource.get_bytes(&index) { let value = match with { EnumLoader::Template => TemplateLoader::load(bytes), - EnumLoader::Json => todo!(), + EnumLoader::Json => JsonLoader::load(bytes), EnumLoader::TextWithFrontmatter => { TextWithFrontmatterLoader::load(bytes) } EnumLoader::Blob => BlobLoader::load(bytes), - EnumLoader::Text => TextLoader::load(bytes), + EnumLoader::Text => JsonLoader::load(bytes), } .with_context(|| { format!( diff --git a/src/pipeline/resource.rs b/src/pipeline/resource.rs index ef388a4..e1883d4 100644 --- a/src/pipeline/resource.rs +++ b/src/pipeline/resource.rs @@ -4,7 +4,7 @@ use anyhow::Context; use path_absolutize::Absolutize; use tracing::{debug, info, span, Level}; -use crate::{BlobLoader, Loader, TemplateLoader, TextLoader, TextWithFrontmatterLoader, Value}; +use crate::{BlobLoader, JsonLoader, Loader, TemplateLoader, TextWithFrontmatterLoader, Value}; use super::Pipeline; @@ -42,12 +42,12 @@ impl Resource { let bytes = &byte_map.get(&index).unwrap()[..]; let value = match with { crate::pipeline::EnumLoader::Template => TemplateLoader::load(bytes), - crate::pipeline::EnumLoader::Json => todo!(), + crate::pipeline::EnumLoader::Json => JsonLoader::load(bytes), crate::pipeline::EnumLoader::TextWithFrontmatter => { TextWithFrontmatterLoader::load(bytes) } crate::pipeline::EnumLoader::Blob => BlobLoader::load(bytes), - crate::pipeline::EnumLoader::Text => TextLoader::load(bytes), + crate::pipeline::EnumLoader::Text => JsonLoader::load(bytes), } .with_context(|| format!("failed to preload with {:?}", with))?; value_map.insert(index, value);