mirror of
https://github.com/hexedtech/codemp.git
synced 2024-11-22 15:24:48 +01:00
feat(py): better configuration constructor.
Now it accept only keywords arguments so it support just passing in a dict.
This commit is contained in:
parent
9218e9e9c7
commit
e67b1c6bd0
1 changed files with 34 additions and 4 deletions
|
@ -3,14 +3,17 @@ pub mod controllers;
|
||||||
pub mod workspace;
|
pub mod workspace;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
api::{Cursor, TextChange},
|
api::{Config, Cursor, TextChange},
|
||||||
buffer::Controller as BufferController,
|
buffer::Controller as BufferController,
|
||||||
cursor::Controller as CursorController,
|
cursor::Controller as CursorController,
|
||||||
Client, Workspace,
|
Client, Workspace,
|
||||||
};
|
};
|
||||||
|
|
||||||
use pyo3::exceptions::{PyConnectionError, PyRuntimeError, PySystemError};
|
|
||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
|
use pyo3::{
|
||||||
|
exceptions::{PyAttributeError, PyConnectionError, PyRuntimeError, PySystemError},
|
||||||
|
types::PyDict,
|
||||||
|
};
|
||||||
|
|
||||||
use std::sync::OnceLock;
|
use std::sync::OnceLock;
|
||||||
use tokio::sync::{mpsc, oneshot};
|
use tokio::sync::{mpsc, oneshot};
|
||||||
|
@ -154,9 +157,36 @@ fn get_default_config() -> crate::api::Config {
|
||||||
conf
|
conf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pymethods]
|
||||||
|
impl Config {
|
||||||
|
#[new]
|
||||||
|
#[pyo3(signature = (*, username, password, **kwds))]
|
||||||
|
pub fn pynew(
|
||||||
|
username: String,
|
||||||
|
password: String,
|
||||||
|
kwds: Option<Bound<'_, PyDict>>,
|
||||||
|
) -> PyResult<Self> {
|
||||||
|
let config = kwds.map_or(Config::new(username, password), |dict| {
|
||||||
|
let host: Option<String> = dict.get_item("host")?.map(|s| s.extract());
|
||||||
|
let port: Option<u16> = dict.get_item("port")?.map(|p| p.extract());
|
||||||
|
let tls: Option<bool> = dict.get_item("tls")?.map(|t| t.extract());
|
||||||
|
|
||||||
|
Config {
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
host,
|
||||||
|
port,
|
||||||
|
tls,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(config)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[pyfunction]
|
#[pyfunction]
|
||||||
fn connect(py: Python, config: Py<crate::api::Config>) -> PyResult<Promise> {
|
fn connect(py: Python, config: Py<Config>) -> PyResult<Promise> {
|
||||||
let conf: crate::api::Config = config.extract(py)?;
|
let conf: Config = config.extract(py)?;
|
||||||
a_sync!(Client::connect(conf).await)
|
a_sync!(Client::connect(conf).await)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue