Skip to main content

code_moniker_workspace/registry/
ports.rs

1use std::sync::Arc;
2
3use crate::changes::ChangeOverlayPort;
4use crate::code::CodeIndexPort;
5use crate::linkage::LinkagePort;
6use crate::live::WorkspaceWatchRoot;
7use crate::snapshot::{
8	WorkspaceFailure, WorkspaceRequest, WorkspaceSnapshot, WorkspaceTransition, WorkspaceView,
9};
10use crate::source::SourceCatalogPort;
11
12use super::command::{WorkspaceCommandKind, WorkspaceScopeUri, WorkspaceSnapshotPublication};
13use super::event::{WorkspaceEvent, WorkspaceEventCursor};
14
15pub struct WorkspacePorts {
16	pub(crate) source_catalog: Box<dyn SourceCatalogPort + Send>,
17	pub(crate) code_index: Box<dyn CodeIndexPort + Send>,
18	pub(crate) linkage: Box<dyn LinkagePort + Send>,
19	pub(crate) change_overlay: Box<dyn ChangeOverlayPort + Send>,
20	live_watch_roots: Box<LiveWatchRoots>,
21}
22
23type LiveWatchRoots = dyn Fn(Option<&WorkspaceSnapshot>) -> Vec<WorkspaceWatchRoot> + Send + Sync;
24
25impl WorkspacePorts {
26	pub fn new(
27		source_catalog: impl SourceCatalogPort + Send + 'static,
28		code_index: impl CodeIndexPort + Send + 'static,
29		linkage: impl LinkagePort + Send + 'static,
30		change_overlay: impl ChangeOverlayPort + Send + 'static,
31	) -> Self {
32		Self {
33			source_catalog: Box::new(source_catalog),
34			code_index: Box::new(code_index),
35			linkage: Box::new(linkage),
36			change_overlay: Box::new(change_overlay),
37			live_watch_roots: Box::new(|_| Vec::new()),
38		}
39	}
40
41	pub(crate) fn with_live_watch_roots<F>(mut self, live_watch_roots: F) -> Self
42	where
43		F: Fn(Option<&WorkspaceSnapshot>) -> Vec<WorkspaceWatchRoot> + Send + Sync + 'static,
44	{
45		self.live_watch_roots = Box::new(live_watch_roots);
46		self
47	}
48
49	pub(crate) fn live_watch_roots(
50		&self,
51		snapshot: Option<&WorkspaceSnapshot>,
52	) -> Vec<WorkspaceWatchRoot> {
53		(self.live_watch_roots)(snapshot)
54	}
55}
56
57pub trait WorkspaceCommandPort {
58	fn execute_command(
59		&mut self,
60		kind: WorkspaceCommandKind,
61		scope_uri: WorkspaceScopeUri,
62		request: WorkspaceRequest,
63	) -> WorkspaceTransition;
64
65	fn publish_snapshot(
66		&mut self,
67		publication: WorkspaceSnapshotPublication,
68	) -> WorkspaceTransition;
69}
70
71pub trait WorkspaceQueryPort {
72	fn snapshot(&self) -> Option<&WorkspaceSnapshot>;
73	fn snapshot_arc(&self) -> Option<Arc<WorkspaceSnapshot>>;
74	fn view(&self) -> Option<WorkspaceView<'_>>;
75	fn last_failure(&self) -> Option<&WorkspaceFailure>;
76}
77
78pub trait WorkspaceEventPort {
79	fn event_cursor(&self) -> WorkspaceEventCursor;
80	fn events_since(&self, cursor: WorkspaceEventCursor) -> &[WorkspaceEvent];
81}