Skip to content

Commit 2243be4

Browse files
committed
Refactor most of common, in correct modules
1 parent d1142fd commit 2243be4

File tree

6 files changed

+52
-72
lines changed

6 files changed

+52
-72
lines changed

src/common/mod.rs

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
use async_trait::async_trait;
2-
use futures::prelude::*;
31
use gtk::glib;
42
use once_cell::sync::Lazy;
5-
use std::borrow::Cow;
6-
use std::cell::RefCell;
7-
use std::rc::Rc;
8-
use url::Url;
93

10-
use crate::gemini;
4+
use url::Url;
115

126
pub static DOWNLOAD_PATH: Lazy<std::path::PathBuf> = Lazy::new(|| {
137
let mut download_path = glib::user_special_dir(glib::UserDirectory::Downloads)
@@ -63,65 +57,3 @@ pub fn bookmarks_url() -> Url {
6357
pub fn glibctx() -> glib::MainContext {
6458
glib::MainContext::default()
6559
}
66-
67-
pub struct Color(pub u8, pub u8, pub u8);
68-
69-
impl std::fmt::LowerHex for Color {
70-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71-
write!(f, "{:02x}", self.0)?;
72-
write!(f, "{:02x}", self.1)?;
73-
write!(f, "{:02x}", self.2)?;
74-
Ok(())
75-
}
76-
}
77-
78-
#[derive(Debug)]
79-
pub enum PageElement {
80-
Heading(String),
81-
Quote(String),
82-
Preformatted(String),
83-
Text(String),
84-
Link(String, Option<String>),
85-
Empty,
86-
}
87-
88-
#[derive(Debug, Clone, PartialEq)]
89-
pub struct HistoryItem {
90-
pub url: url::Url,
91-
pub cache: Rc<RefCell<Option<Vec<u8>>>>,
92-
pub scroll_progress: f64,
93-
}
94-
95-
#[async_trait(?Send)]
96-
pub trait LossyTextRead {
97-
async fn read_line_lossy(&mut self, mut buf: &mut String) -> std::io::Result<usize>;
98-
}
99-
100-
#[async_trait(?Send)]
101-
impl<T: AsyncBufRead + Unpin> LossyTextRead for T {
102-
async fn read_line_lossy(&mut self, buf: &mut String) -> std::io::Result<usize> {
103-
// FIXME: thread 'main' panicked at 'assertion failed: self.is_char_boundary(new_len)', /build/rustc-1.58.1-src/library/alloc/src/string.rs:1204:13
104-
// This is safe because we treat buf as a mut Vec to read the data, BUT,
105-
// we check if it's valid utf8 using String::from_utf8_lossy.
106-
// If it's not valid utf8, we swap our buf with the newly allocated and
107-
// safe string returned from String::from_utf8_lossy
108-
//
109-
// In the implementation of BufReader::read_line, they talk about some things about
110-
// panic handling, which I don't understand currently. Whatever...
111-
unsafe {
112-
let vec_buf = buf.as_mut_vec();
113-
let mut n = self.read_until(b'\n', vec_buf).await?;
114-
115-
let correct_string = String::from_utf8_lossy(vec_buf);
116-
if let Cow::Owned(valid_utf8_string) = correct_string {
117-
// Yes, I know this is not good for performance because it requires useless copying.
118-
// BUT, this code will only be executed when invalid utf8 is found, so i
119-
// consider this as good enough
120-
buf.truncate(buf.len() - n); // Remove bad non-utf8 data
121-
buf.push_str(&valid_utf8_string); // Add correct utf8 data instead
122-
n = valid_utf8_string.len();
123-
}
124-
Ok(n)
125-
}
126-
}
127-
}

src/gemini/parser.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
use crate::common::PageElement;
21
use once_cell::sync::Lazy;
32
use regex::Regex;
43
static R_GEMINI_LINK: Lazy<Regex> =
54
Lazy::new(|| Regex::new(r"^=>\s*(?P<href>\S*)\s*(?P<label>.*)").unwrap());
65

6+
#[derive(Debug)]
7+
pub enum PageElement {
8+
Heading(String),
9+
Quote(String),
10+
Preformatted(String),
11+
Text(String),
12+
Link(String, Option<String>),
13+
Empty,
14+
}
15+
716
pub struct Parser {
817
inside_pre: bool,
918
}

src/lossy_text_read.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use async_trait::async_trait;
2+
use futures::prelude::*;
3+
use std::borrow::Cow;
4+
5+
#[async_trait(?Send)]
6+
pub trait LossyTextRead {
7+
async fn read_line_lossy(&mut self, mut buf: &mut String) -> std::io::Result<usize>;
8+
}
9+
10+
#[async_trait(?Send)]
11+
impl<T: AsyncBufRead + Unpin> LossyTextRead for T {
12+
async fn read_line_lossy(&mut self, buf: &mut String) -> std::io::Result<usize> {
13+
// FIXME: thread 'main' panicked at 'assertion failed: self.is_char_boundary(new_len)', /build/rustc-1.58.1-src/library/alloc/src/string.rs:1204:13
14+
// This is safe because we treat buf as a mut Vec to read the data, BUT,
15+
// we check if it's valid utf8 using String::from_utf8_lossy.
16+
// If it's not valid utf8, we swap our buf with the newly allocated and
17+
// safe string returned from String::from_utf8_lossy
18+
//
19+
// In the implementation of BufReader::read_line, they talk about some things about
20+
// panic handling, which I don't understand currently. Whatever...
21+
unsafe {
22+
let vec_buf = buf.as_mut_vec();
23+
let mut n = self.read_until(b'\n', vec_buf).await?;
24+
25+
let correct_string = String::from_utf8_lossy(vec_buf);
26+
if let Cow::Owned(valid_utf8_string) = correct_string {
27+
// Yes, I know this is not good for performance because it requires useless copying.
28+
// BUT, this code will only be executed when invalid utf8 is found, so i
29+
// consider this as good enough
30+
buf.truncate(buf.len() - n); // Remove bad non-utf8 data
31+
buf.push_str(&valid_utf8_string); // Add correct utf8 data instead
32+
n = valid_utf8_string.len();
33+
}
34+
Ok(n)
35+
}
36+
}
37+
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mod common;
1515
mod config;
1616
mod download_page;
1717
mod gemini;
18-
mod input_page;
18+
mod lossy_text_read;
1919
mod macros;
2020
mod tab;
2121
mod text_extensions;

src/tab.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ use std::rc::Rc;
2222
use url::Url;
2323

2424
use crate::common;
25-
use crate::common::{glibctx, HistoryItem, LossyTextRead, PageElement};
25+
use crate::common::{glibctx, HistoryItem, LossyTextRead};
2626
use crate::gemini;
27+
use crate::gemini::PageElement;
2728
use crate::text_extensions::Gemini as GeminiTextExt;
2829

2930
#[derive(Clone, Debug, glib::Boxed, Default)]

src/text_extensions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
/// Composable extensions over `gtk::TextEditor`
12
mod gemini;
23
pub use gemini::*;

0 commit comments

Comments
 (0)