Skip to main content

nerva/
core.rs

1use core::fmt::Debug as DebugImpl;
2use serde::{Deserialize, Serialize};
3use std::error::Error;
4
5/// API Parameters for all clients
6pub trait Params
7where
8    Self: Into<String> + Clone + Copy + DebugImpl + PartialEq + Default,
9{
10}
11
12/// Interface behavior for all clients
13pub trait Client<P>
14where
15    P: Params,
16    Self: Default,
17{
18    /// The base URL endpoint
19    const BASE_URL: &'static str;
20    /// The response type
21    type Response: for<'de> Deserialize<'de> + Serialize + Clone + DebugImpl + PartialEq;
22
23    /// Get the response from the API
24    fn get(&self, params: P) -> Result<Self::Response, Box<dyn Error>>;
25}
26
27#[cfg(test)]
28mod tests
29{
30    use super::*;
31    use crate::clients::apod::{Apod, ApodParams};
32
33    #[test]
34    fn test_apod()
35    {
36        let apod = Apod::default();
37        let res = apod.get(ApodParams::default());
38        match res
39        {
40            Ok(json) => println!("{:#?}", json),
41            Err(e) =>
42            {
43                println!("{:#?}", e);
44                panic!();
45            }
46        }
47    }
48}