2024-08-08 23:58:45 +02:00
|
|
|
pub mod client;
|
|
|
|
pub mod controllers;
|
|
|
|
pub mod workspace;
|
|
|
|
|
2024-08-17 01:12:35 +02:00
|
|
|
use std::{
|
|
|
|
future::Future,
|
2024-08-20 11:22:45 +02:00
|
|
|
pin::Pin,
|
|
|
|
sync::OnceLock,
|
2024-08-17 01:12:35 +02:00
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
2024-08-08 23:58:45 +02:00
|
|
|
|
|
|
|
use crate::{
|
2024-08-09 09:14:27 +02:00
|
|
|
api::{Cursor, TextChange},
|
|
|
|
buffer::Controller as BufferController,
|
|
|
|
cursor::Controller as CursorController,
|
|
|
|
Client, Workspace,
|
2024-08-08 23:58:45 +02:00
|
|
|
};
|
|
|
|
use pyo3::exceptions::{PyConnectionError, PyRuntimeError, PySystemError};
|
|
|
|
use pyo3::prelude::*;
|
2024-08-20 11:22:45 +02:00
|
|
|
use tokio::sync::watch;
|
2024-08-08 23:58:45 +02:00
|
|
|
|
2024-08-17 01:12:35 +02:00
|
|
|
fn tokio() -> &'static tokio::runtime::Runtime {
|
|
|
|
static RT: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
|
2024-08-18 19:06:07 +02:00
|
|
|
RT.get_or_init(|| {
|
|
|
|
tokio::runtime::Builder::new_current_thread()
|
|
|
|
.enable_all()
|
|
|
|
.on_thread_start(|| tracing::info!("tokio thread started."))
|
|
|
|
.on_thread_stop(|| tracing::info!("tokio thread stopped."))
|
|
|
|
.build()
|
|
|
|
.unwrap()
|
|
|
|
})
|
2024-08-17 01:12:35 +02:00
|
|
|
}
|
|
|
|
|
2024-08-17 23:48:02 +02:00
|
|
|
// workaround to allow the GIL to be released across awaits, waiting on
|
|
|
|
// https://github.com/PyO3/pyo3/pull/3610
|
2024-08-17 01:12:35 +02:00
|
|
|
struct AllowThreads<F>(F);
|
|
|
|
|
|
|
|
impl<F> Future for AllowThreads<F>
|
|
|
|
where
|
|
|
|
F: Future + Unpin + Send,
|
|
|
|
F::Output: Send,
|
|
|
|
{
|
|
|
|
type Output = F::Output;
|
|
|
|
|
2024-08-20 11:22:45 +02:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2024-08-17 01:12:35 +02:00
|
|
|
let waker = cx.waker();
|
2024-08-20 11:22:45 +02:00
|
|
|
let fut = unsafe { self.map_unchecked_mut(|e| &mut e.0) };
|
|
|
|
Python::with_gil(|py| py.allow_threads(|| fut.poll(&mut Context::from_waker(waker))))
|
2024-08-17 01:12:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 11:22:45 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! spawn_future_allow_threads {
|
|
|
|
($fut:expr) => {
|
|
|
|
$crate::ffi::python::tokio().spawn($crate::ffi::python::AllowThreads(Box::pin(
|
|
|
|
async move {
|
|
|
|
tracing::info!("running future from rust.");
|
|
|
|
$fut.await
|
|
|
|
},
|
|
|
|
)))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! spawn_future {
|
|
|
|
($fut:expr) => {
|
|
|
|
$crate::ffi::python::tokio().spawn(async move { $fut.await })
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-08-08 23:58:45 +02:00
|
|
|
impl From<crate::Error> for PyErr {
|
|
|
|
fn from(value: crate::Error) -> Self {
|
|
|
|
match value {
|
|
|
|
crate::Error::Transport { status, message } => {
|
|
|
|
PyConnectionError::new_err(format!("Transport error: ({}) {}", status, message))
|
|
|
|
}
|
|
|
|
crate::Error::Channel { send } => {
|
|
|
|
PyConnectionError::new_err(format!("Channel error (send:{})", send))
|
|
|
|
}
|
|
|
|
crate::Error::InvalidState { msg } => {
|
|
|
|
PyRuntimeError::new_err(format!("Invalid state: {}", msg))
|
|
|
|
}
|
|
|
|
crate::Error::Deadlocked => PyRuntimeError::new_err("Deadlock, retry."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-17 01:11:11 +02:00
|
|
|
impl IntoPy<PyObject> for crate::api::User {
|
|
|
|
fn into_py(self, py: Python<'_>) -> PyObject {
|
|
|
|
self.id.to_string().into_py(py)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-08 23:58:45 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2024-08-20 11:22:45 +02:00
|
|
|
struct LoggerProducer(watch::Sender<String>);
|
2024-08-08 23:58:45 +02:00
|
|
|
|
|
|
|
impl std::io::Write for LoggerProducer {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
2024-08-20 11:22:45 +02:00
|
|
|
let _ = self.0.send(String::from_utf8_lossy(buf).to_string()); // ignore: logger disconnected or with full buffer
|
2024-08-08 23:58:45 +02:00
|
|
|
Ok(buf.len())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> std::io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyclass]
|
2024-08-20 11:22:45 +02:00
|
|
|
struct PyLogger(watch::Receiver<String>);
|
2024-08-08 23:58:45 +02:00
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl PyLogger {
|
|
|
|
#[new]
|
|
|
|
fn init_logger(debug: bool) -> PyResult<Self> {
|
2024-08-20 11:22:45 +02:00
|
|
|
let (tx, mut rx) = watch::channel("logger initialised".to_string());
|
2024-08-08 23:58:45 +02:00
|
|
|
let level = if debug {
|
|
|
|
tracing::Level::DEBUG
|
|
|
|
} else {
|
|
|
|
tracing::Level::INFO
|
|
|
|
};
|
|
|
|
|
|
|
|
let format = tracing_subscriber::fmt::format()
|
|
|
|
.without_time()
|
|
|
|
.with_level(true)
|
|
|
|
.with_target(true)
|
|
|
|
.with_thread_ids(false)
|
|
|
|
.with_thread_names(false)
|
|
|
|
.with_file(false)
|
|
|
|
.with_line_number(false)
|
|
|
|
.with_source_location(false)
|
|
|
|
.compact();
|
|
|
|
|
|
|
|
match tracing_subscriber::fmt()
|
|
|
|
.with_ansi(false)
|
|
|
|
.event_format(format)
|
|
|
|
.with_max_level(level)
|
|
|
|
.with_writer(std::sync::Mutex::new(LoggerProducer(tx)))
|
|
|
|
.try_init()
|
|
|
|
{
|
2024-08-20 11:22:45 +02:00
|
|
|
Ok(_) => Ok(PyLogger(rx)),
|
2024-08-08 23:58:45 +02:00
|
|
|
Err(_) => Err(PySystemError::new_err("A logger already exists")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 11:22:45 +02:00
|
|
|
async fn listen(&mut self) -> Option<String> {
|
|
|
|
if self.0.changed().await.is_ok() {
|
|
|
|
return Some(self.0.borrow().clone());
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
2024-08-08 23:58:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymodule]
|
2024-08-17 01:11:11 +02:00
|
|
|
fn codemp(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
2024-08-08 23:58:45 +02:00
|
|
|
m.add_class::<PyLogger>()?;
|
2024-08-09 09:14:27 +02:00
|
|
|
|
|
|
|
m.add_class::<TextChange>()?;
|
2024-08-08 23:58:45 +02:00
|
|
|
m.add_class::<BufferController>()?;
|
|
|
|
|
|
|
|
m.add_class::<Cursor>()?;
|
2024-08-09 09:14:27 +02:00
|
|
|
m.add_class::<CursorController>()?;
|
|
|
|
|
|
|
|
m.add_class::<Workspace>()?;
|
|
|
|
m.add_class::<Client>()?;
|
2024-08-08 23:58:45 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|