Skip to main content

systemprompt_models/services/
includable.rs

1//! [`IncludableString`] — a string field that can either carry inline
2//! content or a `!include <path>` reference resolved at load time.
3//!
4//! Copyright (c) systemprompt.io — Business Source License 1.1.
5//! See <https://systemprompt.io> for licensing details.
6
7use serde::{Deserialize, Deserializer, Serialize};
8
9#[derive(Debug, Clone, Serialize)]
10#[serde(untagged)]
11pub enum IncludableString {
12    Inline(String),
13    Include { path: String },
14}
15
16impl<'de> Deserialize<'de> for IncludableString {
17    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
18    where
19        D: Deserializer<'de>,
20    {
21        let s = String::deserialize(deserializer)?;
22        Ok(s.strip_prefix("!include ").map_or_else(
23            || Self::Inline(s.clone()),
24            |path| Self::Include {
25                path: path.trim().to_owned(),
26            },
27        ))
28    }
29}
30
31impl IncludableString {
32    #[must_use]
33    pub const fn is_include(&self) -> bool {
34        matches!(self, Self::Include { .. })
35    }
36
37    #[must_use]
38    pub fn as_inline(&self) -> Option<&str> {
39        match self {
40            Self::Inline(s) => Some(s),
41            Self::Include { .. } => None,
42        }
43    }
44}
45
46impl Default for IncludableString {
47    fn default() -> Self {
48        Self::Inline(String::new())
49    }
50}