crev_data/util/
mod.rs

1use rand::{self, Rng};
2use std::fmt;
3
4#[must_use]
5pub fn random_id_str() -> String {
6    let mut out = [0u8; 32];
7    rand::thread_rng().fill(&mut out[..]);
8    crev_common::base64_encode(&out)
9}
10
11pub fn write_comment_proof(comment: &str, f: &mut dyn fmt::Write) -> fmt::Result {
12    if comment.is_empty() {
13        return Ok(());
14    }
15    writeln!(f, "comment: |-")?;
16    for line in comment.lines() {
17        writeln!(f, "  {line}")?;
18    }
19    Ok(())
20}
21
22pub fn write_comment_draft(comment: &str, f: &mut dyn fmt::Write) -> fmt::Result {
23    writeln!(f, "comment: |-")?;
24    for line in comment.lines() {
25        writeln!(f, "  {line}")?;
26    }
27    if comment.is_empty() {
28        writeln!(f, "  ")?;
29    }
30    Ok(())
31}
32
33#[macro_export]
34macro_rules! serde_content_serialize {
35    ($self: ident, $fmt: ident) => {
36        // Remove comment for manual formatting
37        let mut clone = $self.clone();
38        let mut comment = String::new();
39        std::mem::swap(&mut comment, &mut clone.comment);
40
41        if clone.common.kind.is_none() {
42            clone.common.kind = Some(Self::KIND.into());
43        }
44
45        crev_common::serde::write_as_headerless_yaml(&clone, $fmt)?;
46        $crate::util::write_comment_proof(comment.as_str(), $fmt)?;
47    };
48}
49
50#[macro_export]
51macro_rules! serde_draft_serialize {
52    ($self: ident, $fmt: ident) => {
53        // Remove comment for manual formatting
54        let mut clone = $self.clone();
55        let mut comment = String::new();
56        std::mem::swap(&mut comment, &mut clone.comment);
57
58        crev_common::serde::write_as_headerless_yaml(&clone, $fmt)?;
59        $crate::util::write_comment_draft(comment.as_str(), $fmt)?;
60    };
61}