lb-rs 26.11.26

The rust library for interacting with your lockbook.
Documentation
use std::hash::{DefaultHasher, Hash, Hasher};
use strum::VariantArray;
use strum_macros::VariantArray;

#[derive(VariantArray, Clone, Copy)]
pub enum WelcomeDoc {
    /// generated by username: `test0`
    NoWelcomeDoc,

    /// generated by username: `test1`
    OldWelcomeDoc,

    /// generated by username: `test5`
    HypeWelcomeDoc,

    /// generated by username: `test2`
    FramgentedArchetypes,
}

/// When a new account is asked to sign in on another device, if at all.
/// Retired: whether and when the backup nudge appeared after signup (replaced
/// on desktop by the immediate 3-step checklist). Kept so historical cohort
/// lookups remain reproducible.
/// Assigned with [`cohort_in`] under [`SignInNudge::EXPERIMENT`].
#[derive(VariantArray, Clone, Copy, Debug, PartialEq, Eq)]
pub enum SignInNudge {
    /// generated by username: `holdoutqa3`
    Holdout,

    /// generated by username: `twominqa1`
    TwoMinutes,

    /// generated by username: `fiveminqa3`
    FiveMinutes,
}

impl SignInNudge {
    pub const EXPERIMENT: &str = "nudges";
}

/// Whether newly created accounts see the "Choose a look" customization step
/// before landing in their workspace.
/// Assigned with [`cohort_in`] under [`OnboardLook::EXPERIMENT`].
#[derive(VariantArray, Clone, Copy, Debug, PartialEq, Eq)]
pub enum OnboardLook {
    Show,
    Skip,
}

impl OnboardLook {
    pub const EXPERIMENT: &str = "onboard_look";
}

pub fn cohort<E: VariantArray + Copy>(username: &str) -> E {
    let mut hasher = DefaultHasher::new();
    username.hash(&mut hasher);
    pick(hasher)
}

/// The arm for `username`, hashed with the experiment's name so experiments
/// with the same number of arms are assigned independently.
pub fn cohort_in<E: VariantArray + Copy>(experiment: &str, username: &str) -> E {
    let mut hasher = DefaultHasher::new();
    (experiment, username).hash(&mut hasher);
    pick(hasher)
}

fn pick<E: VariantArray + Copy>(hasher: DefaultHasher) -> E {
    let variant_id = (hasher.finish() % E::VARIANTS.len() as u64) as usize;
    E::VARIANTS[variant_id]
}

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

    /// Cohorts are recomputed from usernames long after signup, so the hash
    /// must never change: a new Rust release or a code change here would
    /// reshuffle every experiment's arms.
    #[test]
    fn assignments_are_stable() {
        let arms = |arm: fn(&str) -> usize| -> String {
            (0..32)
                .map(|i| arm(&format!("user{i}")).to_string())
                .collect()
        };
        assert_eq!(arms(|u| cohort::<WelcomeDoc>(u) as usize), "23202200310101001212012201201111");
        assert_eq!(
            arms(|u| cohort_in::<SignInNudge>(SignInNudge::EXPERIMENT, u) as usize),
            "22200022022220110220200002201212"
        );
        assert_eq!(
            arms(|u| cohort_in::<OnboardLook>(OnboardLook::EXPERIMENT, u) as usize),
            "10001101100111011111110111110110"
        );
    }
}