1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use crate::OpenLibraryError;
use serde;
use std::fmt::Display;
use std::str::FromStr;

pub trait Identifier: Display {
    fn acronym(&self) -> &'static str;
    fn value(&self) -> &str;
}

macro_rules! identifier {
    ($name:ident, $acy: literal) => {
        #[derive(serde::Serialize, serde::Deserialize, Debug, Eq, PartialEq, Hash, Clone)]
        #[serde(transparent)]
        pub struct $name(String);

        impl Identifier for $name {
            fn acronym(&self) -> &'static str {
                $acy
            }

            fn value(&self) -> &str {
                &self.0.as_str()
            }
        }

        impl FromStr for $name {
            type Err = OpenLibraryError;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                Ok($name(s.to_string()))
            }
        }

        impl std::fmt::Display for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                self.0.fmt(f)
            }
        }
    };
}

identifier!(InternationalStandardBookNumber, "ISBN");
identifier!(OpenLibraryIdentifer, "OLID");