vstorage 0.7.0

Common API for various icalendar/vcard storages.
Documentation
// Copyright 2023-2026 Hugo Osvaldo Barrera
//
// SPDX-License-Identifier: EUPL-1.2
#![allow(dead_code)] // Bug in cargo; warns about used symbols being unused.

//! Common test utilities shared across test files.

use rand::{Rng, distr::Alphanumeric, rng};
use std::fmt::Write;

pub fn random_string(len: usize) -> String {
    rng()
        .sample_iter(Alphanumeric)
        .take(len)
        .map(char::from)
        .collect()
}

pub fn minimal_icalendar(summary: &str) -> anyhow::Result<String> {
    let uid = random_string(12);
    minimal_icalendar_with_uid(&uid, summary)
}

pub fn minimal_icalendar_with_uid(uid: &str, summary: &str) -> anyhow::Result<String> {
    let mut entry = String::new();

    entry.push_str("BEGIN:VCALENDAR\r\n");
    entry.push_str("VERSION:2.0\r\n");
    entry.push_str("PRODID:-//nl.whynothugo.pimsync//EN\r\n");
    entry.push_str("BEGIN:VEVENT\r\n");
    write!(entry, "UID:{uid}\r\n")?;
    entry.push_str("DTSTAMP:19970610T172345Z\r\n");
    entry.push_str("DTSTART:19970714T170000Z\r\n");
    write!(entry, "SUMMARY:{summary}\r\n")?;
    entry.push_str("END:VEVENT\r\n");
    entry.push_str("END:VCALENDAR\r\n");

    Ok(entry)
}