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    pub data: JsonValue,
18    pub layout: SectionLayout,
19}
20
21impl DashboardSection {
22    pub fn new(
23        section_id: impl Into<String>,
24        title: impl Into<String>,
25        section_type: SectionType,
26    ) -> Self {
27        Self {
28            section_id: SectionId::new(section_id),
29            title: title.into(),
30            section_type,
31            data: JsonValue::Object(serde_json::Map::new()),
32            layout: SectionLayout::default(),
33        }
34    }
35
36    pub fn with_data<T: Serialize>(mut self, data: T) -> Result<Self, serde_json::Error> {
37        self.data = serde_json::to_value(data)?;
38        Ok(self)
39    }
40
41    pub const fn with_layout(mut self, layout: SectionLayout) -> Self {
42        self.layout = layout;
43        self
44    }
45
46    pub const fn with_order(mut self, order: u32) -> Self {
47        self.layout.order = order;
48        self
49    }
50}