janus_core/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2
3use std::future::Future;
4use tokio::task::JoinHandle;
5use url::Url;
6
7pub mod auth_tokens;
8pub mod cli;
9pub mod dp;
10pub mod hpke;
11pub mod http;
12pub mod report_id;
13pub mod retries;
14#[cfg(feature = "test-util")]
15#[cfg_attr(docsrs, doc(cfg(feature = "test-util")))]
16pub mod test_util;
17pub mod time;
18pub mod vdaf;
19
20/// This trait provides a mockable facade for [`tokio::task::spawn`].
21pub trait Runtime {
22    /// Spawn a future on a new task managed by an asynchronous runtime, and
23    /// return a handle that can be used to await completion of that task.
24    fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
25    where
26        F: Future + Send + 'static,
27        F::Output: Send + 'static;
28}
29
30/// This type implements [`Runtime`] by directly calling [`tokio::task::spawn`].
31pub struct TokioRuntime;
32
33impl Runtime for TokioRuntime {
34    fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
35    where
36        F: Future + Send + 'static,
37        F::Output: Send + 'static,
38    {
39        tokio::task::spawn(future)
40    }
41}
42
43pub mod taskprov {
44    pub const TASKPROV_HEADER: &str = "dap-taskprov";
45}
46
47/// Returns the given [`Url`], possibly modified to end with a slash.
48///
49/// Aggregator endpoint URLs should end with a slash if they will be used with [`Url::join`],
50/// because that method will drop the last path component of the base URL if it does not end with a
51/// slash.
52pub fn url_ensure_trailing_slash(mut url: Url) -> Url {
53    if !url.as_str().ends_with('/') {
54        url.set_path(&format!("{}/", url.path()));
55    }
56    url
57}