perspective_python/
lib.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13#![doc = include_str!("../docs/lib.md")]
14#![warn(unstable_features)]
15
16mod client;
17mod py_err;
18mod server;
19
20pub use client::client_sync::{Client, Table, View};
21pub use client::proxy_session::ProxySession;
22use py_err::PyPerspectiveError;
23use pyo3::prelude::*;
24use tracing_subscriber::layer::SubscriberExt;
25use tracing_subscriber::util::SubscriberInitExt;
26use tracing_subscriber::{EnvFilter, fmt};
27
28macro_rules! inherit_doc {
29    (#[inherit_doc = $y:literal] $x:item) => {
30        #[cfg_attr(feature = "external-docs", doc =
31                                include_str!(concat!(env!("PERSPECTIVE_CLIENT_DOCS_PATH"), $y)))]
32        $x
33    };
34}
35
36pub(crate) use inherit_doc;
37
38/// Create a tracing filter which mimics the default behavior of reading from
39/// env, customized to exclude timestamp.
40/// [`tracing` filter docs](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/layer/index.html#per-layer-filtering)
41fn init_tracing() {
42    let fmt_layer = fmt::layer().without_time().with_target(true);
43    let filter_layer = EnvFilter::try_from_default_env()
44        .or_else(|_| EnvFilter::try_new("info"))
45        .unwrap();
46
47    tracing_subscriber::registry()
48        .with(filter_layer)
49        .with(fmt_layer)
50        .init();
51}
52
53/// A Python module implemented in Rust.
54#[pymodule]
55fn perspective(py: Python, m: &Bound<PyModule>) -> PyResult<()> {
56    init_tracing();
57    m.add_class::<client::client_sync::Client>()?;
58    m.add_class::<server::PySyncServer>()?;
59    m.add_class::<server::PySyncSession>()?;
60    m.add_class::<client::client_sync::Table>()?;
61    m.add_class::<client::client_sync::View>()?;
62    m.add_class::<client::client_async::AsyncClient>()?;
63    m.add_class::<client::client_async::AsyncTable>()?;
64    m.add_class::<client::client_async::AsyncView>()?;
65    m.add_class::<client::proxy_session::ProxySession>()?;
66
67    m.add("PerspectiveError", py.get_type::<PyPerspectiveError>())?;
68
69    Ok(())
70}