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
//! This crate introduces 2 things:
//! The trait for tokens. (`GetToken`) and
//! Newtype over `String` for tokens. (`Token`)
//! 
//! # Example
//! ```
//! use token_manager::{Token};
//! let token = Token::new("token-some".into());
//! ```

use async_trait::async_trait;

/// New type over `String` for Token type.
/// # Using
/// ```
/// use token_manager::token::Token;
/// let token = Token::new(String::from("MY AWESOME TOKEN"));
/// assert_eq!(token.0, String::from("MY AWESOME TOKEN"));
/// ```
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Token(pub String);

impl Token {
    /// Create new token from string
    /// # Using
    /// ```
    /// use token_manager::token::Token;
    /// let token = Token::new("my-token".into());
    /// ```
    pub fn new(token: String) -> Self {
        Self(token)
    }
}

impl From<String> for Token {
    fn from(string: String) -> Self {
        Self::new(string)
    }
}

impl From<&str> for Token {
    fn from(string: &str) -> Self {
        Self::new(string.into())
    }
}

/// Trait that marks object that it has token.
/// It can be anything like: one token, pool of tokens, so on
#[async_trait]
pub trait GetToken {
    /// Get token.
    /// # Using
    /// ```ignore
    /// // `GetToken` implementor here..
    /// let token = many_tokens.get_token().await;
    /// ```
    async fn get_token(&self) -> &Token;
}