items.rs

  1use std::{
  2    ffi::OsStr,
  3    fmt::Display,
  4    hash::Hash,
  5    os::unix::prelude::OsStrExt,
  6    path::{Path, PathBuf},
  7    sync::Arc,
  8};
  9
 10use anyhow::Result;
 11use collections::HashSet;
 12use rusqlite::{named_params, params, types::FromSql};
 13
 14use crate::workspace::WorkspaceId;
 15
 16use super::Db;
 17
 18/// Current design makes the cut at the item level,
 19///   - Maybe A little more bottom up, serialize 'Terminals' and 'Editors' directly, and then make a seperate
 20///   - items table, with a kind, and an integer that acts as a key to one of these other tables
 21/// This column is a foreign key to ONE OF: editors, terminals, searches
 22///   -
 23
 24// (workspace_id, item_id)
 25// kind -> ::Editor::
 26
 27// ->
 28// At the workspace level
 29// -> (Workspace_ID, item_id)
 30// -> One shot, big query, load everything up:
 31
 32// -> SerializedWorkspace::deserialize(tx, itemKey)
 33//     -> SerializedEditor::deserialize(tx, itemKey)
 34
 35//         ->
 36// -> Workspace::new(SerializedWorkspace)
 37//     -> Editor::new(serialized_workspace[???]serializedEditor)
 38
 39// //Pros: Keeps sql out of every body elese, makes changing it easier (e.g. for loading from a network or RocksDB)
 40// //Cons: DB has to know the internals of the entire rest of the app
 41
 42// Workspace
 43// Worktree roots
 44// Pane groups
 45// Dock
 46// Items
 47// Sidebars
 48
 49// Things I'm doing: finding about nullability for foreign keys
 50pub(crate) const ITEMS_M_1: &str = "
 51CREATE TABLE project_searches(
 52    workspace_id INTEGER,
 53    item_id INTEGER,
 54    query TEXT,
 55    PRIMARY KEY (workspace_id, item_id)
 56    FOREIGN KEY(workspace_id) REFERENCES workspace_ids(workspace_id)
 57) STRICT;
 58
 59CREATE TABLE editors(
 60    workspace_id INTEGER,
 61    item_id INTEGER,
 62    path BLOB NOT NULL,
 63    PRIMARY KEY (workspace_id, item_id)
 64    FOREIGN KEY(workspace_id) REFERENCES workspace_ids(workspace_id)
 65) STRICT;
 66";
 67
 68#[derive(Debug, PartialEq, Eq)]
 69pub struct ItemId {
 70    workspace_id: usize,
 71    item_id: usize,
 72}
 73
 74enum SerializedItemKind {
 75    Editor,
 76    Diagnostics,
 77    ProjectSearch,
 78    Terminal,
 79}
 80
 81struct SerializedItemRow {
 82    kind: SerializedItemKind,
 83    item_id: usize,
 84    path: Option<Arc<Path>>,
 85    query: Option<String>,
 86}
 87
 88#[derive(Debug, PartialEq, Eq)]
 89pub enum SerializedItem {
 90    Editor { item_id: usize, path: Arc<Path> },
 91    Diagnostics { item_id: usize },
 92    ProjectSearch { item_id: usize, query: String },
 93    Terminal { item_id: usize },
 94}
 95
 96impl SerializedItem {
 97    pub fn item_id(&self) -> usize {
 98        match self {
 99            SerializedItem::Editor { item_id, .. } => *item_id,
100            SerializedItem::Diagnostics { item_id } => *item_id,
101            SerializedItem::ProjectSearch { item_id, .. } => *item_id,
102            SerializedItem::Terminal { item_id } => *item_id,
103        }
104    }
105}
106
107impl Db {
108    pub fn get_item(&self, item_id: ItemId) -> SerializedItem {
109        unimplemented!()
110    }
111
112    pub fn save_item(&self, workspace_id: WorkspaceId, item: &SerializedItem) {}
113
114    pub fn close_item(&self, item_id: ItemId) {}
115}