Skip to content

Commit fb06042

Browse files
authored
♻️ refactor: rustify writer utility mark_comment_row (#321)
1 parent ae510db commit fb06042

File tree

6 files changed

+26
-11
lines changed

6 files changed

+26
-11
lines changed

packages/biliass/rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ fn biliass_pyo3(m: &Bound<'_, PyModule>) -> PyResult<()> {
3434
m.add_function(wrap_pyfunction!(python::py_test_free_rows, m)?)?;
3535
m.add_class::<python::PyRows>()?;
3636
m.add_function(wrap_pyfunction!(python::py_find_alternative_row, m)?)?;
37+
m.add_function(wrap_pyfunction!(python::py_mark_comment_row, m)?)?;
3738
Ok(())
3839
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ pub use proto::{PyDanmakuElem, PyDmSegMobileReply};
88
pub use reader::{py_read_comments_from_protobuf, py_read_comments_from_xml};
99
pub use writer::{
1010
py_ass_escape, py_convert_color, py_convert_flash_rotation, py_convert_timestamp,
11-
py_find_alternative_row, py_get_zoom_factor, py_test_free_rows, PyRows,
11+
py_find_alternative_row, py_get_zoom_factor, py_mark_comment_row, py_test_free_rows, PyRows,
1212
};

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,13 @@ pub fn py_find_alternative_row(
110110
bottom_reserved,
111111
))
112112
}
113+
114+
#[pyfunction(name = "mark_comment_row")]
115+
pub fn py_mark_comment_row(
116+
rows: &mut python::writer::PyRows,
117+
comment: &crate::python::PyComment,
118+
row: usize,
119+
) -> PyResult<()> {
120+
writer::rows::mark_comment_row(&mut rows.inner, &comment.inner, row);
121+
Ok(())
122+
}

packages/biliass/rust/src/writer/rows.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::comment::{Comment, CommentPosition};
22

33
pub type Rows = Vec<Vec<Option<Comment>>>;
44

5+
// TODO(SigureMo): Remove clone in the future
6+
57
#[allow(clippy::too_many_arguments)]
68
pub fn test_free_rows(
79
rows: &Rows,
@@ -77,3 +79,13 @@ pub fn find_alternative_row(
7779
}
7880
res
7981
}
82+
83+
pub fn mark_comment_row(rows: &mut Rows, comment: &Comment, row: usize) {
84+
let comment_pos_id = comment.pos.clone() as usize;
85+
for i in row..(row + comment.height.ceil() as usize) {
86+
if i >= rows[comment_pos_id].len() {
87+
break;
88+
}
89+
rows[comment_pos_id][i] = Some(comment.clone());
90+
}
91+
}

packages/biliass/src/biliass/_core.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,4 @@ class Rows:
9090
def set(self, row: int, col: int, OptionComment) -> None: ...
9191

9292
def find_alternative_row(rows: Rows, comment: Comment, height: int, bottom_reserved: int): ...
93+
def mark_comment_row(rows: Rows, comment: Comment, row: int) -> None: ...

packages/biliass/src/biliass/biliass.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
from biliass._core import (
1313
Comment,
1414
CommentPosition,
15-
OptionComment,
1615
Rows,
1716
ass_escape,
1817
convert_color,
1918
convert_flash_rotation,
2019
convert_timestamp,
2120
find_alternative_row,
2221
get_zoom_factor,
22+
mark_comment_row,
2323
read_comments_from_protobuf,
2424
read_comments_from_xml,
2525
test_free_rows,
@@ -317,15 +317,6 @@ def process_comments(
317317
return ass.to_string()
318318

319319

320-
def mark_comment_row(rows: Rows, comment: Comment, row: int):
321-
comment_pos_id = comment.pos.id
322-
try:
323-
for i in range(row, row + math.ceil(comment.height)):
324-
rows.set(comment_pos_id, i, OptionComment.from_comment(comment))
325-
except IndexError:
326-
pass
327-
328-
329320
def convert_type2(row, height, bottom_reserved):
330321
return height - bottom_reserved - row
331322

0 commit comments

Comments
 (0)