mysql_handler/hton/panic_function.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//! `ha_panic_function` from `include/my_base.h`.
24
25/// The reason MySQL is invoking the engine-level `panic` callback.
26///
27/// Mirrors `enum ha_panic_function` in `mysql-server/include/my_base.h`. Used
28/// only as the argument to [`Handlerton::panic`](crate::hton::Handlerton::panic).
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30#[non_exhaustive]
31pub enum HaPanicFunction {
32 /// `HA_PANIC_CLOSE`: close all databases before shutdown.
33 Close,
34 /// `HA_PANIC_WRITE`: unlock and write status.
35 Write,
36 /// `HA_PANIC_READ`: lock and read keyinfo.
37 Read,
38}
39
40impl HaPanicFunction {
41 /// Decode the C `enum ha_panic_function` value. Unknown values map to
42 /// [`HaPanicFunction::Close`] so the engine still observes a defined variant
43 /// rather than a panic on a forward-compatible MySQL change.
44 #[must_use]
45 pub const fn from_raw(value: u32) -> Self {
46 match value {
47 1 => Self::Write,
48 2 => Self::Read,
49 _ => Self::Close,
50 }
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn from_raw_maps_each_known_value() {
60 assert_eq!(HaPanicFunction::from_raw(0), HaPanicFunction::Close);
61 assert_eq!(HaPanicFunction::from_raw(1), HaPanicFunction::Write);
62 assert_eq!(HaPanicFunction::from_raw(2), HaPanicFunction::Read);
63 }
64
65 #[test]
66 fn from_raw_unknown_value_falls_back_to_close() {
67 assert_eq!(HaPanicFunction::from_raw(7), HaPanicFunction::Close);
68 assert_eq!(HaPanicFunction::from_raw(u32::MAX), HaPanicFunction::Close);
69 }
70}