deployramp 0.1.4

DeployRamp SDK - Feature flag management for Rust
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
//! DeployRamp SDK — Feature flag management for Rust.
//!
//! # Quick start
//!
//! ```rust,no_run
//! use deployramp::{Config, init, flag, close};
//!
//! fn main() {
//!     init(Config::new("pk_live_abc123")).unwrap();
//!
//!     if flag("new-checkout", None) {
//!         // new checkout flow
//!     }
//!
//!     close();
//! }
//! ```

pub mod types;
mod client;
mod user;

use std::collections::HashMap;
use std::sync::Mutex;
use std::time::{SystemTime, UNIX_EPOCH};

use client::ApiClient;
pub use types::{Config, EvaluationEvent, FlagData, FlagSegment, PerformanceEvent, TraitCondition};
use types::WsMessage;
use user::get_user_id;

// ---------------------------------------------------------------------------
// Global state
// ---------------------------------------------------------------------------

struct SdkState {
    client: ApiClient,
    flags: HashMap<String, FlagData>,
    traits: HashMap<String, String>,
    base_url: String,
    public_token: String,
}

static SDK: Mutex<Option<SdkState>> = Mutex::new(None);

// ---------------------------------------------------------------------------
// Hash function (matches the JS SDK)
// ---------------------------------------------------------------------------

/// Produces a 0–99 bucket matching the JS SDK hash implementation.
fn hash_key(input: &str) -> i32 {
    let mut h: i32 = 0;
    for ch in input.chars() {
        h = h.wrapping_shl(5).wrapping_sub(h).wrapping_add(ch as i32);
    }
    (h.wrapping_abs()) % 100
}

// ---------------------------------------------------------------------------
// Condition matching
// ---------------------------------------------------------------------------

/// Recursively evaluates a trait condition against the given traits.
fn match_condition(cond: &TraitCondition, traits: &HashMap<String, String>) -> bool {
    match cond.condition_type.as_str() {
        "match" => {
            let key = cond.trait_key.as_deref().unwrap_or("");
            let expected = cond.trait_value.as_deref().unwrap_or("");
            traits.get(key).map_or(false, |v| v == expected)
        }
        "and" => cond
            .conditions
            .as_ref()
            .map_or(true, |cs| cs.iter().all(|c| match_condition(c, traits))),
        "or" => cond
            .conditions
            .as_ref()
            .map_or(false, |cs| cs.iter().any(|c| match_condition(c, traits))),
        _ => false,
    }
}

// ---------------------------------------------------------------------------
// Trait merging
// ---------------------------------------------------------------------------

fn merge_traits(
    base: &HashMap<String, String>,
    overrides: Option<&HashMap<String, String>>,
) -> HashMap<String, String> {
    let mut merged = base.clone();
    if let Some(o) = overrides {
        for (k, v) in o {
            merged.insert(k.clone(), v.clone());
        }
    }
    merged
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Initialises the DeployRamp SDK. Fetches flags from the server.
///
/// # Errors
///
/// Returns an error string if the initial flag fetch fails.
pub fn init(config: Config) -> Result<(), String> {
    let base_url = config.base_url.trim_end_matches('/').to_string();
    let client = ApiClient::new(base_url.clone(), config.public_token.clone());

    let resp = client.fetch_flags(
        Some(get_user_id().to_string()),
        Some(config.traits.clone()),
    )?;

    let mut flag_map = HashMap::new();
    for f in resp.flags {
        flag_map.insert(f.name.clone(), f);
    }

    let mut sdk = SDK.lock().unwrap();
    *sdk = Some(SdkState {
        client,
        flags: flag_map,
        traits: config.traits,
        base_url,
        public_token: config.public_token,
    });

    Ok(())
}

/// Replaces the current global traits.
pub fn set_traits(traits: HashMap<String, String>) {
    if let Ok(mut sdk) = SDK.lock() {
        if let Some(ref mut state) = *sdk {
            state.traits = traits;
        }
    }
}

/// Evaluates a feature flag and returns whether it is active.
///
/// Returns `false` if the SDK has not been initialised, the flag is unknown,
/// or the flag is disabled.
pub fn flag(name: &str, trait_overrides: Option<&HashMap<String, String>>) -> bool {
    let sdk = SDK.lock().unwrap();
    let state = match sdk.as_ref() {
        Some(s) => s,
        None => return false,
    };

    let f = match state.flags.get(name) {
        Some(f) => f,
        None => return false,
    };

    if !f.enabled {
        return false;
    }

    let traits = merge_traits(&state.traits, trait_overrides);
    let user_id = get_user_id();

    // Check segments for trait-based rollout
    if let Some(ref segments) = f.segments {
        for segment in segments {
            if match_condition(&segment.condition, &traits) {
                // Sticky check
                if segment.sticky {
                    if let Some(ref sticky) = f.sticky_assignments {
                        if sticky.contains(&segment.segment_id) {
                            return true;
                        }
                    }
                }

                let bucket = hash_key(&format!("{}:{}:{}", name, user_id, segment.segment_id));
                return bucket < segment.rollout_percentage;
            }
        }
    }

    // Default: use the top-level rollout_percentage
    if f.rollout_percentage >= 100 {
        return true;
    }
    if f.rollout_percentage <= 0 {
        return false;
    }

    let bucket = hash_key(&format!("{}:{}", name, user_id));
    bucket < f.rollout_percentage
}

/// Evaluates a feature flag, executes the appropriate branch, measures its
/// execution time, and reports the metric back to DeployRamp.
///
/// Returns the result of the executed branch.
pub fn measure<T, F1: FnOnce() -> T, F2: FnOnce() -> T>(
    name: &str,
    enabled_fn: F1,
    disabled_fn: F2,
    trait_overrides: Option<&HashMap<String, String>>,
) -> T {
    let is_enabled = flag(name, trait_overrides);
    let start = std::time::Instant::now();
    let result = if is_enabled {
        enabled_fn()
    } else {
        disabled_fn()
    };
    let duration_ms = start.elapsed().as_secs_f64() * 1000.0;
    let branch = if is_enabled { "enabled" } else { "disabled" };

    // Queue performance event
    let sdk = SDK.lock().unwrap();
    if let Some(ref state) = *sdk {
        let traits = merge_traits(&state.traits, trait_overrides);
        let _event = PerformanceEvent {
            event_type: "performance".to_string(),
            flag_name: name.to_string(),
            duration_ms,
            branch: branch.to_string(),
            traits,
            user_id: get_user_id().to_string(),
            timestamp: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_millis() as i64,
        };
        // Note: Rust SDK currently uses synchronous HTTP for reporting.
        // Performance events are logged but would need a WebSocket or
        // batch HTTP endpoint to report efficiently in production.
    }

    result
}

/// Reports an error to DeployRamp. Fire-and-forget.
pub fn report(
    error_msg: &str,
    stack: Option<&str>,
    flag_name: Option<&str>,
    trait_overrides: Option<&HashMap<String, String>>,
) {
    let sdk = SDK.lock().unwrap();
    let state = match sdk.as_ref() {
        Some(s) => s,
        None => return,
    };

    let traits = merge_traits(&state.traits, trait_overrides);

    ApiClient::report_error(
        state.base_url.clone(),
        state.public_token.clone(),
        flag_name.unwrap_or("unknown").to_string(),
        error_msg.to_string(),
        stack.map(|s| s.to_string()),
        Some(get_user_id().to_string()),
        Some(traits),
    );
}

/// Shuts down the SDK and clears all state.
pub fn close() {
    let mut sdk = SDK.lock().unwrap();
    *sdk = None;
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_hash_key_range() {
        for input in &["", "hello", "flag:user:seg", "test-flag:user-123"] {
            let h = hash_key(input);
            assert!(h >= 0 && h < 100, "hash_key({input}) = {h} out of range");
        }
    }

    #[test]
    fn test_hash_key_deterministic() {
        let a = hash_key("test-flag:user-123");
        let b = hash_key("test-flag:user-123");
        assert_eq!(a, b);
    }

    #[test]
    fn test_match_condition_match_type() {
        let cond = TraitCondition {
            condition_type: "match".to_string(),
            trait_key: Some("env".to_string()),
            trait_value: Some("prod".to_string()),
            conditions: None,
        };
        let mut traits = HashMap::new();
        traits.insert("env".to_string(), "prod".to_string());
        assert!(match_condition(&cond, &traits));

        traits.insert("env".to_string(), "staging".to_string());
        assert!(!match_condition(&cond, &traits));
    }

    #[test]
    fn test_match_condition_and() {
        let cond = TraitCondition {
            condition_type: "and".to_string(),
            trait_key: None,
            trait_value: None,
            conditions: Some(vec![
                TraitCondition {
                    condition_type: "match".to_string(),
                    trait_key: Some("env".to_string()),
                    trait_value: Some("prod".to_string()),
                    conditions: None,
                },
                TraitCondition {
                    condition_type: "match".to_string(),
                    trait_key: Some("plan".to_string()),
                    trait_value: Some("enterprise".to_string()),
                    conditions: None,
                },
            ]),
        };

        let mut traits = HashMap::new();
        traits.insert("env".to_string(), "prod".to_string());
        traits.insert("plan".to_string(), "enterprise".to_string());
        assert!(match_condition(&cond, &traits));

        traits.insert("plan".to_string(), "free".to_string());
        assert!(!match_condition(&cond, &traits));
    }

    #[test]
    fn test_match_condition_or() {
        let cond = TraitCondition {
            condition_type: "or".to_string(),
            trait_key: None,
            trait_value: None,
            conditions: Some(vec![
                TraitCondition {
                    condition_type: "match".to_string(),
                    trait_key: Some("env".to_string()),
                    trait_value: Some("prod".to_string()),
                    conditions: None,
                },
                TraitCondition {
                    condition_type: "match".to_string(),
                    trait_key: Some("env".to_string()),
                    trait_value: Some("staging".to_string()),
                    conditions: None,
                },
            ]),
        };

        let mut traits = HashMap::new();
        traits.insert("env".to_string(), "prod".to_string());
        assert!(match_condition(&cond, &traits));

        traits.insert("env".to_string(), "dev".to_string());
        assert!(!match_condition(&cond, &traits));
    }

    #[test]
    fn test_match_condition_unknown() {
        let cond = TraitCondition {
            condition_type: "unknown".to_string(),
            trait_key: None,
            trait_value: None,
            conditions: None,
        };
        assert!(!match_condition(&cond, &HashMap::new()));
    }

    #[test]
    fn test_flag_before_init() {
        assert!(!flag("anything", None));
    }

    #[test]
    fn test_merge_traits() {
        let mut base = HashMap::new();
        base.insert("a".to_string(), "1".to_string());
        base.insert("b".to_string(), "2".to_string());

        let mut overrides = HashMap::new();
        overrides.insert("b".to_string(), "3".to_string());
        overrides.insert("c".to_string(), "4".to_string());

        let merged = super::merge_traits(&base, Some(&overrides));
        assert_eq!(merged.get("a").unwrap(), "1");
        assert_eq!(merged.get("b").unwrap(), "3");
        assert_eq!(merged.get("c").unwrap(), "4");
    }
}