Skip to content

Commit

Permalink
take Value as value
Browse files Browse the repository at this point in the history
  • Loading branch information
hudson-ai committed Nov 19, 2024
1 parent d822980 commit a3a1847
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion parser/src/earley/from_guidance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn grammar_from_json(
"cannot have both json_schema/lark_grammar and nodes/rx_nodes"
);

let mut new_grm = if let Some(json_schema) = input.json_schema.as_ref() {
let mut new_grm = if let Some(json_schema) = input.json_schema.take() {
ensure!(
input.lark_grammar.is_none(),
"cannot have both json_schema and lark_grammar"
Expand Down
2 changes: 1 addition & 1 deletion parser/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ fn new_constraint_json(init: &LlgConstraintInit, json_schema: *const c_char) ->
.map_err(|e| anyhow::anyhow!("Invalid JSON in json_schema: {e}"))?;
let opts = JsonCompileOptions::default();
let grammar = opts
.json_to_llg(&json_schema)
.json_to_llg(json_schema)
.map_err(|e| anyhow::anyhow!("Error compiling JSON schema to LLG: {e}"))?;
init.build_constraint(grammar)
}
Expand Down
8 changes: 4 additions & 4 deletions parser/src/json/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,19 @@ impl Default for JsonCompileOptions {
}

impl JsonCompileOptions {
pub fn json_to_llg(&self, schema: &Value) -> Result<TopLevelGrammar> {
pub fn json_to_llg(&self, schema: Value) -> Result<TopLevelGrammar> {
let mut compiler = Compiler::new(self.clone());
#[cfg(feature = "jsonschema_validation")]
{
use crate::json_validation::validate_schema;
validate_schema(schema)?;
validate_schema(&schema)?;
}

compiler.execute(schema)?;
compiler.builder.finalize()
}

pub fn json_to_llg_no_validate(&self, schema: &Value) -> Result<TopLevelGrammar> {
pub fn json_to_llg_no_validate(&self, schema: Value) -> Result<TopLevelGrammar> {
let mut compiler = Compiler::new(self.clone());
compiler.execute(schema)?;
compiler.builder.finalize()
Expand All @@ -138,7 +138,7 @@ impl Compiler {
}
}

pub fn execute(&mut self, schema: &Value) -> Result<()> {
pub fn execute(&mut self, schema: Value) -> Result<()> {
self.builder.add_grammar(GrammarWithLexer {
greedy_skip_rx: if self.options.whitespace_flexible {
Some(mk_regex(r"[\x20\x0A\x0D\x09]+"))
Expand Down
10 changes: 5 additions & 5 deletions parser/src/json/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ fn instance_if_valid<'a>(instance: &'a Value, validator: &'a Validator) -> Optio
}
}

pub fn build_schema(contents: &Value) -> Result<(Schema, HashMap<String, Schema>)> {
pub fn build_schema(contents: Value) -> Result<(Schema, HashMap<String, Schema>)> {
if let Some(b) = contents.as_bool() {
if b {
return Ok((Schema::Any, HashMap::new()));
Expand All @@ -332,9 +332,8 @@ pub fn build_schema(contents: &Value) -> Result<(Schema, HashMap<String, Schema>
}
}

let draft = draft_for(contents);
let resource_ref = draft.create_resource_ref(contents);
let resource = draft.create_resource(contents.clone());
let draft = draft_for(&contents);
let resource = draft.create_resource(contents);
let base_uri = resource.id().unwrap_or(DEFAULT_ROOT_URI).to_string();

let registry = Registry::try_new(&base_uri, resource)?;
Expand All @@ -347,7 +346,8 @@ pub fn build_schema(contents: &Value) -> Result<(Schema, HashMap<String, Schema>
options: SchemaBuilderOptions::default(),
};

let schema = compile_resource(&ctx, resource_ref)?;
let root_resource = ctx.lookup_resource(&base_uri)?;
let schema = compile_resource(&ctx, root_resource)?;
let defs = std::mem::take(&mut ctx.shared.borrow_mut().defs);
Ok((schema, defs))
}
Expand Down
2 changes: 1 addition & 1 deletion rust/src/py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ impl JsonCompiler {
key_separator: self.key_separator.clone(),
whitespace_flexible: self.whitespace_flexible,
};
let tlg = compile_options.json_to_llg(&schema).map_err(val_error)?;
let tlg = compile_options.json_to_llg(schema).map_err(val_error)?;
let grammar = &tlg.grammars[0];
Ok(serde_json::to_string(grammar).map_err(val_error)?)
}
Expand Down
2 changes: 1 addition & 1 deletion sample_parser/src/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn main() {
} else if args[1].ends_with(".schema.json") {
let opts = JsonCompileOptions::default();
let val = serde_json::from_str(&schema_file).expect("Invalid JSON in schema");
opts.json_to_llg(&val)
opts.json_to_llg(val)
.expect("Failed to convert JSON to LLG")
} else {
panic!("Unknown schema file extension")
Expand Down
2 changes: 1 addition & 1 deletion sample_parser/src/sample_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() {
} else if args[1].ends_with(".schema.json") {
let opts = JsonCompileOptions::default();
let val = serde_json::from_str(&schema_file).expect("Invalid JSON in schema");
opts.json_to_llg(&val)
opts.json_to_llg(val)
.expect("Failed to convert JSON to LLG")
} else if args[1].ends_with(".lark") {
lark_to_llguidance(&schema_file).expect("Failed to convert lark to LLG")
Expand Down
2 changes: 1 addition & 1 deletion sample_parser/src/schema_tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn test_file(tok_env: TokEnv, file: &str) {
return;
}

let schema = opts.json_to_llg(&val);
let schema = opts.json_to_llg(val);

let schema = match schema {
Ok(schema) => schema,
Expand Down

0 comments on commit a3a1847

Please sign in to comment.