Skip to main content

docx_core/control/
mod.rs

1//! Control-plane operations and requests for docx-mcp.
2//!
3//! The control plane coordinates parsing, ingestion, and query operations for
4//! a single solution against the backing store.
5
6use std::{error::Error, fmt, sync::Arc};
7
8use surrealdb::{Connection, Surreal};
9
10use crate::parsers::{CsharpParseError, RustdocParseError};
11use crate::store::{StoreError, SurrealDocStore};
12
13pub mod ingest;
14pub mod metadata;
15pub mod data;
16
17pub use ingest::{CsharpIngestReport, CsharpIngestRequest};
18pub use ingest::{RustdocIngestReport, RustdocIngestRequest};
19pub use metadata::ProjectUpsertRequest;
20
21/// Errors returned by control-plane operations.
22#[derive(Debug)]
23pub enum ControlError {
24    /// C# XML parse error.
25    Parse(CsharpParseError),
26    /// Rustdoc JSON parse error.
27    RustdocParse(RustdocParseError),
28    Store(StoreError),
29}
30
31impl fmt::Display for ControlError {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            Self::Parse(err) => write!(f, "{err}"),
35            Self::RustdocParse(err) => write!(f, "{err}"),
36            Self::Store(err) => write!(f, "{err}"),
37        }
38    }
39}
40
41impl Error for ControlError {}
42
43impl From<CsharpParseError> for ControlError {
44    fn from(err: CsharpParseError) -> Self {
45        Self::Parse(err)
46    }
47}
48
49impl From<RustdocParseError> for ControlError {
50    fn from(err: RustdocParseError) -> Self {
51        Self::RustdocParse(err)
52    }
53}
54
55impl From<StoreError> for ControlError {
56    fn from(err: StoreError) -> Self {
57        Self::Store(err)
58    }
59}
60
61/// Facade for ingestion and query operations for a single solution store.
62pub struct DocxControlPlane<C: Connection> {
63    store: SurrealDocStore<C>,
64}
65
66impl<C: Connection> Clone for DocxControlPlane<C> {
67    fn clone(&self) -> Self {
68        Self {
69            store: self.store.clone(),
70        }
71    }
72}
73
74impl<C: Connection> DocxControlPlane<C> {
75    /// Creates a control plane from a `SurrealDB` connection.
76    #[must_use]
77    pub fn new(db: Surreal<C>) -> Self {
78        Self {
79            store: SurrealDocStore::new(db),
80        }
81    }
82
83    /// Creates a control plane from a shared `SurrealDB` connection.
84    #[must_use]
85    pub const fn from_arc(db: Arc<Surreal<C>>) -> Self {
86        Self {
87            store: SurrealDocStore::from_arc(db),
88        }
89    }
90
91    /// Creates a control plane from an existing store implementation.
92    #[must_use]
93    pub const fn with_store(store: SurrealDocStore<C>) -> Self {
94        Self { store }
95    }
96
97    /// Returns the underlying store implementation.
98    #[must_use]
99    pub const fn store(&self) -> &SurrealDocStore<C> {
100        &self.store
101    }
102}