Skip to main content

systemprompt_models/artifacts/dashboard/
section.rs

1//! Dashboard section content model.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use super::section_types::{SectionLayout, SectionType};
7use schemars::JsonSchema;
8use serde::{Deserialize, Serialize};
9use serde_json::Value as JsonValue;
10use systemprompt_identifiers::SectionId;
11
12#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
13pub struct DashboardSection {
14    pub section_id: SectionId,
15    pub title: String,
16    pub section_type: SectionType,
17    // JSON: Section payload is one of the `section_data` shapes, decoded by `kind`.
18    pub data: JsonValue,
19    pub layout: SectionLayout,
20}
21
22impl DashboardSection {
23    pub fn new(
24        section_id: impl Into<String>,
25        title: impl Into<String>,
26        section_type: SectionType,
27    ) -> Self {
28        Self {
29            section_id: SectionId::new(section_id),
30            title: title.into(),
31            section_type,
32            data: JsonValue::Object(serde_json::Map::new()),
33            layout: SectionLayout::default(),
34        }
35    }
36
37    pub fn with_data<T: Serialize>(mut self, data: T) -> Result<Self, serde_json::Error> {
38        self.data = serde_json::to_value(data)?;
39        Ok(self)
40    }
41
42    pub const fn with_layout(mut self, layout: SectionLayout) -> Self {
43        self.layout = layout;
44        self
45    }
46
47    pub const fn with_order(mut self, order: u32) -> Self {
48        self.layout.order = order;
49        self
50    }
51}