Skip to content

Commit 56e9878

Browse files
committed
♻️ refactor: use rust binding comment struct
1 parent bd0657e commit 56e9878

File tree

3 files changed

+115
-145
lines changed

3 files changed

+115
-145
lines changed

packages/biliass/rust/src/python/comment.rs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,21 @@ pub enum PyCommentPosition {
1111
Special,
1212
}
1313

14-
#[pyclass(name = "Comment")]
14+
#[pymethods]
15+
impl PyCommentPosition {
16+
#[getter]
17+
fn id(&self) -> PyResult<u8> {
18+
Ok(match self {
19+
PyCommentPosition::Scroll => 0,
20+
PyCommentPosition::Bottom => 1,
21+
PyCommentPosition::Top => 2,
22+
PyCommentPosition::Reversed => 3,
23+
PyCommentPosition::Special => 4,
24+
})
25+
}
26+
}
27+
28+
#[pyclass(name = "Comment", frozen)]
1529
pub struct PyComment {
1630
inner: comment::Comment,
1731
}
@@ -25,57 +39,57 @@ impl PyComment {
2539
#[pymethods]
2640
impl PyComment {
2741
#[getter]
28-
fn timeline(&self) -> PyResult<f64> {
29-
Ok(self.inner.timeline)
42+
fn timeline(&self) -> f64 {
43+
self.inner.timeline
3044
}
3145

3246
#[getter]
33-
fn timestamp(&self) -> PyResult<u64> {
34-
Ok(self.inner.timestamp)
47+
fn timestamp(&self) -> u64 {
48+
self.inner.timestamp
3549
}
3650

3751
#[getter]
38-
fn no(&self) -> PyResult<u64> {
39-
Ok(self.inner.no)
52+
fn no(&self) -> u64 {
53+
self.inner.no
4054
}
4155

4256
#[getter]
43-
fn comment(&self) -> PyResult<String> {
44-
Ok(self.inner.comment.clone())
57+
fn comment(&self) -> &str {
58+
&self.inner.comment
4559
}
4660

4761
#[getter]
48-
fn pos(&self) -> PyResult<PyCommentPosition> {
49-
Ok(match self.inner.pos {
62+
fn pos(&self) -> PyCommentPosition {
63+
match self.inner.pos {
5064
comment::CommentPosition::Scroll => PyCommentPosition::Scroll,
5165
comment::CommentPosition::Bottom => PyCommentPosition::Bottom,
5266
comment::CommentPosition::Top => PyCommentPosition::Top,
5367
comment::CommentPosition::Reversed => PyCommentPosition::Reversed,
5468
comment::CommentPosition::Special => PyCommentPosition::Special,
55-
})
69+
}
5670
}
5771

5872
#[getter]
59-
fn color(&self) -> PyResult<u32> {
60-
Ok(self.inner.color)
73+
fn color(&self) -> u32 {
74+
self.inner.color
6175
}
6276

6377
#[getter]
64-
fn size(&self) -> PyResult<f32> {
65-
Ok(self.inner.size)
78+
fn size(&self) -> f32 {
79+
self.inner.size
6680
}
6781

6882
#[getter]
69-
fn height(&self) -> PyResult<f32> {
70-
Ok(self.inner.height)
83+
fn height(&self) -> f32 {
84+
self.inner.height
7185
}
7286

7387
#[getter]
74-
fn width(&self) -> PyResult<f32> {
75-
Ok(self.inner.width)
88+
fn width(&self) -> f32 {
89+
self.inner.width
7690
}
7791

78-
fn __repr__(&self) -> PyResult<String> {
79-
Ok(format!("Comment({:?})", self.inner))
92+
fn __repr__(&self) -> String {
93+
format!("Comment({:?})", self.inner)
8094
}
8195
}

packages/biliass/src/biliass/_core.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ class CommentPosition:
3939
Top: ClassVar[CommentPosition]
4040
Bottom: ClassVar[CommentPosition]
4141
Reversed: ClassVar[CommentPosition]
42-
Normal: ClassVar[CommentPosition]
4342
Special: ClassVar[CommentPosition]
4443

44+
@property
45+
def id(self) -> int: ...
46+
4547
class Comment:
4648
timeline: float
4749
timestamp: int

0 commit comments

Comments
 (0)