Skip to main content

gen_memory/
db.rs

1use std::hash::Hash;
2use std::io;
3
4#[derive(Clone, Debug, Eq, PartialEq, Hash)]
5pub struct Db {
6    name: String,
7}
8
9impl Db {
10    pub fn try_from(name: &str) -> io::Result<Db> {
11        if name.is_empty() {
12            return Err(io::Error::new(io::ErrorKind::InvalidInput, "Db name is empty"));
13        }
14
15        let ok_chars: bool = name
16            .chars()
17            .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_');
18
19        if !ok_chars {
20            return Err(io::Error::new(
21                io::ErrorKind::InvalidInput,
22                "Db name must match [A-Za-z0-9_-]+",
23            ));
24        }
25
26        Ok(Db {
27            name: name.to_ascii_lowercase(),
28        })
29    }
30}
31
32#[derive(PartialEq)]
33pub struct DbKey {
34    pub db: Db,
35    pub name: String,
36}
37
38impl DbKey {
39    pub fn from(db: &Db, name: &str) -> DbKey {
40        DbKey { db: db.clone(), name: name.to_string() }
41    }
42}
43
44#[cfg(test)]
45#[path = "../tests/unit_tests/db.rs"]
46pub mod tests;