Skip to content

Commit 7bdbb2e

Browse files
committed
feat: refactor MediaType handling and introduce Node and NodeContent structures
1 parent f160800 commit 7bdbb2e

8 files changed

Lines changed: 207 additions & 148 deletions

File tree

src/clients/tryx.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ use crate::events::types::{
5858
};
5959
use crate::exceptions::UnsupportedBackend;
6060
use crate::events::dispatcher::Dispatcher;
61-
use crate::types::MessageInfo;
6261

6362

6463
#[pyclass]
@@ -616,8 +615,7 @@ impl Tryx {
616615
Self::call_event(connect_failure_callbacks, payload, locals.clone()).await.unwrap();
617616
}
618617
Event::StreamError(_) => {
619-
let payload = Python::attach(|py| pyo3::Py::new(py, EvStreamError {})).map_err(|e| e).unwrap();
620-
Self::call_event(stream_error_callbacks, payload, locals.clone()).await.unwrap();
618+
todo!()
621619
}
622620
}
623621
}

src/clients/tryx_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use prost::Message;
1010
use whatsapp_rust::Client;
1111
use crate::events::types::{EvMessage};
1212
use crate::types::{JID, UploadResponse};
13-
use crate::wacore::MediaType;
13+
use crate::wacore::download::MediaType;
1414
#[pyclass]
1515
pub struct TryxClient {
1616
pub client_rx: watch::Receiver<Option<Arc<Client>>>,

src/events/types.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use whatsapp_rust::{Jid, types::events::{ConnectFailureReason, LoggedOut as What
1010
use pyo3::sync::PyOnceLock;
1111
use whatsapp_rust::types::message::{MessageInfo as WhatsappMessageInfo};
1212
use crate::types::{JID, MessageInfo, MessageSource};
13+
use crate::wacore::node::{Node, NodeContent, NodeValue};
1314

1415
static WHATSAPP_MESSAGE_PROTO: PyOnceLock<Py<PyType>> = PyOnceLock::new();
1516
static SYNC_ACTION_VALUE: PyOnceLock<Py<PyType>> = PyOnceLock::new();
@@ -413,7 +414,11 @@ impl EvTemporaryBan {
413414
pub struct EvConnectFailure;
414415

415416
#[pyclass]
416-
pub struct EvStreamError;
417+
pub struct EvStreamError{
418+
#[pyo3(get)]
419+
code: String,
420+
raw: Option<Py<Node>>
421+
}
417422

418423
#[pyclass]
419424
pub struct EvMessage {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use self::events::types::{
4141
};
4242
use self::backend::SqliteBackend;
4343
use self::types::{JID, MessageInfo, UploadResponse};
44-
use self::wacore::MediaType;
44+
use self::wacore::download::MediaType;
4545

4646
/// A Python module implemented in Rust.
4747
///

src/types.rs

Lines changed: 1 addition & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl JID {
5151
fn server(&self) -> String {
5252
self.inner.server.clone()
5353
}
54-
fn __repr__(&self) -> String {
54+
pub fn __repr__(&self) -> String {
5555
format!("JID(user='{}', server='{}')", self.inner.user, self.inner.server)
5656
}
5757
}
@@ -318,144 +318,3 @@ pub struct UploadResponse {
318318
#[pyo3(get)]
319319
pub file_length: u64,
320320
}
321-
pub enum NodeValueEnum {
322-
String(String),
323-
Jid(pyo3::Py<JID>),
324-
}
325-
326-
/// PyClass wrapper untuk NodeValue
327-
#[pyclass]
328-
pub struct NodeValue {
329-
inner: NodeValueEnum,
330-
}
331-
332-
#[pymethods]
333-
impl NodeValue {
334-
#[new]
335-
pub fn new_string(value: String) -> Self {
336-
Self { inner: NodeValueEnum::String(value) }
337-
}
338-
339-
#[staticmethod]
340-
pub fn jid(value: pyo3::Py<JID>) -> Self {
341-
Self { inner: NodeValueEnum::Jid(value) }
342-
}
343-
344-
pub fn set_string(&mut self, value: String) {
345-
self.inner = NodeValueEnum::String(value);
346-
}
347-
348-
pub fn set_jid(&mut self, value: pyo3::Py<JID>) {
349-
self.inner = NodeValueEnum::Jid(value);
350-
}
351-
352-
pub fn __repr__(&self, py: Python<'_>) -> String {
353-
match &self.inner {
354-
NodeValueEnum::String(s) => format!("NodeValue::String({})", s),
355-
NodeValueEnum::Jid(jid) => {
356-
let jid_ref = jid.bind(py).borrow();
357-
format!("NodeValue::Jid({})", jid_ref.__repr__())
358-
}
359-
}
360-
}
361-
}
362-
363-
/// NodeContent enum internal
364-
pub enum NodeContentEnum {
365-
Bytes(Vec<u8>),
366-
String(String),
367-
Nodes(Vec<pyo3::Py<Node>>),
368-
}
369-
370-
/// PyClass wrapper untuk NodeContent
371-
#[pyclass]
372-
pub struct NodeContent {
373-
inner: NodeContentEnum,
374-
}
375-
376-
#[pymethods]
377-
impl NodeContent {
378-
#[staticmethod]
379-
pub fn bytes(data: Vec<u8>) -> Self {
380-
Self { inner: NodeContentEnum::Bytes(data) }
381-
}
382-
383-
#[staticmethod]
384-
pub fn string(data: String) -> Self {
385-
Self { inner: NodeContentEnum::String(data) }
386-
}
387-
388-
#[staticmethod]
389-
pub fn nodes(data: Vec<pyo3::Py<Node>>) -> Self {
390-
Self { inner: NodeContentEnum::Nodes(data) }
391-
}
392-
393-
pub fn is_bytes(&self) -> bool {
394-
matches!(self.inner, NodeContentEnum::Bytes(_))
395-
}
396-
397-
pub fn is_string(&self) -> bool {
398-
matches!(self.inner, NodeContentEnum::String(_))
399-
}
400-
401-
pub fn is_nodes(&self) -> bool {
402-
matches!(self.inner, NodeContentEnum::Nodes(_))
403-
}
404-
405-
pub fn as_bytes<'py>(&self, py: Python<'py>) -> Option<pyo3::Py<PyBytes>> {
406-
match &self.inner {
407-
NodeContentEnum::Bytes(b) => Some(PyBytes::new(py, b).unbind()),
408-
_ => None,
409-
}
410-
}
411-
412-
pub fn as_string(&self) -> Option<&str> {
413-
match &self.inner {
414-
NodeContentEnum::String(s) => Some(s),
415-
_ => None,
416-
}
417-
}
418-
419-
pub fn as_nodes(&self) -> Option<&Vec<pyo3::Py<Node>>> {
420-
match &self.inner {
421-
NodeContentEnum::Nodes(n) => Some(n),
422-
_ => None,
423-
}
424-
}
425-
426-
pub fn __repr__(&self) -> String {
427-
match &self.inner {
428-
NodeContentEnum::Bytes(_) => "NodeContent::Bytes(...)".to_string(),
429-
NodeContentEnum::String(s) => format!("NodeContent::String({})", s),
430-
NodeContentEnum::Nodes(n) => format!("NodeContent::Nodes(len={})", n.len()),
431-
}
432-
}
433-
}
434-
435-
/// PyClass Node
436-
#[pyclass]
437-
pub struct Node {
438-
#[pyo3(get, set)]
439-
pub tag: String,
440-
441-
#[pyo3(get, set)]
442-
pub content: Option<pyo3::Py<NodeContent>>,
443-
}
444-
445-
#[pymethods]
446-
impl Node {
447-
#[new]
448-
pub fn new(tag: String, content: Option<pyo3::Py<NodeContent>>) -> Self {
449-
Self { tag, content }
450-
}
451-
452-
pub fn __repr__(&self, py: Python<'_>) -> String {
453-
match &self.content {
454-
Some(c) => {
455-
let content_ref = c.bind(py).borrow();
456-
format!("Node(tag={}, content={})", self.tag, content_ref.__repr__())
457-
}
458-
None => format!("Node(tag={}, content=None)", self.tag),
459-
}
460-
}
461-
}
File renamed without changes.

src/wacore/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod node;
2+
pub mod download;

0 commit comments

Comments
 (0)