Skip to content

Commit e8a2076

Browse files
committed
Refactor code structure for improved readability and maintainability
1 parent a6fcba9 commit e8a2076

15 files changed

Lines changed: 26618 additions & 64 deletions

File tree

Cargo.lock

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[package]
2-
name = "karat"
2+
name = "tryx"
33
version = "0.1.0"
44
edition = "2024"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77
[lib]
8-
name = "karat"
8+
name = "tryx"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]

pyproject.toml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ requires = ["maturin>=1.12,<2.0"]
33
build-backend = "maturin"
44

55
[project]
6-
name = "karat"
6+
name = "tryx"
77
requires-python = ">=3.8"
88
classifiers = [
99
"Programming Language :: Rust",
@@ -13,4 +13,14 @@ classifiers = [
1313
dynamic = ["version"]
1414

1515
[tool.maturin]
16-
module-name = "karat._karat"
16+
module-name = "tryx.tryx"
17+
python-source = "python"
18+
19+
[dependency-groups]
20+
dev = [
21+
"mypy-protobuf>=5.0.0",
22+
]
23+
# include = [
24+
# { path = "python/tryx/__init__.py", format = ["sdist", "wheel"] },
25+
# { path = "python/tryx/yaya.py", format = ["sdist", "wheel"] },
26+
# ]

python/tryx/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .yaya import hallo
2+
3+
__all__ = ["hallo"]

python/tryx/tryx.pyi

Whitespace-only changes.

python/tryx/waproto/whatsapp_pb2.py

Lines changed: 1420 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/tryx/waproto/whatsapp_pb2.pyi

Lines changed: 19208 additions & 0 deletions
Large diffs are not rendered by default.

src/backend.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11

2-
use std::sync::Arc;
3-
use async_trait::async_trait;
4-
use whatsapp_rust::store::Backend;
52
use whatsapp_rust_sqlite_storage::SqliteStore;
63
use pyo3::prelude::*;
74

@@ -13,10 +10,6 @@ pub struct SqliteBackend {
1310
path: String,
1411
}
1512

16-
#[pyclass(extends=BackendBase)]
17-
pub struct PostgresBackend {
18-
connection_string: String,
19-
}
2013
#[pymethods]
2114
impl SqliteBackend {
2215
#[new]
@@ -34,15 +27,3 @@ impl SqliteBackend {
3427
}
3528
}
3629

37-
#[pymethods]
38-
impl PostgresBackend {
39-
#[new]
40-
fn new(connection_string: String) -> (Self, BackendBase) {
41-
(PostgresBackend { connection_string }, BackendBase)
42-
}
43-
}
44-
pub enum BackendType {
45-
Sqlite(SqliteBackend),
46-
Postgres(PostgresBackend)
47-
}
48-

src/client.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
use std::any::Any;
2-
use std::{ sync::Arc};
3-
use pyo3::exceptions::PyException;
4-
use pyo3::{pyclass, pymethods};
1+
use std::sync::Arc;
2+
use pyo3::{Bound, PyAny, pyclass, pymethods};
53
use pyo3::prelude::*;
64
use pyo3_async_runtimes::async_std::future_into_py;
75
use tokio::runtime;
@@ -11,26 +9,43 @@ use whatsapp_rust::store::Backend;
119
use whatsapp_rust_tokio_transport::TokioWebSocketTransportFactory;
1210
use whatsapp_rust_ureq_http_client::UreqHttpClient;
1311

14-
use crate::backend::{BackendType, SqliteBackend, BackendBase};
12+
use crate::backend::{SqliteBackend, BackendBase};
13+
use crate::events::Dispatcher;
1514

1615
#[pyclass]
17-
struct KaratClient {
16+
pub struct Tryx {
1817
backend: Arc<dyn Backend>,
18+
handlers: Py<Dispatcher>,
1919
}
2020

2121
#[pymethods]
22-
impl KaratClient {
22+
impl Tryx {
2323
#[new]
2424
fn new(py: Python, backend: Py<BackendBase>) -> PyResult<Self> {
2525
if let Ok(sqlite) = backend.extract::<Py<SqliteBackend>>(py) {
2626
let backends = sqlite.borrow(py);
2727
let store = runtime::Runtime::new().unwrap().block_on(backends.connect()).map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e))?;
2828
// let store = sqlite_backend.connect().await.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e))?;
29-
Ok(KaratClient { backend: Arc::new(store)})
29+
Ok(Tryx {
30+
backend: Arc::new(store),
31+
handlers: Py::new(py, Dispatcher::empty())?,
32+
})
3033
} else {
3134
Err(PyErr::new::<pyo3::exceptions::PyTypeError, _>("Unsupported backend type"))
3235
}
3336
}
37+
38+
/// Returns a decorator compatible with:
39+
/// @client.on(Message)
40+
/// async def on_message(client, data): ...
41+
fn on(&self, py: Python, event_type: &Bound<PyAny>) -> PyResult<Py<PyAny>> {
42+
let decorator = self
43+
.handlers
44+
.bind(py)
45+
.call_method1("on", (event_type,))?;
46+
Ok(decorator.unbind())
47+
}
48+
3449
fn run<'py>(&'py self, py: Python<'py> ) -> Result<Bound<PyAny>, PyErr> {
3550
// Here you would implement the logic to run the bot using the backend
3651
let backend = self.backend.clone();
@@ -50,7 +65,7 @@ impl KaratClient {
5065
}
5166
})
5267
.build();
53-
bot.await.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?.run().await. .map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?.await.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?;
68+
// bot.await.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?.run().await. .map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?.await.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?;
5469
Ok(())
5570
})
5671
}

0 commit comments

Comments
 (0)