#[derive(Debug, Clone)]
pub enum GrammarSpec {
JsonSchema(serde_json::Value),
Gbnf(String),
}
impl GrammarSpec {
pub fn json_schema_str(json: &str) -> Result<Self, serde_json::Error> {
let v = serde_json::from_str(json)?;
Ok(Self::JsonSchema(v))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn json_schema_str_roundtrip() {
let spec = GrammarSpec::json_schema_str(r#"{"type":"object"}"#).unwrap();
assert!(matches!(spec, GrammarSpec::JsonSchema(_)));
}
#[test]
fn json_schema_str_invalid() {
assert!(GrammarSpec::json_schema_str("not json").is_err());
}
#[test]
fn gbnf_variant() {
let spec = GrammarSpec::Gbnf("root ::= \"hello\"".to_string());
assert!(matches!(spec, GrammarSpec::Gbnf(_)));
}
#[test]
fn clone_and_debug() {
let spec = GrammarSpec::JsonSchema(serde_json::json!({"type": "string"}));
let cloned = spec.clone();
assert!(format!("{cloned:?}").contains("JsonSchema"));
}
}