laburnum 1.17.3

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.
// Copyright Two Neutron Stars Incorporated and contributors
// SPDX-License-Identifier: BlueOak-1.0.0

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

pub struct Workspace;

impl PartitionKey for Workspace {
  const KEY: crate::SpannedIdent = crate::partitions::known_keys::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 bluegum::Bluegum for WorkspaceSortKey {
  fn node(&self, b: &mut bluegum::Builder) {
    b.name("WorkspaceSortKey");
  }
}

impl bluegum::BluegumWithState<dyn crate::SourceResolver> for WorkspaceSortKey {
  fn node_with_state(
    &self,
    b: &mut bluegum::Builder,
    resolver: &(dyn crate::SourceResolver + 'static),
  ) {
    b.name("WorkspaceSortKey")
      .alt(crate::database::PartitionSortKey::resolve(self, resolver));
  }
}

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 {}