model.rs

  1use std::path::{Path, PathBuf};
  2
  3use anyhow::{bail, Result};
  4
  5use gpui::Axis;
  6use sqlez::{
  7    bindable::{Bind, Column},
  8    statement::Statement,
  9};
 10
 11#[derive(Debug, PartialEq, Eq, Clone)]
 12pub(crate) struct WorkspaceId(pub(crate) Vec<PathBuf>);
 13
 14impl<P: AsRef<Path>, T: IntoIterator<Item = P>> From<T> for WorkspaceId {
 15    fn from(iterator: T) -> Self {
 16        let mut roots = iterator
 17            .into_iter()
 18            .map(|p| p.as_ref().to_path_buf())
 19            .collect::<Vec<_>>();
 20        roots.sort();
 21        Self(roots)
 22    }
 23}
 24
 25impl Bind for &WorkspaceId {
 26    fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
 27        bincode::serialize(&self.0)
 28            .expect("Bincode serialization of paths should not fail")
 29            .bind(statement, start_index)
 30    }
 31}
 32
 33impl Column for WorkspaceId {
 34    fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
 35        let blob = statement.column_blob(start_index)?;
 36        Ok((WorkspaceId(bincode::deserialize(blob)?), start_index + 1))
 37    }
 38}
 39
 40#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
 41pub enum DockAnchor {
 42    #[default]
 43    Bottom,
 44    Right,
 45    Expanded,
 46}
 47
 48impl Bind for DockAnchor {
 49    fn bind(&self, statement: &Statement, start_index: i32) -> anyhow::Result<i32> {
 50        match self {
 51            DockAnchor::Bottom => "Bottom",
 52            DockAnchor::Right => "Right",
 53            DockAnchor::Expanded => "Expanded",
 54        }
 55        .bind(statement, start_index)
 56    }
 57}
 58
 59impl Column for DockAnchor {
 60    fn column(statement: &mut Statement, start_index: i32) -> anyhow::Result<(Self, i32)> {
 61        String::column(statement, start_index).and_then(|(anchor_text, next_index)| {
 62            Ok((
 63                match anchor_text.as_ref() {
 64                    "Bottom" => DockAnchor::Bottom,
 65                    "Right" => DockAnchor::Right,
 66                    "Expanded" => DockAnchor::Expanded,
 67                    _ => bail!("Stored dock anchor is incorrect"),
 68                },
 69                next_index,
 70            ))
 71        })
 72    }
 73}
 74
 75pub(crate) type WorkspaceRow = (WorkspaceId, DockAnchor, bool);
 76
 77#[derive(Debug)]
 78pub struct SerializedWorkspace {
 79    pub dock_anchor: DockAnchor,
 80    pub dock_visible: bool,
 81    pub center_group: SerializedPaneGroup,
 82    pub dock_pane: SerializedPane,
 83}
 84
 85#[derive(Debug, PartialEq, Eq)]
 86pub struct SerializedPaneGroup {
 87    axis: Axis,
 88    children: Vec<SerializedPaneGroup>,
 89}
 90
 91#[derive(Debug)]
 92pub struct SerializedPane {
 93    _children: Vec<SerializedItem>,
 94}
 95
 96#[derive(Debug)]
 97pub enum SerializedItemKind {}
 98
 99#[derive(Debug)]
100pub enum SerializedItem {}