grovedb_version/
lib.rs

1use version::GroveVersion;
2
3pub mod error;
4pub mod version;
5
6#[macro_export]
7macro_rules! check_grovedb_v0_with_cost {
8    ($method:expr, $version:expr) => {{
9        const EXPECTED_VERSION: u16 = 0;
10        if $version != EXPECTED_VERSION {
11            return grovedb_costs::CostsExt::wrap_with_cost(
12                Err($crate::error::GroveVersionError::UnknownVersionMismatch {
13                    method: $method.to_string(),
14                    known_versions: vec![EXPECTED_VERSION],
15                    received: $version,
16                }
17                .into()),
18                Default::default(),
19            );
20        }
21    }};
22}
23
24#[macro_export]
25macro_rules! check_grovedb_v0 {
26    ($method:expr, $version:expr) => {{
27        const EXPECTED_VERSION: u16 = 0;
28        if $version != EXPECTED_VERSION {
29            return Err($crate::error::GroveVersionError::UnknownVersionMismatch {
30                method: $method.to_string(),
31                known_versions: vec![EXPECTED_VERSION],
32                received: $version,
33            }
34            .into());
35        }
36    }};
37}
38
39#[macro_export]
40macro_rules! check_grovedb_v0_or_v1 {
41    ($method:expr, $version:expr) => {{
42        const EXPECTED_VERSION_V0: u16 = 0;
43        const EXPECTED_VERSION_V1: u16 = 1;
44        if $version != EXPECTED_VERSION_V0 && $version != EXPECTED_VERSION_V1 {
45            return Err(GroveVersionError::UnknownVersionMismatch {
46                method: $method.to_string(),
47                known_versions: vec![EXPECTED_VERSION_V0, EXPECTED_VERSION_V1],
48                received: $version,
49            }
50            .into());
51        }
52        $version
53    }};
54}
55
56#[macro_export]
57macro_rules! check_merk_v0_with_cost {
58    ($method:expr, $version:expr) => {{
59        const EXPECTED_VERSION: u16 = 0;
60        if $version != EXPECTED_VERSION {
61            return grovedb_costs::CostsExt::wrap_with_cost(
62                Err($crate::error::GroveVersionError::UnknownVersionMismatch {
63                    method: $method.to_string(),
64                    known_versions: vec![EXPECTED_VERSION],
65                    received: $version,
66                }
67                .into()),
68                Default::default(),
69            );
70        }
71    }};
72}
73
74#[macro_export]
75macro_rules! check_merk_v0 {
76    ($method:expr, $version:expr) => {{
77        const EXPECTED_VERSION: u16 = 0;
78        if $version != EXPECTED_VERSION {
79            return Err($crate::error::GroveVersionError::UnknownVersionMismatch {
80                method: $method.to_string(),
81                known_versions: vec![EXPECTED_VERSION],
82                received: $version,
83            }
84            .into());
85        }
86    }};
87}
88
89pub trait TryFromVersioned<T>: Sized {
90    /// The type returned in the event of a conversion error.
91    type Error;
92
93    /// Performs the conversion.
94    fn try_from_versioned(value: T, grove_version: &GroveVersion) -> Result<Self, Self::Error>;
95}
96
97pub trait TryIntoVersioned<T>: Sized {
98    /// The type returned in the event of a conversion error.
99    type Error;
100
101    /// Performs the conversion.
102    fn try_into_versioned(self, grove_version: &GroveVersion) -> Result<T, Self::Error>;
103}
104
105impl<T, U> TryIntoVersioned<U> for T
106where
107    U: TryFromVersioned<T>,
108{
109    type Error = U::Error;
110
111    #[inline]
112    fn try_into_versioned(self, grove_version: &GroveVersion) -> Result<U, U::Error> {
113        U::try_from_versioned(self, grove_version)
114    }
115}
116
117impl<T, U> TryFromVersioned<U> for T
118where
119    T: TryFrom<U>,
120{
121    type Error = T::Error;
122
123    #[inline]
124    fn try_from_versioned(value: U, _grove_version: &GroveVersion) -> Result<Self, Self::Error> {
125        T::try_from(value)
126    }
127}