Skip to main content

opys_engine/
config.rs

1//! Shared field-spec types used by the universal config (`opys.toml`). See
2//! [`crate::project_config`] for the engine config.
3
4use serde::Deserialize;
5
6/// Declared type of a custom frontmatter field.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize)]
8#[serde(rename_all = "lowercase")]
9pub enum FieldType {
10    #[default]
11    String,
12    List,
13    Bool,
14    Int,
15    /// A string constrained to the declared `values` set (see [`FieldSpec`]).
16    Enum,
17}
18
19impl FieldType {
20    pub fn as_str(self) -> &'static str {
21        match self {
22            FieldType::String => "string",
23            FieldType::List => "list",
24            FieldType::Bool => "bool",
25            FieldType::Int => "int",
26            FieldType::Enum => "enum",
27        }
28    }
29}
30
31/// Declaration of a project-specific frontmatter field (a type's `[fields.*]`).
32#[derive(Debug, Clone, Deserialize)]
33pub struct FieldSpec {
34    #[serde(default, rename = "type")]
35    pub field_type: FieldType,
36    #[serde(default)]
37    pub required: bool,
38    #[serde(default)]
39    pub description: Option<String>,
40    /// Allowed values for an `enum` field; ignored for other types.
41    #[serde(default)]
42    pub values: Vec<String>,
43    /// Optional regex a `string` value must fully match.
44    #[serde(default)]
45    pub pattern: Option<String>,
46}