Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Jul 24, 2024
1 parent be0af28 commit e1933d9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
23 changes: 14 additions & 9 deletions dsp-meta/src/api/convert/serde/json_schema_validator.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use std::collections::HashMap;
use std::fs::File;
use std::path::{Path};
use std::path::Path;

use serde_json::Value;
use valico::json_schema::{Scope, ValidationState};
use valico::json_schema::schema::ScopedSchema;
use valico::json_schema::{Scope, ValidationState};

use crate::api::convert::serde::json_schema_validator::SchemaVersion::Draft;
use crate::api::convert::serde::json_schema_validator::ValidationError::*;

static DRAFT_SCHEMA: &str = include_str!("../../../../resources/schema-metadata-draft.json");

pub enum SchemaVersion {
Draft
Draft,
}
impl SchemaVersion {
fn schema_str(&self) -> &str {
match self {
Draft => DRAFT_SCHEMA
Draft => DRAFT_SCHEMA,
}
}
}
Expand All @@ -37,7 +37,10 @@ pub fn validate_file(path: &Path, schema_version: SchemaVersion) -> Result<Valid
Ok(schema.validate(&contents))
}

pub fn validate_files(paths: Vec<&Path>, schema_version: SchemaVersion) -> Result<HashMap<&Path, ValidationState>> {
pub fn validate_files(
paths: Vec<&Path>,
schema_version: SchemaVersion,
) -> Result<HashMap<&Path, ValidationState>> {
let mut scope = Scope::new();
let schema = load_json_schema(schema_version, &mut scope)?;
let mut results = HashMap::with_capacity(paths.len());
Expand All @@ -50,13 +53,15 @@ pub fn validate_files(paths: Vec<&Path>, schema_version: SchemaVersion) -> Resul
}

fn load_path_as_json(path: &Path) -> Result<Value> {
let file = File::open(path).map_err(|e| { FileNotLoaded(e) })?;
let file = File::open(path).map_err(|e| FileNotLoaded(e))?;
let value = serde_json::from_reader::<File, Value>(file);
value.map_err(|e| { NotAJsonFile(e) })
value.map_err(|e| NotAJsonFile(e))
}

fn load_json_schema(schema_version: SchemaVersion, scope: &mut Scope) -> Result<ScopedSchema> {
let schema_str = schema_version.schema_str();
let json = serde_json::from_str(schema_str).map_err(|e| NotAJsonFile(e))?;
scope.compile_and_return(json, false).map_err(|e| { SchemaError(e) })
}
scope
.compile_and_return(json, false)
.map_err(|e| SchemaError(e))
}
12 changes: 7 additions & 5 deletions dsp-meta/tests/draft_schema_test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{env, fs};
use std::path::{Path, PathBuf};
use std::{env, fs};

use api::convert::serde::draft_model::*;
use dsp_meta::api;
use dsp_meta::api::convert::serde::json_schema_validator::{SchemaVersion, validate_files};
use dsp_meta::api::convert::serde::json_schema_validator::{validate_files, SchemaVersion};

#[test]
fn test_json_and_yaml_serialization_are_equal() {
Expand Down Expand Up @@ -38,8 +38,7 @@ fn test_deserialization_data() {
for path in paths {
let path = path.as_path();
println!("Checking {}:", path.to_str().get_or_insert(""));
let contents =
fs::read_to_string(path).expect("Should have been able to read the file");
let contents = fs::read_to_string(path).expect("Should have been able to read the file");
let metadata = serde_json::from_str::<DraftMetadata>(&*contents);
match metadata {
Ok(_data) => {
Expand All @@ -61,7 +60,10 @@ fn test_deserialization_data() {
}

fn collect_data_json_paths() -> Vec<PathBuf> {
let mut current_dir = env::current_dir().ok().and_then(|e| e.parent().map(|p| p.to_path_buf())).expect("Project root dir");
let mut current_dir = env::current_dir()
.ok()
.and_then(|e| e.parent().map(|p| p.to_path_buf()))
.expect("Project root dir");
current_dir.push("data");
current_dir.push("json");
fs::read_dir(current_dir)
Expand Down
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ check:
cargo +nightly fmt --check
cargo clippy -- -D warnings

# format all rust code
fmt:
cargo fmt

# Fix justfile formating. Warning: will change existing file. Please first use check.
fix:
just --fmt --unstable
Expand Down

0 comments on commit e1933d9

Please sign in to comment.