Skip to main content

uqa_sql/notice/
level.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! The levels of notices.
8
9/// The level of a notice, as `PostgreSQL` names its message levels below `ERROR`, in increasing order of severity.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub enum NoticeLevel {
12    /// `DEBUG`, which `PL/pgSQL`'s `RAISE DEBUG` reports as `DEBUG1`.
13    Debug,
14    Log,
15    Info,
16    Notice,
17    Warning,
18}
19
20impl NoticeLevel {
21    /// The level a name spells, as `RAISE` and `elog` accept it; `DEBUG1` through `DEBUG5` are `DEBUG`.
22    pub fn parse(name: &str) -> Option<Self> {
23        let name = name.to_ascii_uppercase();
24        match name.as_str() {
25            "LOG" => Some(NoticeLevel::Log),
26            "INFO" => Some(NoticeLevel::Info),
27            "NOTICE" => Some(NoticeLevel::Notice),
28            "WARNING" => Some(NoticeLevel::Warning),
29            _ if name.starts_with("DEBUG") => Some(NoticeLevel::Debug),
30            _ => None,
31        }
32    }
33
34    /// The level's name, as clients show it.
35    pub const fn as_str(self) -> &'static str {
36        match self {
37            NoticeLevel::Debug => "DEBUG",
38            NoticeLevel::Log => "LOG",
39            NoticeLevel::Info => "INFO",
40            NoticeLevel::Notice => "NOTICE",
41            NoticeLevel::Warning => "WARNING",
42        }
43    }
44
45    /// The message level of `PostgreSQL`'s `elog.h` that the notice is reported at, which `client_min_messages` compares against.
46    pub const fn message_level(self) -> u8 {
47        use crate::semantics::parameters::catalog::message_levels;
48        match self {
49            NoticeLevel::Debug => message_levels::DEBUG1,
50            NoticeLevel::Log => message_levels::LOG,
51            NoticeLevel::Info => message_levels::INFO,
52            NoticeLevel::Notice => message_levels::NOTICE,
53            NoticeLevel::Warning => message_levels::WARNING,
54        }
55    }
56
57    /// Whether a notice of this level reaches a client whose `client_min_messages` is `minimum`: `INFO` always does, and every other level at or above the minimum (`errstart`'s `output_to_client`).
58    pub const fn reaches_client(self, minimum: u8) -> bool {
59        matches!(self, NoticeLevel::Info) || self.message_level() >= minimum
60    }
61
62    /// The SQLSTATE of a notice whose report names none, as `errstart` assigns it: `01000` (`warning`) for a warning and `00000` (`successful_completion`) for every lower level.
63    pub const fn default_sqlstate(self) -> &'static str {
64        match self {
65            NoticeLevel::Warning => "01000",
66            NoticeLevel::Debug | NoticeLevel::Log | NoticeLevel::Info | NoticeLevel::Notice => {
67                "00000"
68            }
69        }
70    }
71}