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, ProxySession, Table, View};
21use py_err::PyPerspectiveError;
22use pyo3::prelude::*;
23use tracing_subscriber::layer::SubscriberExt;
24use tracing_subscriber::util::SubscriberInitExt;
25use tracing_subscriber::{fmt, EnvFilter};
26
27macro_rules! inherit_doc {
28    (#[inherit_doc = $y:literal] $x:item) => {
29        #[cfg_attr(feature = "external-docs", doc =
30                                include_str!(concat!(env!("PERSPECTIVE_CLIENT_DOCS_PATH"), $y)))]
31        $x
32    };
33}
34
35pub(crate) use inherit_doc;
36
37/// Create a tracing filter which mimics the default behavior of reading from
38/// env, customized to exclude timestamp.
39/// [`tracing` filter docs](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/layer/index.html#per-layer-filtering)
40fn init_tracing() {
41    let fmt_layer = fmt::layer().without_time().with_target(true);
42    let filter_layer = EnvFilter::try_from_default_env()
43        .or_else(|_| EnvFilter::try_new("info"))
44        .unwrap();
45
46    tracing_subscriber::registry()
47        .with(filter_layer)
48        .with(fmt_layer)
49        .init();
50}
51
52/// A Python module implemented in Rust.
53#[pymodule]
54fn perspective(py: Python, m: &Bound<PyModule>) -> PyResult<()> {
55    init_tracing();
56    m.add_class::<client::client_sync::Client>()?;
57    m.add_class::<server::PySyncServer>()?;
58    m.add_class::<server::PySyncSession>()?;
59    m.add_class::<client::client_sync::Table>()?;
60    m.add_class::<client::client_sync::View>()?;
61    m.add_class::<client::client_sync::ProxySession>()?;
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
66    m.add("PerspectiveError", py.get_type::<PyPerspectiveError>())?;
67
68    Ok(())
69}