thinkingdata_core/lib.rs
1// Copyright 2026 houseme
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Core implementation of the ThinkingData Analytics Rust SDK.
16//!
17//! This crate carries all functional logic: the wire-format [`data::Data`]
18//! model, input validation, diagnostics logging, and the three consumers
19//! (`log`, `batch`, `debug`) ported from the official ThinkingData Go/Lua
20//! SDKs. The [`thinkingdata`](https://crates.io/crates/thinkingdata) facade
21//! crate re-exports everything and adds a CLI.
22//!
23//! # Feature flags
24//!
25//! - `blocking` (default): the synchronous [`consumer::Consumer`] trait,
26//! [`TDAnalytics`], and blocking consumer methods.
27//! - `async`: the [`consumer::AsyncConsumer`] trait, [`TDAnalyticsAsync`], and
28//! `*_async` consumer methods. Internals are streaming pipelines built on
29//! Tokio (channel-fed writer/sender tasks).
30//!
31//! Both features may be enabled together; every consumer then implements both
32//! traits.
33//!
34//! # TLS
35//!
36//! HTTP transport uses `reqwest` 0.13 with its default rustls stack backed by
37//! the **aws-lc-rs** crypto provider only; no native-tls/openssl backend is
38//! supported or compiled in.
39//!
40//! # Quick start (blocking)
41//!
42//! ```no_run
43//! # #[cfg(feature = "blocking")]
44//! # fn quickstart() -> Result<(), thinkingdata_core::TDError> {
45//! # use thinkingdata_core::{TDAnalytics, consumer::LogConsumer};
46//!
47//! # let consumer = LogConsumer::new("/var/log/thinkingdata")?;
48//! # let ta = TDAnalytics::new(consumer)?;
49//!
50//! # ta.track("account_id_123", "purchase", serde_json::json!({
51//! # "product_name": "Rust SDK",
52//! # "price": 99.0,
53//! # }))?;
54//!
55//! # ta.close()?;
56//! # Ok(())
57//! # }
58//! ```
59//!
60//! # Quick start (async)
61//!
62//! ```no_run
63//! # #[cfg(feature = "async")]
64//! # async fn demo() -> Result<(), thinkingdata_core::TDError> {
65//! # use thinkingdata_core::{TDAnalyticsAsync, consumer::BatchConsumer};
66//!
67//! # let consumer = BatchConsumer::new("https://receiver.thinkingdata.io", "your_app_id")?;
68//! # let ta = TDAnalyticsAsync::new(consumer)?;
69//!
70//! # ta.track("account_id_123", "purchase", serde_json::json!({ "price": 99.0 })).await?;
71//!
72//! # ta.close().await?;
73//! # Ok(())
74//! # }
75//! ```
76
77pub mod analytics;
78pub mod consumer;
79pub mod data;
80pub mod error;
81pub mod log;
82pub mod utils;
83
84pub use data::{Data, DataType, IntoPropertyMap, LIB_NAME, PRESET_PROPERTIES, is_preset_property};
85pub use error::{Result, TDError};
86
87/// The version of the SDK, reported as `#lib_version` on every event.
88pub const SDK_VERSION: &str = env!("CARGO_PKG_VERSION");
89
90/// The blocking analytics facade. Available with the `blocking` feature.
91#[cfg(feature = "blocking")]
92pub use analytics::TDAnalytics;
93
94/// The asynchronous analytics facade. Available with the `async` feature.
95#[cfg(feature = "async")]
96pub use analytics::TDAnalyticsAsync;