Skip to content

Commit

Permalink
Merge pull request #70 from yuma140902/add-json-loader
Browse files Browse the repository at this point in the history
add JsonLoader
  • Loading branch information
yuma140902 authored Nov 10, 2023
2 parents d85e99c + 1af1451 commit cf1e831
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
17 changes: 17 additions & 0 deletions src/loader/json_loader.rs
Original file line number Diff line number Diff line change
@@ -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<Value> {
let json =
serde_json::from_reader(reader).with_context(|| "Could not parse JSON".to_string())?;

Ok(Value::JSON(json))
}
}
12 changes: 6 additions & 6 deletions src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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);
Expand All @@ -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!(
Expand Down
6 changes: 3 additions & 3 deletions src/pipeline/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit cf1e831

Please sign in to comment.