Skip to main content

openauth_core/plugin/
error.rs

1//! Plugin error code registry types.
2
3use crate::error::OpenAuthError;
4
5/// Error code contributed by a plugin.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct PluginErrorCode {
8    pub code: String,
9    pub message: String,
10}
11
12impl PluginErrorCode {
13    pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
14        Self {
15            code: code.into(),
16            message: message.into(),
17        }
18    }
19
20    pub fn validate(&self) -> Result<(), OpenAuthError> {
21        if self.code.is_empty()
22            || !self
23                .code
24                .bytes()
25                .all(|byte| byte == b'_' || byte.is_ascii_uppercase())
26        {
27            return Err(OpenAuthError::InvalidConfig(format!(
28                "plugin error code `{}` must use upper snake case",
29                self.code
30            )));
31        }
32        Ok(())
33    }
34}