1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//! Provides a way to construct [`DocumentAuthority`].
//!
//! A [`DocumentAuthority`] is responsible for resolving a particular class of
//! schema identifiers as per *[Ion Schema spec]*.
//!
//! There are two types of a [`DocumentAuthority`] as defined below.
//! * [`FileSystemDocumentAuthority`] : Attempts to resolve schema ids to files relative to a basePath.
//! * [`MapDocumentAuthority`] : Attempts to resolve schema ids to ion elements using the map of (id, ion content).
//!
//! [Ion Schema spec]: https://amazon-ion.github.io/ion-schema/docs/isl-1-0/spec#schema-authorities
//!
//! ## Example usage of `authority` module to create a [`DocumentAuthority`]:
//! ```
//! use ion_schema::authority::{MapDocumentAuthority, FileSystemDocumentAuthority};
//! use std::path::Path;
//!
//! // map with (id, ion content) to represent `sample_number` schema
//! let map_authority = [
//! (
//! "sample_number.isl",
//! r#"
//! schema_header::{
//! imports: [{ id: "sample_decimal.isl", type: my_decimal, as: other_decimal }],
//! }
//! type::{
//! name: my_int,
//! type: int,
//! }
//! type::{
//! name: my_number,
//! all_of: [
//! my_int,
//! other_decimal,
//! ],
//! }
//! schema_footer::{
//! }
//! "#,
//! ),
//! (
//! "sample_decimal.isl",
//! r#"
//! schema_header::{
//! imports: [],
//! }
//! type::{
//! name: my_decimal,
//! type: decimal,
//! }
//! schema_footer::{
//! }
//! "#,
//! ),
//! ];
//!
//! let map_document_authority = MapDocumentAuthority::new(map_authority);
//!
//! let file_system_document_authority = FileSystemDocumentAuthority::new(Path::new(
//! "sample_schemas",
//! ));
//! ```
use crate;
use Element;
use HashMap;
use Debug;
use fs;
use ;
/// An [`DocumentAuthority`] is responsible for resolving a particular class of
/// schema identifiers.
///
/// The structure of a schema identifier string is defined by the
/// Authority responsible for the schema/type(s) being imported.
///
/// [`DocumentAuthority`] is *[Send and Sync]* i.e. it is safe to send it to another thread and to be shared between threads.
/// For cases when one does need thread-safe interior mutability, they can use the explicit locking via [`std::sync::Mutex`] and [`std::sync::RwLock`].
///
/// [Send and Sync]: https://doc.rust-lang.org/nomicon/send-and-sync.html
/// An [`DocumentAuthority`] implementation that attempts to resolve schema ids to files
/// relative to a basePath.
/// An [`DocumentAuthority`] implementation that attempts to resolve schema ids to ion elements using the map.