1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3#![allow(renamed_and_removed_lints)] #![allow(unknown_lints)] #![warn(missing_docs)]
7#![warn(noop_method_call)]
8#![warn(unreachable_pub)]
9#![warn(clippy::all)]
10#![deny(clippy::await_holding_lock)]
11#![deny(clippy::cargo_common_metadata)]
12#![deny(clippy::cast_lossless)]
13#![deny(clippy::checked_conversions)]
14#![warn(clippy::cognitive_complexity)]
15#![deny(clippy::debug_assert_with_mut_call)]
16#![deny(clippy::exhaustive_enums)]
17#![deny(clippy::exhaustive_structs)]
18#![deny(clippy::expl_impl_clone_on_copy)]
19#![deny(clippy::fallible_impl_from)]
20#![deny(clippy::implicit_clone)]
21#![deny(clippy::large_stack_arrays)]
22#![warn(clippy::manual_ok_or)]
23#![deny(clippy::missing_docs_in_private_items)]
24#![warn(clippy::needless_borrow)]
25#![warn(clippy::needless_pass_by_value)]
26#![warn(clippy::option_option)]
27#![deny(clippy::print_stderr)]
28#![deny(clippy::print_stdout)]
29#![warn(clippy::rc_buffer)]
30#![deny(clippy::ref_option_ref)]
31#![warn(clippy::semicolon_if_nothing_returned)]
32#![warn(clippy::trait_duplication_in_bounds)]
33#![deny(clippy::unchecked_time_subtraction)]
34#![deny(clippy::unnecessary_wraps)]
35#![warn(clippy::unseparated_literal_suffix)]
36#![deny(clippy::unwrap_used)]
37#![deny(clippy::mod_module_files)]
38#![allow(clippy::let_unit_value)] #![allow(clippy::uninlined_format_args)]
40#![allow(clippy::significant_drop_in_scrutinee)] #![allow(clippy::result_large_err)] #![allow(clippy::needless_raw_string_hashes)] #![allow(clippy::needless_lifetimes)] #![allow(mismatched_lifetime_syntaxes)] #![allow(clippy::collapsible_if)] #![deny(clippy::unused_async)]
47use std::time;
50use thiserror::Error;
51use web_time_compat::{SystemTime, SystemTimeExt};
52
53pub mod signed;
54pub mod timed;
55
56#[derive(Debug, Clone, Error, PartialEq, Eq)]
59#[non_exhaustive]
60pub enum TimeValidityError {
61 #[error("Object will not be valid for {}", humantime::format_duration(*.0))]
63 NotYetValid(time::Duration),
64 #[error("Object has been expired for {}", humantime::format_duration(*.0))]
66 Expired(time::Duration),
67 #[error("Object is not currently valid")]
69 Unspecified,
70}
71
72pub trait Timebound<T>: Sized {
78 type Error;
80
81 fn is_valid_at(&self, t: &time::SystemTime) -> Result<(), Self::Error>;
85
86 fn dangerously_assume_timely(self) -> T;
88
89 fn check_valid_at(self, t: &time::SystemTime) -> Result<T, Self::Error> {
91 self.is_valid_at(t)?;
92 Ok(self.dangerously_assume_timely())
93 }
94
95 fn check_valid_now(self) -> Result<T, Self::Error> {
97 self.check_valid_at(&SystemTime::get())
98 }
99
100 fn check_valid_at_opt(self, t: Option<time::SystemTime>) -> Result<T, Self::Error> {
103 match t {
104 Some(when) => self.check_valid_at(&when),
105 None => self.check_valid_now(),
106 }
107 }
108}
109
110pub trait SelfSigned<T>: Sized {
119 type Error;
121 fn is_well_signed(&self) -> Result<(), Self::Error>;
123 fn dangerously_assume_wellsigned(self) -> T;
125
126 fn check_signature(self) -> Result<T, Self::Error> {
128 self.is_well_signed()?;
129 Ok(self.dangerously_assume_wellsigned())
130 }
131}
132
133pub trait ExternallySigned<T>: Sized {
136 type Key: ?Sized;
141
142 type KeyHint;
144
145 type Error;
147
148 fn key_is_correct(&self, k: &Self::Key) -> Result<(), Self::KeyHint>;
154
155 fn is_well_signed(&self, k: &Self::Key) -> Result<(), Self::Error>;
157
158 fn dangerously_assume_wellsigned(self) -> T;
160
161 fn check_signature(self, k: &Self::Key) -> Result<T, Self::Error> {
163 self.is_well_signed(k)?;
164 Ok(self.dangerously_assume_wellsigned())
165 }
166}