Skip to content

Commit

Permalink
Bare new()
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Jul 31, 2024
1 parent 1655a5d commit 490f4f3
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 67 deletions.
37 changes: 19 additions & 18 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,43 @@ use relative_path::{Component, RelativePath};
/// Public but undocumented and un-exported module that creates a
/// boon::Compiler for use in validation and Tests.
use std::error::Error;
use std::fs::File;
use std::path::Path;

use boon::Compiler;
use serde_json::Value;

use std::{fs::File, path::Path};
use wax::Glob;

/// new returns a new boon::Compiler with the schema files loaded from `dir`
/// and configured to validate `path` and `license` formats.
pub fn new<P: AsRef<Path>>(dir: P) -> Result<Compiler, Box<dyn Error>> {
pub fn new() -> Compiler {
let schema_v1 = include_str!(concat!(env!("OUT_DIR"), "/pgxn-meta-v1.schema.json"));
let schema_v2 = include_str!(concat!(env!("OUT_DIR"), "/pgxn-meta-v2.schema.json"));
let mut compiler = spec_compiler();

let glob = Glob::new("**/*.schema.json")?;
for path in glob.walk(dir) {
let schema: Value = serde_json::from_reader(File::open(path?.into_path())?)?;
for str in [schema_v1, schema_v2] {
let schema: Value = serde_json::from_str(str).unwrap();
let id = &schema["$id"]
.as_str()
.ok_or(super::valid::ValidationError::UnknownID)?;
compiler.add_resource(id, schema.to_owned())?;
.ok_or(super::valid::ValidationError::UnknownID)
.unwrap();
compiler.add_resource(id, schema.to_owned()).unwrap();
}

Ok(compiler)
compiler
}

/// from_strings returns a new boon::Compiler with the schemas contained in
/// `strings` and configured to validate `path` and `license` formats.
pub fn from_strings(strings: &[&str]) -> Result<Compiler, Box<dyn Error>> {
pub fn for_test<P: AsRef<Path>>(dir: P) -> Result<Compiler, Box<dyn Error>> {
let mut compiler = spec_compiler();
for str in strings {
let schema: Value = serde_json::from_str(str)?;
let glob = Glob::new("**/*.schema.json")?;
for path in glob.walk(dir) {
let schema: Value = serde_json::from_reader(File::open(path?.into_path())?)?;
let id = &schema["$id"]
.as_str()
.ok_or(super::valid::ValidationError::UnknownID)?;
compiler.add_resource(id, schema.to_owned())?;
}

Ok(compiler)
}

Expand Down Expand Up @@ -86,6 +88,7 @@ mod tests {
use super::*;
use boon::Schemas;
use serde_json::json;
use std::{fs::File, path::Path};

#[test]
fn test_path() {
Expand Down Expand Up @@ -166,10 +169,8 @@ mod tests {
}

#[test]
fn test_from_strings() -> Result<(), Box<dyn Error>> {
let schema_v1 = include_str!(concat!(env!("OUT_DIR"), "/pgxn-meta-v1.schema.json"));
let schema_v2 = include_str!(concat!(env!("OUT_DIR"), "/pgxn-meta-v2.schema.json"));
let mut compiler = from_strings(&[schema_v1, schema_v2])?;
fn test_new() -> Result<(), Box<dyn Error>> {
let mut compiler = new();

for tc in [("v1", "widget.json"), ("v2", "typical-sql.json")] {
let mut schemas = Schemas::new();
Expand Down
4 changes: 1 addition & 3 deletions src/valid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ impl Validator {
/// new creates and returns a new Validator with the schemas loaded from
/// `dir`.
pub fn new() -> Validator {
let schema_v1 = include_str!(concat!(env!("OUT_DIR"), "/pgxn-meta-v1.schema.json"));
let schema_v2 = include_str!(concat!(env!("OUT_DIR"), "/pgxn-meta-v2.schema.json"));
Validator {
compiler: super::compiler::from_strings(&[schema_v1, schema_v2]).unwrap(),
compiler: super::compiler::new(),
schemas: Schemas::new(),
}
}
Expand Down
34 changes: 17 additions & 17 deletions tests/v1_schema_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ fn test_schema_v1() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_term() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the term schema.
let compiler = compiler::new("schema/v1")?;
let compiler = compiler::for_test("schema/v1")?;
test_term_schema(compiler, SCHEMA_VERSION)
}

#[test]
fn test_v1_tags() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the tags schema.
let compiler = compiler::new("schema/v1")?;
let compiler = compiler::for_test("schema/v1")?;
test_tags_schema(compiler, SCHEMA_VERSION)
}

#[test]
fn test_v1_version() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the version schema.
let mut compiler = compiler::new("schema/v1")?;
let mut compiler = compiler::for_test("schema/v1")?;
let mut schemas = Schemas::new();
let id = id_for(SCHEMA_VERSION, "version");
let idx = compiler.compile(&id, &mut schemas)?;
Expand All @@ -57,7 +57,7 @@ fn test_v1_version() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_version_range() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the version_range schema.
let mut compiler = compiler::new("schema/v1")?;
let mut compiler = compiler::for_test("schema/v1")?;
let mut schemas = Schemas::new();
let id = id_for(SCHEMA_VERSION, "version_range");
let idx = compiler.compile(&id, &mut schemas)?;
Expand Down Expand Up @@ -128,7 +128,7 @@ fn test_v1_version_range() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_license() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the license schema.
let mut compiler = compiler::new("schema/v1")?;
let mut compiler = compiler::for_test("schema/v1")?;
let mut schemas = Schemas::new();
let id = id_for(SCHEMA_VERSION, "license");
let idx = compiler.compile(&id, &mut schemas)?;
Expand Down Expand Up @@ -197,7 +197,7 @@ fn test_v1_license() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_provides() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the provides schema.
let mut compiler = compiler::new("schema/v1")?;
let mut compiler = compiler::for_test("schema/v1")?;
let mut schemas = Schemas::new();
let id = id_for(SCHEMA_VERSION, "provides");
let idx = compiler.compile(&id, &mut schemas)?;
Expand Down Expand Up @@ -296,7 +296,7 @@ fn test_v1_provides() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_extension() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the extension schema.
let mut compiler = compiler::new("schema/v1")?;
let mut compiler = compiler::for_test("schema/v1")?;
let mut schemas = Schemas::new();
let id = id_for(SCHEMA_VERSION, "extension");
let idx = compiler.compile(&id, &mut schemas)?;
Expand Down Expand Up @@ -472,7 +472,7 @@ fn test_v1_extension() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_maintainer() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the maintainer schema.
let mut compiler = compiler::new("schema/v1")?;
let mut compiler = compiler::for_test("schema/v1")?;
let mut schemas = Schemas::new();
let id = id_for(SCHEMA_VERSION, "maintainer");
let idx = compiler.compile(&id, &mut schemas)?;
Expand Down Expand Up @@ -522,7 +522,7 @@ fn test_v1_maintainer() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_meta_spec() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the maintainer schema.
let mut compiler = compiler::new("schema/v1")?;
let mut compiler = compiler::for_test("schema/v1")?;
let mut schemas = Schemas::new();
let id = id_for(SCHEMA_VERSION, "meta-spec");
let idx = compiler.compile(&id, &mut schemas)?;
Expand Down Expand Up @@ -576,7 +576,7 @@ fn test_v1_meta_spec() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_bugtracker() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the maintainer schema.
let mut compiler = compiler::new("schema/v1")?;
let mut compiler = compiler::for_test("schema/v1")?;
let mut schemas = Schemas::new();
let id = id_for(SCHEMA_VERSION, "bugtracker");
let idx = compiler.compile(&id, &mut schemas)?;
Expand Down Expand Up @@ -630,7 +630,7 @@ fn test_v1_bugtracker() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_no_index() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the maintainer schema.
let mut compiler = compiler::new("schema/v1")?;
let mut compiler = compiler::for_test("schema/v1")?;
let mut schemas = Schemas::new();
let id = id_for(SCHEMA_VERSION, "no_index");
let idx = compiler.compile(&id, &mut schemas)?;
Expand Down Expand Up @@ -697,7 +697,7 @@ fn test_v1_no_index() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_prereq_relationship() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the maintainer schema.
let mut compiler = compiler::new("schema/v1")?;
let mut compiler = compiler::for_test("schema/v1")?;
let mut schemas = Schemas::new();
let id = id_for(SCHEMA_VERSION, "prereq_relationship");
let idx = compiler.compile(&id, &mut schemas)?;
Expand Down Expand Up @@ -750,7 +750,7 @@ fn test_v1_prereq_relationship() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_prereq_phase() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the maintainer schema.
let mut compiler = compiler::new("schema/v1")?;
let mut compiler = compiler::for_test("schema/v1")?;
let mut schemas = Schemas::new();
let id = id_for(SCHEMA_VERSION, "prereq_phase");
let idx = compiler.compile(&id, &mut schemas)?;
Expand Down Expand Up @@ -862,7 +862,7 @@ fn test_v1_prereq_phase() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_prereqs() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the maintainer schema.
let mut compiler = compiler::new("schema/v1")?;
let mut compiler = compiler::for_test("schema/v1")?;
let mut schemas = Schemas::new();
let id = id_for(SCHEMA_VERSION, "prereqs");
let idx = compiler.compile(&id, &mut schemas)?;
Expand Down Expand Up @@ -1010,7 +1010,7 @@ fn test_v1_prereqs() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_repository() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the repository schema.
let mut compiler = compiler::new("schema/v1")?;
let mut compiler = compiler::for_test("schema/v1")?;
let mut schemas = Schemas::new();
let id = id_for(SCHEMA_VERSION, "repository");
let idx = compiler.compile(&id, &mut schemas)?;
Expand Down Expand Up @@ -1087,7 +1087,7 @@ fn test_v1_repository() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_resources() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the resources schema.
let mut compiler = compiler::new("schema/v1")?;
let mut compiler = compiler::for_test("schema/v1")?;
let mut schemas = Schemas::new();
let id = id_for(SCHEMA_VERSION, "resources");
let idx = compiler.compile(&id, &mut schemas)?;
Expand Down Expand Up @@ -1224,7 +1224,7 @@ fn valid_distribution() -> Value {
#[test]
fn test_v1_distribution() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the distribution schema.
let mut compiler = compiler::new("schema/v1")?;
let mut compiler = compiler::for_test("schema/v1")?;
let mut schemas = Schemas::new();
let id = id_for(SCHEMA_VERSION, "distribution");
let idx = compiler.compile(&id, &mut schemas)?;
Expand Down
Loading

0 comments on commit 490f4f3

Please sign in to comment.