我计划有一个通过 trait 方法提供 JSON 模式的结构。模式存储在一个惰性静态变量中,但我的schema()
函数必须返回哪种类型?
lazy_static::lazy_static! {
static ref DEF: serde_json::Value = serde_json::json!({
"type": "object",
"properties": {
"name": { "type": "string",
"minLength": 10,
},
},
});
static ref SCHEMA: jsonschema::JSONSchema<'static> = jsonschema::JSONSchema::compile(&DEF).unwrap();
}
struct MySchema {
name: String,
}
impl MySchema {
pub fn schema() -> jsonschema::JSONSchema<'static> {
SCHEMA
}
}
#[test]
pub fn test() {
let test = serde_json::json!({"name":"test"});
assert_eq!(SCHEMA.is_valid(&test), false);
assert_eq!(MySchema::schema().is_valid(&test), false);
}
我会收到这个错误
pub fn schema() -> jsonschema::JSONSchema<'static> {
------------------------------- expected `JSONSchema<'static>` because of return type
SCHEMA
^^^^^^ expected struct `JSONSchema`, found struct `SCHEMA`