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
//! # Tinify Crate
//!
//! `tinify-rs` is a Rust client for the Tinify API.
//! Used for TinyPNG and TinyJPG. Tinify compresses your images intelligently.
//!
//! Read more at `https://tinify.com`
// --snip--

mod error;
mod client;
mod source;

pub use crate::client::Client;
pub use crate::source::Source;
pub use crate::error::TinifyException;

#[derive(Debug)]
pub struct Tinify {
  pub key: String,
}

impl Tinify {
  /// Create a new Tinify Object.
  ///
  /// # Examples
  ///
  /// ```
  /// use tinify::Tinify;
  /// 
  /// fn main() {
  ///   let tinify = Tinify::new();
  /// }
  /// ```
  pub fn new() -> Self {
    Self {
      key: String::new(),
    }
  }
  
  /// Set a Tinify Key.
  ///
  /// # Examples
  ///
  /// ```
  /// use tinify::Tinify;
  /// 
  /// fn main() {
  ///   let key = "tinify api key";
  ///   let tinify = Tinify::new().set_key(key);
  /// }
  /// ```
  pub fn set_key(mut self, key: &str) -> Self {
    self.key = key.to_string();
    self
  }

  /// Get a new Tinify Client.
  ///
  /// # Examples
  ///
  /// ```
  /// use tinify::{Tinify, TinifyException};
  /// 
  /// fn main() -> Result<(), TinifyException> {
  ///   let key = "tinify api key";
  ///   let tinify = Tinify::new().set_key(key);
  ///   let client = tinify.get_client()?;
  ///   
  ///   Ok(())
  /// }
  /// ```
  pub fn get_client(&self) -> Result<Client, TinifyException> {
    if self.key.is_empty() {
      return Err(TinifyException::KeyException);
    }
    let client = Client::new(self.key.to_string());
  
    Ok(client)
  }
}

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

  #[test]
  #[should_panic(
    expected="Provide an API key with set_key(key) method"
  )]
  fn test_not_set_key() {
    let client = Tinify::new().get_client();
    if let Err(err) = client {
      panic!("{}", err.to_string());
    }
  }

  #[test]
  fn test_get_client() -> Result<(), TinifyException> {
    dotenv().ok();
    let key = match env::var("KEY") {
      Ok(key) => key,
      Err(_err) => panic!("No such file or directory."),
    };
    let _ = Tinify::new()
      .set_key(&key)
      .get_client()?;

    Ok(())
  }
}