Skip to content

Commit ae510db

Browse files
authored
♻️ refactor: rustify writer utility find_alternative_row (#320)
1 parent 2ee43a8 commit ae510db

File tree

6 files changed

+42
-12
lines changed

6 files changed

+42
-12
lines changed

packages/biliass/rust/src/lib.rs

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

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_get_zoom_factor, py_test_free_rows, PyRows,
11+
py_find_alternative_row, py_get_zoom_factor, py_test_free_rows, PyRows,
1212
};

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,18 @@ pub fn py_test_free_rows(
9595
duration_still,
9696
))
9797
}
98+
99+
#[pyfunction(name = "find_alternative_row")]
100+
pub fn py_find_alternative_row(
101+
rows: &python::writer::PyRows,
102+
comment: &crate::python::PyComment,
103+
height: usize,
104+
bottom_reserved: usize,
105+
) -> PyResult<usize> {
106+
Ok(writer::rows::find_alternative_row(
107+
&rows.inner,
108+
&comment.inner,
109+
height,
110+
bottom_reserved,
111+
))
112+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,25 @@ pub fn test_free_rows(
5555
}
5656
res
5757
}
58+
59+
pub fn find_alternative_row(
60+
rows: &Rows,
61+
comment: &Comment,
62+
height: usize,
63+
bottom_reserved: usize,
64+
) -> usize {
65+
let mut res = 0;
66+
let comment_pos_id = comment.pos.clone() as usize;
67+
for row in 0..(height - bottom_reserved - comment.height.ceil() as usize) {
68+
match &rows[comment_pos_id][row] {
69+
None => return row,
70+
Some(comment) => {
71+
let comment_res = &rows[comment_pos_id][res].as_ref().expect("res is None");
72+
if comment.timeline < comment_res.timeline {
73+
res = row;
74+
}
75+
}
76+
}
77+
}
78+
res
79+
}

packages/biliass/src/biliass/_core.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,5 @@ class Rows:
8888
def __init__(self, num_types: int, capacity: int) -> None: ...
8989
def get(self, row: int, col: int) -> OptionComment: ...
9090
def set(self, row: int, col: int, OptionComment) -> None: ...
91+
92+
def find_alternative_row(rows: Rows, comment: Comment, height: int, bottom_reserved: int): ...

packages/biliass/src/biliass/biliass.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
convert_color,
1919
convert_flash_rotation,
2020
convert_timestamp,
21+
find_alternative_row,
2122
get_zoom_factor,
2223
read_comments_from_protobuf,
2324
read_comments_from_xml,
@@ -316,17 +317,6 @@ def process_comments(
316317
return ass.to_string()
317318

318319

319-
def find_alternative_row(rows: Rows, comment: Comment, height, bottom_reserved):
320-
res = 0
321-
comment_pos_id = comment.pos.id
322-
for row in range(height - bottom_reserved - math.ceil(comment.height)):
323-
if rows.get(comment_pos_id, row).is_none():
324-
return row
325-
elif rows.get(comment_pos_id, row).unwrap().timeline < rows.get(comment_pos_id, res).unwrap().timeline:
326-
res = row
327-
return res
328-
329-
330320
def mark_comment_row(rows: Rows, comment: Comment, row: int):
331321
comment_pos_id = comment.pos.id
332322
try:

0 commit comments

Comments
 (0)