Skip to main content

mysql_handler/hton/
dict_kind.rs

1// Copyright (C) 2026 ren-yamanashi
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is designed to work with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of this program hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have either included with
13// the program or referenced in the documentation.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, see <https://www.gnu.org/licenses/>.
22
23//! `dict_init_mode_t` and `dict_recovery_mode_t` from `sql/handler.h`.
24
25/// How a data-dictionary backend should initialise its on-disk files.
26///
27/// Mirrors `enum dict_init_mode_t` in `mysql-server/sql/handler.h`.
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29#[non_exhaustive]
30pub enum DictInitMode {
31    /// `DICT_INIT_CREATE_FILES`: create all required SE files.
32    CreateFiles,
33    /// `DICT_INIT_CHECK_FILES`: verify existence of expected files.
34    CheckFiles,
35}
36
37impl DictInitMode {
38    /// Decode the C `enum dict_init_mode_t` value. Unknown values map to
39    /// [`DictInitMode::CheckFiles`] so the engine still observes a defined
40    /// variant.
41    #[must_use]
42    pub const fn from_raw(value: u32) -> Self {
43        match value {
44            0 => Self::CreateFiles,
45            _ => Self::CheckFiles,
46        }
47    }
48}
49
50/// Mode for data-dictionary recovery during startup.
51///
52/// Mirrors `enum dict_recovery_mode_t` in `mysql-server/sql/handler.h`.
53#[derive(Debug, Clone, Copy, PartialEq, Eq)]
54#[non_exhaustive]
55pub enum DictRecoveryMode {
56    /// `DICT_RECOVERY_INITIALIZE_SERVER`: first start of a new server.
57    InitializeServer,
58    /// `DICT_RECOVERY_INITIALIZE_TABLESPACES`: first start, create tablespaces.
59    InitializeTablespaces,
60    /// `DICT_RECOVERY_RESTART_SERVER`: restart of an existing server.
61    RestartServer,
62}
63
64impl DictRecoveryMode {
65    /// Decode the C `enum dict_recovery_mode_t` value. Unknown values map to
66    /// [`DictRecoveryMode::RestartServer`].
67    #[must_use]
68    pub const fn from_raw(value: u32) -> Self {
69        match value {
70            0 => Self::InitializeServer,
71            1 => Self::InitializeTablespaces,
72            _ => Self::RestartServer,
73        }
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn dict_init_mode_round_trip() {
83        assert_eq!(DictInitMode::from_raw(0), DictInitMode::CreateFiles);
84        assert_eq!(DictInitMode::from_raw(1), DictInitMode::CheckFiles);
85        assert_eq!(DictInitMode::from_raw(99), DictInitMode::CheckFiles);
86    }
87
88    #[test]
89    fn dict_recovery_mode_round_trip() {
90        assert_eq!(
91            DictRecoveryMode::from_raw(0),
92            DictRecoveryMode::InitializeServer
93        );
94        assert_eq!(
95            DictRecoveryMode::from_raw(1),
96            DictRecoveryMode::InitializeTablespaces
97        );
98        assert_eq!(
99            DictRecoveryMode::from_raw(2),
100            DictRecoveryMode::RestartServer
101        );
102        assert_eq!(
103            DictRecoveryMode::from_raw(99),
104            DictRecoveryMode::RestartServer
105        );
106    }
107}