Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/gunnar-ui

crates/gunnar-ui-web/src/sso_import.rs

//! **The CSV import job as the Identity provider page shows it** (A34-17):
//! Gunnar's fields and words; pure, so it is tested natively (the page itself
//! is wasm-only).

use serde::Deserialize;

/// An import job as Gunnar answers it (`POST`/`GET /api/account/sso/import`).
#[derive(Debug, Clone, Default, Deserialize)]
pub struct ImportJob {
    #[serde(default)]
    pub job_id: String,
    #[serde(default)]
    pub state: String,
    #[serde(default)]
    pub error: String,
    #[serde(default)]
    pub done_rows: u64,
    #[serde(default)]
    pub total_rows: u64,
    #[serde(default)]
    pub created: u32,
    #[serde(default)]
    pub updated: u32,
    #[serde(default)]
    pub deactivated: u32,
    #[serde(default)]
    pub reactivated: u32,
    #[serde(default)]
    pub errors: Vec<String>,
    #[serde(default)]
    pub dry_run: bool,
}

/// Whether an import job has ended (Gunnar's words: `done`, `failed`).
pub fn ended(state: &str) -> bool {
    matches!(state, "done" | "failed")
}

/// One line for an import job, from Gunnar's fields (A34-17).
pub fn import_line(d: &ImportJob) -> String {
    match d.state.as_str() {
        "failed" => format!("Nothing was imported: {}", d.error),
        "done" => format!(
            "{}{} created, {} updated, {} deactivated, {} reactivated{}",
            if d.dry_run { "Would be: " } else { "" },
            d.created, d.updated, d.deactivated, d.reactivated,
            if d.errors.is_empty() { String::new() } else { format!(" — {}", d.errors.join("; ")) }
        ),
        _ if d.total_rows > 0 => format!("Importing … {} of {} rows", d.done_rows, d.total_rows),
        _ => "Importing …".to_owned(),
    }
}

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

    /// ★ A34-17: the import is a job; the page says where it is in Gunnar's words.
    #[test]
    fn an_import_job_reads_as_gunnar_said_it() {
        let mut d = ImportJob { state: "running".into(), done_rows: 2, total_rows: 9, ..Default::default() };
        assert!(!ended(&d.state));
        assert_eq!(import_line(&d), "Importing … 2 of 9 rows");
        d.state = "done".into();
        d.created = 9;
        assert!(ended(&d.state));
        assert_eq!(import_line(&d), "9 created, 0 updated, 0 deactivated, 0 reactivated");
        d.state = "failed".into();
        d.error = "nothing was imported: line 4: no room".into();
        assert!(import_line(&d).starts_with("Nothing was imported: "));
    }
}