Skip to main content

mysql_handler/hton/
stat_type.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_stat_type` from `sql/handler.h`.
24
25/// Which subset of engine status MySQL wants for `SHOW ENGINE <name> STATUS`.
26///
27/// Mirrors `enum ha_stat_type` in `mysql-server/sql/handler.h`. Used as the
28/// argument to [`Handlerton::show_status`](crate::hton::Handlerton::show_status).
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30#[non_exhaustive]
31pub enum HaStatType {
32    /// `HA_ENGINE_STATUS`: `SHOW ENGINE <name> STATUS` — the engine's general
33    /// status block.
34    Status,
35    /// `HA_ENGINE_LOGS`: `SHOW ENGINE <name> LOGS` — log-file inventory.
36    Logs,
37    /// `HA_ENGINE_MUTEX`: `SHOW ENGINE <name> MUTEX` — mutex / lock statistics.
38    Mutex,
39}
40
41impl HaStatType {
42    /// Decode the C `enum ha_stat_type` value. Unknown values map to
43    /// [`HaStatType::Status`] so the engine still observes a defined variant.
44    #[must_use]
45    pub const fn from_raw(value: u32) -> Self {
46        match value {
47            1 => Self::Logs,
48            2 => Self::Mutex,
49            _ => Self::Status,
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!(HaStatType::from_raw(0), HaStatType::Status);
61        assert_eq!(HaStatType::from_raw(1), HaStatType::Logs);
62        assert_eq!(HaStatType::from_raw(2), HaStatType::Mutex);
63    }
64
65    #[test]
66    fn from_raw_unknown_value_falls_back_to_status() {
67        assert_eq!(HaStatType::from_raw(5), HaStatType::Status);
68        assert_eq!(HaStatType::from_raw(u32::MAX), HaStatType::Status);
69    }
70}