laburnum 1.17.2

An LSP framework for building language servers and compilers, powered by an incremental query tree with content-addressed storage, task-based dataflow, and parallel queries.
Documentation
// Copyright Two Neutron Stars Incorporated and contributors
// SPDX-License-Identifier: BlueOak-1.0.0

use crate::{
  Ident,
  database::{
    DynPartition,
    PartitionKey,
  },
};

pub struct Workspace;

impl PartitionKey for Workspace {
  const KEY: Ident = Ident::new("laburnum::workspace");
}
/// To write workspace configuration, create a wrapper partition with
/// [`impl_partition_for_dyn!`]:
///
/// ```rust,ignore
/// use laburnum::{impl_partition_for_dyn, partitions::Workspace};
///
/// // Your record type must implement WorkspaceRecord + Record
/// impl_partition_for_dyn!(MyWorkspacePartition, Workspace, MyConfig);
///
/// // Then write using the wrapper partition
/// let sort_key = WorkspaceSortKey::Config;
/// writer.write::<MyWorkspacePartition>(sort_key, config.into());
/// ```
///
/// [`impl_partition_for_dyn!`]: crate::impl_partition_for_dyn
impl DynPartition for Workspace {
  type DynSortKey = WorkspaceSortKey;
  type RecordConstraint = dyn WorkspaceRecord;
}

impl<R: WorkspaceRecord + crate::record::Record>
  crate::database::DynPartitionRecord<Workspace> for R
{
}

#[derive(Debug, Clone)]
pub enum WorkspaceSortKey {
  Config,
  All,
}

impl std::fmt::Display for WorkspaceSortKey {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      | WorkspaceSortKey::Config => write!(f, "config"),
      | WorkspaceSortKey::All => Ok(()),
    }
  }
}

// -- Sort key ordering (ADR0011) ----------------------------------------------

/// `All` is the empty prefix (token sequence `[]`), so it sorts first and is a
/// prefix of every key.
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
enum WsToken {
  Config,
}

impl WorkspaceSortKey {
  fn tokens(&self) -> Vec<WsToken> {
    match self {
      | WorkspaceSortKey::Config => vec![WsToken::Config],
      | WorkspaceSortKey::All => Vec::new(),
    }
  }
}

impl PartialEq for WorkspaceSortKey {
  fn eq(&self, other: &Self) -> bool {
    self.tokens() == other.tokens()
  }
}
impl Eq for WorkspaceSortKey {}
impl std::hash::Hash for WorkspaceSortKey {
  fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
    self.tokens().hash(state);
  }
}
impl PartialOrd for WorkspaceSortKey {
  fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
    Some(self.cmp(other))
  }
}
impl Ord for WorkspaceSortKey {
  fn cmp(&self, other: &Self) -> std::cmp::Ordering {
    self.tokens().cmp(&other.tokens())
  }
}

impl crate::database::PartitionSortKey for WorkspaceSortKey {
  fn is_prefix_of(&self, other: &Self) -> bool {
    other.tokens().starts_with(&self.tokens())
  }

  fn resolve(&self, _resolver: &dyn crate::SourceResolver) -> String {
    self.to_string()
  }
}

pub trait WorkspaceRecord: Send + Sync + std::fmt::Debug {}