Skip to content

Commit

Permalink
add llg_new_constraint_any
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoskal committed Nov 4, 2024
1 parent 075714b commit bbd8e3c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions parser/llguidance.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ struct LlgConstraint *llg_new_constraint_json(const struct LlgConstraintInit *in
struct LlgConstraint *llg_new_constraint_lark(const struct LlgConstraintInit *init,
const char *lark);

/**
* Create a new constraint with specified type
* Type can be one of "regex", "json_schema" (or "json"), "lark", "llguidance" (or "guidance")
* Always returns a non-null value. Call llg_get_error() on the result to check for errors.
*/
struct LlgConstraint *llg_new_constraint_any(const struct LlgConstraintInit *init,
const char *constraint_type,
const char *data);

/**
* Get the error message from the constraint or null if there is no error.
* After it returns a non-null value, it will always return it until the constraint is freed
Expand Down
29 changes: 29 additions & 0 deletions parser/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,23 @@ fn new_constraint(init: &LlgConstraintInit, grammar_json: *const c_char) -> Resu
new_constraint_core(init, grammar)
}

fn new_constraint_any(
init: &LlgConstraintInit,
constraint_type: *const c_char,
data: *const c_char,
) -> Result<Constraint> {
let tp = unsafe { CStr::from_ptr(constraint_type) }
.to_str()
.map_err(|_| anyhow::anyhow!("Invalid UTF-8 in constraint_type"))?;
match tp {
"regex" => new_constraint_regex(init, data),
"json" | "json_schema" => new_constraint_json(init, data),
"lark" => new_constraint_lark(init, data),
"llguidance" | "guidance" => new_constraint_lark(init, data),
_ => bail!("unknown constraint type: {tp}"),
}
}

fn new_constraint_core(init: &LlgConstraintInit, grammar: TopLevelGrammar) -> Result<Constraint> {
if init.tokenizer.is_null() {
bail!("Tokenizer is null");
Expand Down Expand Up @@ -419,6 +436,18 @@ pub extern "C" fn llg_new_constraint_lark(
return_constraint(new_constraint_lark(init, lark))
}

/// Create a new constraint with specified type
/// Type can be one of "regex", "json_schema" (or "json"), "lark", "llguidance" (or "guidance")
/// Always returns a non-null value. Call llg_get_error() on the result to check for errors.
#[no_mangle]
pub extern "C" fn llg_new_constraint_any(
init: &LlgConstraintInit,
constraint_type: *const c_char,
data: *const c_char,
) -> *mut LlgConstraint {
return_constraint(new_constraint_any(init, constraint_type, data))
}

/// Get the error message from the constraint or null if there is no error.
/// After it returns a non-null value, it will always return it until the constraint is freed
/// using llg_free_constraint() (at which point the pointer will be invalid).
Expand Down

0 comments on commit bbd8e3c

Please sign in to comment.