Skip to main content

orbital_data/
schema.rs

1use serde::{Deserialize, Serialize};
2
3use crate::DataType;
4
5/// Shared field descriptor — binding key for table columns and chart series.
6#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
7pub struct FieldDef {
8    pub key: String,
9    pub label: String,
10    pub data_type: DataType,
11}
12
13impl FieldDef {
14    pub fn new(key: impl Into<String>, label: impl Into<String>, data_type: DataType) -> Self {
15        Self {
16            key: key.into(),
17            label: label.into(),
18            data_type,
19        }
20    }
21
22    pub fn text(key: impl Into<String>, label: impl Into<String>) -> Self {
23        Self::new(key, label, DataType::Text)
24    }
25}
26
27/// Schema describing the fields in a [`crate::Dataset`].
28#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
29pub struct DataSchema {
30    pub fields: Vec<FieldDef>,
31}
32
33impl DataSchema {
34    pub fn new(fields: Vec<FieldDef>) -> Self {
35        Self { fields }
36    }
37
38    pub fn from_text_fields(fields: impl IntoIterator<Item = (String, String)>) -> Self {
39        Self {
40            fields: fields
41                .into_iter()
42                .map(|(key, label)| FieldDef::text(key, label))
43                .collect(),
44        }
45    }
46}