eeyf 0.1.0

Eric Evans' Yahoo Finance API - A rate-limited, reliable Rust adapter for Yahoo Finance API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Runtime abstraction layer for EEYF
//!
//! This module provides a runtime-agnostic interface that allows EEYF to work
//! with different async runtimes (tokio, async-std, smol) without code changes.
//!
//! # Features
//!
//! - `runtime-tokio` (default): Use Tokio runtime
//! - `runtime-async-std`: Use async-std runtime
//! - `runtime-smol`: Use smol runtime
//!
//! # Examples
//!
//! ```no_run
//! use std::time::Duration;
//!
//! use eeyf::runtime::{Runtime, sleep, spawn};
//!
//! async fn example() {
//!     // Spawn a task (works with any runtime)
//!     let handle = spawn(async {
//!         println!("Running on current runtime");
//!     });
//!
//!     // Sleep (works with any runtime)
//!     sleep(Duration::from_secs(1)).await;
//!
//!     // Wait for task
//!     handle.await.ok();
//! }
//! ```

use std::{future::Future, time::Duration};

/// Runtime abstraction trait
///
/// Provides a uniform interface for different async runtimes
pub trait Runtime: Send + Sync + 'static {
    /// Task join handle type
    type JoinHandle<T>: Future<Output = Result<T, JoinError>> + Send
    where
        T: Send + 'static;

    /// Spawn a task on this runtime
    fn spawn<F, T>(&self, future: F) -> Self::JoinHandle<T>
    where
        F: Future<Output = T> + Send + 'static,
        T: Send + 'static;

    /// Spawn a blocking task
    fn spawn_blocking<F, T>(&self, f: F) -> Self::JoinHandle<T>
    where
        F: FnOnce() -> T + Send + 'static,
        T: Send + 'static;

    /// Sleep for a duration
    fn sleep(&self, duration: Duration) -> impl Future<Output = ()> + Send;

    /// Get current runtime name
    fn name(&self) -> &'static str;

    /// Check if runtime is available
    fn is_available() -> bool;
}

/// Join error type
#[derive(Debug)]
pub enum JoinError {
    /// Task was cancelled
    Cancelled,
    /// Task panicked
    Panic(Box<dyn std::any::Any + Send>),
    /// Runtime-specific error
    Runtime(String),
}

impl std::fmt::Display for JoinError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Cancelled => write!(f, "Task was cancelled"),
            Self::Panic(_) => write!(f, "Task panicked"),
            Self::Runtime(msg) => write!(f, "Runtime error: {}", msg),
        }
    }
}

impl std::error::Error for JoinError {}

/// Spawn a task on the current runtime
pub fn spawn<F, T>(future: F) -> impl Future<Output = Result<T, JoinError>> + Send
where
    F: Future<Output = T> + Send + 'static,
    T: Send + 'static,
{
    #[cfg(feature = "runtime-tokio")]
    {
        tokio_runtime::TokioRuntime.spawn(future)
    }

    #[cfg(all(feature = "runtime-async-std", not(feature = "runtime-tokio")))]
    {
        async_std_runtime::AsyncStdRuntime.spawn(future)
    }

    #[cfg(all(
        feature = "runtime-smol",
        not(feature = "runtime-tokio"),
        not(feature = "runtime-async-std")
    ))]
    {
        smol_runtime::SmolRuntime.spawn(future)
    }
}

/// Spawn a blocking task on the current runtime
pub fn spawn_blocking<F, T>(f: F) -> impl Future<Output = Result<T, JoinError>> + Send
where
    F: FnOnce() -> T + Send + 'static,
    T: Send + 'static,
{
    #[cfg(feature = "runtime-tokio")]
    {
        tokio_runtime::TokioRuntime.spawn_blocking(f)
    }

    #[cfg(all(feature = "runtime-async-std", not(feature = "runtime-tokio")))]
    {
        async_std_runtime::AsyncStdRuntime.spawn_blocking(f)
    }

    #[cfg(all(
        feature = "runtime-smol",
        not(feature = "runtime-tokio"),
        not(feature = "runtime-async-std")
    ))]
    {
        smol_runtime::SmolRuntime.spawn_blocking(f)
    }
}

/// Sleep for a duration on the current runtime
pub async fn sleep(duration: Duration) {
    #[cfg(feature = "runtime-tokio")]
    {
        tokio_runtime::TokioRuntime.sleep(duration).await
    }

    #[cfg(all(feature = "runtime-async-std", not(feature = "runtime-tokio")))]
    {
        async_std_runtime::AsyncStdRuntime.sleep(duration).await
    }

    #[cfg(all(
        feature = "runtime-smol",
        not(feature = "runtime-tokio"),
        not(feature = "runtime-async-std")
    ))]
    {
        smol_runtime::SmolRuntime.sleep(duration).await
    }
}

/// Get the name of the current runtime
pub fn runtime_name() -> &'static str {
    #[cfg(feature = "runtime-tokio")]
    {
        "tokio"
    }

    #[cfg(all(feature = "runtime-async-std", not(feature = "runtime-tokio")))]
    {
        "async-std"
    }

    #[cfg(all(
        feature = "runtime-smol",
        not(feature = "runtime-tokio"),
        not(feature = "runtime-async-std")
    ))]
    {
        "smol"
    }
}

// Tokio runtime adapter
#[cfg(feature = "runtime-tokio")]
pub mod tokio_runtime {
    use super::*;

    /// Tokio runtime adapter
    pub struct TokioRuntime;

    /// Tokio join handle wrapper
    pub struct TokioJoinHandle<T>(tokio::task::JoinHandle<T>);

    impl<T> Future for TokioJoinHandle<T>
    where
        T: Send + 'static,
    {
        type Output = Result<T, JoinError>;

        fn poll(
            mut self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<Self::Output> {
            use std::pin::Pin;
            match Pin::new(&mut self.0).poll(cx) {
                std::task::Poll::Ready(Ok(value)) => std::task::Poll::Ready(Ok(value)),
                std::task::Poll::Ready(Err(e)) if e.is_cancelled() => {
                    std::task::Poll::Ready(Err(JoinError::Cancelled))
                },
                std::task::Poll::Ready(Err(e)) if e.is_panic() => {
                    std::task::Poll::Ready(Err(JoinError::Panic(e.into_panic())))
                },
                std::task::Poll::Ready(Err(e)) => {
                    std::task::Poll::Ready(Err(JoinError::Runtime(e.to_string())))
                },
                std::task::Poll::Pending => std::task::Poll::Pending,
            }
        }
    }

    impl Runtime for TokioRuntime {
        type JoinHandle<T>
            = TokioJoinHandle<T>
        where
            T: Send + 'static;

        fn spawn<F, T>(&self, future: F) -> Self::JoinHandle<T>
        where
            F: Future<Output = T> + Send + 'static,
            T: Send + 'static,
        {
            TokioJoinHandle(tokio::task::spawn(future))
        }

        fn spawn_blocking<F, T>(&self, f: F) -> Self::JoinHandle<T>
        where
            F: FnOnce() -> T + Send + 'static,
            T: Send + 'static,
        {
            TokioJoinHandle(tokio::task::spawn_blocking(f))
        }

        async fn sleep(&self, duration: Duration) {
            tokio::time::sleep(duration).await
        }

        fn name(&self) -> &'static str {
            "tokio"
        }

        fn is_available() -> bool {
            tokio::runtime::Handle::try_current().is_ok()
        }
    }
}

// async-std runtime adapter
#[cfg(feature = "runtime-async-std")]
pub mod async_std_runtime {
    use super::*;

    /// async-std runtime adapter
    pub struct AsyncStdRuntime;

    /// async-std join handle wrapper
    pub struct AsyncStdJoinHandle<T>(async_std::task::JoinHandle<T>);

    impl<T> Future for AsyncStdJoinHandle<T>
    where
        T: Send + 'static,
    {
        type Output = Result<T, JoinError>;

        fn poll(
            mut self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<Self::Output> {
            use std::pin::Pin;
            match Pin::new(&mut self.0).poll(cx) {
                std::task::Poll::Ready(value) => std::task::Poll::Ready(Ok(value)),
                std::task::Poll::Pending => std::task::Poll::Pending,
            }
        }
    }

    impl Runtime for AsyncStdRuntime {
        type JoinHandle<T>
            = AsyncStdJoinHandle<T>
        where
            T: Send + 'static;

        fn spawn<F, T>(&self, future: F) -> Self::JoinHandle<T>
        where
            F: Future<Output = T> + Send + 'static,
            T: Send + 'static,
        {
            AsyncStdJoinHandle(async_std::task::spawn(future))
        }

        fn spawn_blocking<F, T>(&self, f: F) -> Self::JoinHandle<T>
        where
            F: FnOnce() -> T + Send + 'static,
            T: Send + 'static,
        {
            AsyncStdJoinHandle(async_std::task::spawn_blocking(f))
        }

        async fn sleep(&self, duration: Duration) {
            async_std::task::sleep(duration).await
        }

        fn name(&self) -> &'static str {
            "async-std"
        }

        fn is_available() -> bool {
            // async-std doesn't have a way to check runtime availability
            true
        }
    }
}

// smol runtime adapter
#[cfg(feature = "runtime-smol")]
pub mod smol_runtime {
    use super::*;

    /// smol runtime adapter
    pub struct SmolRuntime;

    /// smol join handle wrapper
    pub struct SmolJoinHandle<T>(smol::Task<T>);

    impl<T> Future for SmolJoinHandle<T>
    where
        T: Send + 'static,
    {
        type Output = Result<T, JoinError>;

        fn poll(
            mut self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<Self::Output> {
            use std::pin::Pin;
            match Pin::new(&mut self.0).poll(cx) {
                std::task::Poll::Ready(value) => std::task::Poll::Ready(Ok(value)),
                std::task::Poll::Pending => std::task::Poll::Pending,
            }
        }
    }

    impl Runtime for SmolRuntime {
        type JoinHandle<T>
            = SmolJoinHandle<T>
        where
            T: Send + 'static;

        fn spawn<F, T>(&self, future: F) -> Self::JoinHandle<T>
        where
            F: Future<Output = T> + Send + 'static,
            T: Send + 'static,
        {
            SmolJoinHandle(smol::spawn(future))
        }

        fn spawn_blocking<F, T>(&self, f: F) -> Self::JoinHandle<T>
        where
            F: FnOnce() -> T + Send + 'static,
            T: Send + 'static,
        {
            SmolJoinHandle(smol::unblock(f))
        }

        async fn sleep(&self, duration: Duration) {
            smol::Timer::after(duration).await
        }

        fn name(&self) -> &'static str {
            "smol"
        }

        fn is_available() -> bool {
            // smol doesn't have a way to check runtime availability
            true
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    #[cfg(feature = "runtime-tokio")]
    async fn test_tokio_runtime() {
        assert_eq!(runtime_name(), "tokio");

        let handle = spawn(async { 42 });
        let result = handle.await.unwrap();
        assert_eq!(result, 42);

        sleep(Duration::from_millis(10)).await;
    }

    #[async_std::test]
    #[cfg(feature = "runtime-async-std")]
    async fn test_async_std_runtime() {
        assert_eq!(runtime_name(), "async-std");

        let handle = spawn(async { 42 });
        let result = handle.await.unwrap();
        assert_eq!(result, 42);

        sleep(Duration::from_millis(10)).await;
    }

    #[test]
    #[cfg(feature = "runtime-smol")]
    fn test_smol_runtime() {
        smol::block_on(async {
            assert_eq!(runtime_name(), "smol");

            let handle = spawn(async { 42 });
            let result = handle.await.unwrap();
            assert_eq!(result, 42);

            sleep(Duration::from_millis(10)).await;
        });
    }

    #[tokio::test]
    #[cfg(feature = "runtime-tokio")]
    async fn test_spawn_blocking() {
        let handle = spawn_blocking(|| {
            std::thread::sleep(Duration::from_millis(10));
            42
        });
        let result = handle.await.unwrap();
        assert_eq!(result, 42);
    }
}