feature_probe_server_sdk/
lib.rs

1mod config;
2mod evaluate;
3mod feature_probe;
4mod sync;
5mod user;
6
7pub use crate::config::FPConfig;
8pub use crate::evaluate::{load_json, EvalDetail, Repository, Segment, Toggle};
9pub use crate::feature_probe::FeatureProbe;
10pub use crate::sync::SyncType;
11pub use crate::user::FPUser;
12use headers::{Error, Header, HeaderName, HeaderValue};
13use http::header::AUTHORIZATION;
14use lazy_static::lazy_static;
15use serde::{Deserialize, Serialize};
16use std::fmt::Debug;
17use thiserror::Error;
18pub use url::Url;
19
20lazy_static! {
21    pub(crate) static ref USER_AGENT: String = "Rust/".to_owned() + VERSION;
22}
23
24const VERSION: &str = env!("CARGO_PKG_VERSION");
25
26#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Default)]
27#[serde(rename_all = "camelCase")]
28pub struct FPDetail<T: Default + Debug> {
29    pub value: T,
30    pub rule_index: Option<usize>,
31    pub variation_index: Option<usize>,
32    pub version: Option<u64>,
33    pub reason: String,
34}
35
36#[non_exhaustive]
37#[derive(Debug, Error)]
38pub enum FPError {
39    #[error("invalid json: {0} error: {1}")]
40    JsonError(String, serde_json::Error),
41    #[error("invalid url: {0}")]
42    UrlError(String),
43    #[error("http error: {0}")]
44    HttpError(String),
45    #[error("evaluation error")]
46    EvalError,
47    #[error("evaluation error: {0}")]
48    EvalDetailError(String),
49    #[error("internal error: {0}")]
50    InternalError(String),
51}
52
53#[derive(Debug, Error)]
54enum PrerequisiteError {
55    #[error("prerequisite depth overflow")]
56    DepthOverflow,
57    #[error("prerequisite not exist: {0}")]
58    NotExist(String),
59}
60
61#[derive(Debug, Deserialize)]
62pub struct SdkAuthorization(pub String);
63
64impl SdkAuthorization {
65    pub fn encode(&self) -> HeaderValue {
66        HeaderValue::from_str(&self.0).expect("valid header value")
67    }
68}
69
70impl Header for SdkAuthorization {
71    fn name() -> &'static HeaderName {
72        &AUTHORIZATION
73    }
74
75    fn decode<'i, I>(values: &mut I) -> Result<Self, Error>
76    where
77        Self: Sized,
78        I: Iterator<Item = &'i HeaderValue>,
79    {
80        match values.next() {
81            Some(v) => match v.to_str() {
82                Ok(s) => Ok(SdkAuthorization(s.to_owned())),
83                Err(_) => Err(Error::invalid()),
84            },
85            None => Err(Error::invalid()),
86        }
87    }
88
89    fn encode<E: Extend<HeaderValue>>(&self, values: &mut E) {
90        if let Ok(value) = HeaderValue::from_str(&self.0) {
91            values.extend(std::iter::once(value))
92        }
93    }
94}
95
96pub fn unix_timestamp() -> u128 {
97    std::time::SystemTime::now()
98        .duration_since(std::time::UNIX_EPOCH)
99        .expect("Time went backwards!")
100        .as_millis()
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106
107    #[test]
108    #[should_panic]
109    fn test_encode_panic() {
110        let v: Vec<u8> = vec![21, 20, 19, 18]; // not visible string
111        let s = String::from_utf8(v).unwrap();
112        let auth = SdkAuthorization(s);
113        let _ = auth.encode();
114    }
115}