Skip to main content

teksilo_telemetry/
ext.rs

1// SPDX-License-Identifier: MPL-2.0
2// SPDX-FileCopyrightText: 2026 FernTech
3
4//! Extension traits exposing telemetry services on `BuildContext` /
5//! `EventContext`.
6//!
7//! Same convention as
8//! [`teksilo_settings::SettingsExt`]. Apps
9//! `use teksilo_telemetry::TelemetryExt;` to reach `ctx.consent()`,
10//! `ctx.usage_reporter()`, etc.
11
12use teksilo_core::BuildContext;
13use teksilo_core::widget::EventContext;
14
15use crate::bundle::OpenedTelemetry;
16use crate::consent::ConsentStore;
17use crate::dynamic_reporter::DynamicReporter;
18
19/// Convenience accessors for telemetry services attached to the app's
20/// `app_state` registry.
21pub trait TelemetryExt {
22    /// The full bundle. Panics if `TeksiloAppBuilder::telemetry(...)`
23    /// was not called.
24    fn telemetry(&self) -> &OpenedTelemetry {
25        self.try_telemetry().unwrap_or_else(|| {
26            panic!(
27                "TelemetryExt::telemetry(): no OpenedTelemetry registered. \
28                 Call TeksiloAppBuilder::telemetry(TelemetryBundle::new(...)) at startup."
29            )
30        })
31    }
32
33    /// The consent store. Panics if no telemetry bundle was registered.
34    fn consent(&self) -> &ConsentStore {
35        &self.telemetry().consent
36    }
37
38    /// The active dynamic reporter (forwards to the adapter
39    /// matching the current mode). Panics if no telemetry bundle was
40    /// registered.
41    fn usage_reporter(&self) -> &DynamicReporter {
42        &self.telemetry().reporter
43    }
44
45    fn try_telemetry(&self) -> Option<&OpenedTelemetry>;
46}
47
48impl<'a> TelemetryExt for BuildContext<'a> {
49    fn try_telemetry(&self) -> Option<&OpenedTelemetry> {
50        self.app_state::<OpenedTelemetry>()
51    }
52}
53
54impl<'a> TelemetryExt for EventContext<'a> {
55    fn try_telemetry(&self) -> Option<&OpenedTelemetry> {
56        self.app_state::<OpenedTelemetry>()
57    }
58}