Skip to main content

mentat/
lib.rs

1// Copyright 2016-2018 Mozilla
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4// this file except in compliance with the License. You may obtain a copy of the
5// License at http://www.apache.org/licenses/LICENSE-2.0
6// Unless required by applicable law or agreed to in writing, software distributed
7// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8// CONDITIONS OF ANY KIND, either express or implied. See the License for the
9// specific language governing permissions and limitations under the License.
10
11#[macro_use]
12extern crate lazy_static;
13
14pub use edn;
15
16extern crate core_traits;
17
18pub use core_traits::{
19    now, Attribute, Binding, Entid, KnownEntid, StructuredMap, TypedValue, ValueType,
20};
21
22pub use mentat_core::{DateTime, HasSchema, Keyword, Schema, TxReport, Utc, Uuid};
23
24pub use edn::query::FindSpec;
25
26pub use mentat_db::{
27    new_connection, AttributeSet, TxObserver, CORE_SCHEMA_VERSION, DB_SCHEMA_CORE,
28};
29
30#[cfg(feature = "sqlcipher")]
31pub use mentat_db::{change_encryption_key, new_connection_with_key};
32
33/// Produce the appropriate `Variable` for the provided valid ?-prefixed name.
34/// This lives here because we can't re-export macros:
35/// <https://github.com/rust-lang/rust/issues/29638>.
36#[macro_export]
37macro_rules! var {
38    ( ? $var:ident ) => {
39        $crate::Variable::from_valid_name(concat!("?", stringify!($var)))
40    };
41}
42
43/// Produce the appropriate `Keyword` for the provided namespace and name.
44/// This lives here because we can't re-export macros:
45/// <https://github.com/rust-lang/rust/issues/29638>.
46#[macro_export]
47macro_rules! kw {
48    ( : $ns:ident$(. $nss:ident)+ / $nn:ident$(. $nns:ident)+ ) => {
49        $crate::Keyword::namespaced(
50            concat!(stringify!($ns) $(, ".", stringify!($nss))*),
51            concat!(stringify!($nn) $(, ".", stringify!($nns))*),
52        )
53    };
54
55    ( : $ns:ident$(. $nss:ident)+ / $nn:ident ) => {
56        $crate::Keyword::namespaced(
57            concat!(stringify!($ns) $(, ".", stringify!($nss))*),
58            stringify!($nn)
59        )
60    };
61
62    ( : $ns:ident / $nn:ident$(. $nns:ident)+ ) => {
63        $crate::Keyword::namespaced(
64            stringify!($ns),
65            concat!(stringify!($nn) $(, ".", stringify!($nns))*),
66        )
67    };
68
69    ( : $ns:ident / $nn:ident ) => {
70        $crate::Keyword::namespaced(
71            stringify!($ns),
72            stringify!($nn)
73        )
74    };
75
76    ( : $n:ident ) => {
77        $crate::Keyword::plain(
78            stringify!($n)
79        )
80    };
81}
82
83pub use public_traits::errors;
84pub use public_traits::errors::{MentatError, Result};
85
86pub use edn::{FromMicros, FromMillis, ParseError, ToMicros, ToMillis};
87pub use mentat_query_projector::BindingTuple;
88pub use query_algebrizer_traits::errors::AlgebrizerError;
89pub use query_projector_traits::errors::ProjectorError;
90pub use query_pull_traits::errors::PullError;
91pub use sql_traits::errors::SQLError;
92
93pub use mentat_transaction::Metadata;
94
95pub use mentat_query_algebrizer::TemporalBound;
96
97pub use crate::auto_index::{AutoIndex, IndexAction};
98pub use crate::options::options_from_json;
99
100pub use mentat_transaction::entity_builder;
101pub use mentat_transaction::query;
102
103pub use mentat_transaction::query::{
104    q_once, IntoResult, PlainSymbol, QueryExecutionResult, QueryExplanation, QueryInputs,
105    QueryOutput, QueryPlanStep, QueryResults, RelResult, Variable,
106};
107
108/// Return early with an error, converting via `From`. Replaces `failure::bail!`.
109macro_rules! bail {
110    ($e:expr) => {
111        return ::std::result::Result::Err(::std::convert::From::from($e))
112    };
113}
114
115pub mod auto_index;
116pub mod conn;
117pub mod options;
118pub mod query_builder;
119pub mod store;
120pub mod vocabulary;
121
122#[cfg(feature = "mino")]
123pub mod script;
124
125pub use query_builder::QueryBuilder;
126
127pub use conn::Conn;
128
129pub use mentat_transaction::{CacheAction, CacheDirection, InProgress, Pullable, Queryable};
130
131pub use store::Store;
132
133#[cfg(feature = "mino")]
134pub use script::Interpreter as ScriptInterpreter;
135
136#[cfg(test)]
137mod tests {
138    use super::*;
139    use edn::symbols::Keyword;
140
141    #[test]
142    fn can_import_edn() {
143        assert_eq!(":foo", &Keyword::plain("foo").to_string());
144    }
145
146    #[test]
147    fn test_kw() {
148        assert_eq!(kw!(:foo/bar), Keyword::namespaced("foo", "bar"));
149        assert_eq!(
150            kw!(:org.mozilla.foo/bar_baz),
151            Keyword::namespaced("org.mozilla.foo", "bar_baz")
152        );
153        assert_eq!(
154            kw!(:_foo_/_bar_._baz_),
155            Keyword::namespaced("_foo_", "_bar_._baz_")
156        );
157        assert_eq!(
158            kw!(:_org_._mozilla_._foo_/_bar_._baz_),
159            Keyword::namespaced("_org_._mozilla_._foo_", "_bar_._baz_")
160        );
161    }
162
163    #[test]
164    fn test_var() {
165        let foo_baz = var!(?foo_baz);
166        let vu = var!(?vü);
167        assert_eq!(foo_baz, Variable::from_valid_name("?foo_baz"));
168        assert_eq!(vu, Variable::from_valid_name("?vü"));
169        assert_eq!(foo_baz.as_str(), "?foo_baz");
170    }
171}