Debian Codegraph @

src/schemas/schema.rs

raw symbols SCIP
1 //!
2 //! Wrapping of the Schema (xmlSchema)
3 //!
4 use std::sync::OnceLock;
5
6 use super::SchemaParserContext;
7
8 use crate::bindings;
9
10 use crate::error::StructuredError;
11
12 static SCHEMA_TYPES_LOCK: OnceLock<bool> = OnceLock::new();
13
14 /// Wrapper on xmlSchema
15 pub struct Schema(*mut bindings::_xmlSchema);
16
17 impl Schema {
18 /// Create schema by having a SchemaParserContext do the actual parsing of the schema it was provided
19 pub fn from_parser(parser: &mut SchemaParserContext) -> Result<Self, Vec<StructuredError>> {
20
21 // `xmlSchemaParse` calls `xmlSchemaInitTypes`.
22 // `xmlSchemaInitTypes` is a lazy function which is only intended to be
23 // called once for optimization purposes - but libxml2 doesn't do this
24 // in a thread-safe manner. We wrap the call in a OnceLock so that it
25 // only ever needs to be invoked once - and will do it in a thread-safe
26 // way.
27 let _ = SCHEMA_TYPES_LOCK.get_or_init(|| {
28 unsafe { bindings::xmlSchemaInitTypes() };
29 true
30 });
31
32 let raw = unsafe { bindings::xmlSchemaParse(parser.as_ptr()) };
33
34 if raw.is_null() {
35 Err(parser.drain_errors())
36 } else {
37 Ok(Self(raw))
38 }
39 }
40
41 /// Return a raw pointer to the underlying xmlSchema structure
42 pub fn as_ptr(&self) -> *mut bindings::_xmlSchema {
43 self.0
44 }
45 }
46
47 impl Drop for Schema {
48 fn drop(&mut self) {
49 unsafe { bindings::xmlSchemaFree(self.0) }
50 }
51 }