1pub mod env;
30mod exec;
31mod go_err;
32pub mod go_flag;
33mod go_fmt;
34pub mod go_mime;
35mod go_path;
36mod go_time;
37pub mod go_url;
38mod json_kind;
39mod safe_subpath;
40mod time;
41mod tracking;
42mod uuid;
43
44pub use exec::{TaskRunner, TokioTaskRunner};
45pub use futures::future::BoxFuture;
46pub use go_err::{GoWrapped, go_errno_text};
47pub use go_fmt::{
48 go_format_float_g, go_format_float_v, go_query_escape, go_quote, go_quote_bytes,
49 go_sprint_value, go_to_lower, go_to_upper, go_type_name,
50};
51pub use go_path::{
52 go_filepath_is_local, go_filepath_rel, go_path_base, go_path_clean, go_path_dir, go_path_join,
53};
54pub use go_time::{
55 GoDuration, go_format_duration, go_format_rfc3339_nano, go_parse_duration, go_parse_rfc3339,
56};
57pub use json_kind::{json_expected_found, json_kind, json_kind_article, json_text_kind_article};
58pub use safe_subpath::{
59 ERR_NOT_VALID_RELATIVE_PATH, ERR_SYMLINK_IN_RELATIVE_PATH, SafeSubpathError,
60 SafeSubpathErrorKind, is_link_like, safe_subpath,
61};
62pub use time::TimeProvider;
63pub use tracking::tracking_header;
64pub use uuid::UuidProvider;
65
66use chrono::{DateTime, Duration, TimeZone, Utc};
67use std::sync::Arc;
68use std::sync::atomic::{AtomicU64, Ordering};
69
70#[derive(Clone)]
72pub struct Platform {
73 time: Arc<dyn TimeProvider>,
74 uuid: Arc<dyn UuidProvider>,
75 tasks: Arc<dyn TaskRunner>,
76}
77
78impl Default for Platform {
79 fn default() -> Self {
80 Self {
81 time: Arc::new(time::SystemTime),
82 uuid: Arc::new(uuid::RandomUuid),
83 tasks: Arc::new(TokioTaskRunner),
84 }
85 }
86}
87
88impl std::fmt::Debug for Platform {
89 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90 f.write_str("Platform")
91 }
92}
93
94impl Platform {
95 pub fn now(&self) -> DateTime<Utc> {
97 self.time.now()
98 }
99
100 pub fn new_uuid(&self) -> String {
102 self.uuid.new_uuid()
103 }
104
105 pub async fn run_tasks(&self, tasks: Vec<BoxFuture<'static, ()>>) {
108 if tasks.is_empty() {
109 return;
110 }
111 self.tasks.run(tasks).await;
112 }
113
114 pub fn with_time(mut self, p: impl TimeProvider + 'static) -> Self {
116 self.time = Arc::new(p);
117 self
118 }
119
120 pub fn with_uuid(mut self, p: impl UuidProvider + 'static) -> Self {
122 self.uuid = Arc::new(p);
123 self
124 }
125
126 pub fn with_task_runner(mut self, r: impl TaskRunner + 'static) -> Self {
128 self.tasks = Arc::new(r);
129 self
130 }
131
132 pub fn deterministic(seed: u64) -> Self {
135 let base = Utc
136 .with_ymd_and_hms(2000, 1, 1, 0, 0, 0)
137 .single()
138 .unwrap_or_default()
139 + Duration::seconds(i64::try_from(seed % 1_000_000_000).unwrap_or(0));
140 let ticks = Arc::new(AtomicU64::new(0));
141 let ids = Arc::new(AtomicU64::new(0));
142 Self::default()
143 .with_time(move || {
144 let ms = i64::try_from(ticks.fetch_add(1, Ordering::SeqCst)).unwrap_or(i64::MAX);
145 Duration::try_milliseconds(ms)
146 .and_then(|d| base.checked_add_signed(d))
147 .unwrap_or(DateTime::<Utc>::MAX_UTC)
148 })
149 .with_uuid(move || {
150 let n = ids.fetch_add(1, Ordering::SeqCst);
151 let mut b = [0u8; 16];
152 b[..8].copy_from_slice(&seed.to_be_bytes());
153 b[8..].copy_from_slice(&n.to_be_bytes());
154 ::uuid::Builder::from_random_bytes(b)
155 .into_uuid()
156 .to_string()
157 })
158 }
159}