oml_storage/storage_id/
random_id.rs1use crate::StorageId;
2use color_eyre::eyre::Result;
3use serde::{Deserialize, Serialize};
4use std::fmt;
5#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Serialize, Deserialize, Hash)]
10pub struct RandomId(String);
11
12impl RandomId {
13 pub fn new() -> Self {
15 Self(nanoid::nanoid!())
16 }
17
18 pub fn from_str(s: &str) -> Self {
20 Self(s.to_string())
21 }
22
23 pub fn value(&self) -> &str {
25 &self.0
26 }
27}
28
29impl StorageId for RandomId {
30 fn from_string(s: &str) -> Result<Self> {
31 Ok(Self(s.to_string()))
32 }
33
34 fn generate_new(_previous: Option<&Self>) -> Self {
35 Self::new()
36 }
37
38 fn is_valid_format(_s: &str) -> bool {
39 true
42 }
43}
44
45impl fmt::Display for RandomId {
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 write!(f, "{}", self.0)
48 }
49}