Skip to content

Commit

Permalink
improvement: clean up all unwrap call
Browse files Browse the repository at this point in the history
  • Loading branch information
7sDream committed Nov 14, 2023
1 parent c0a3cc2 commit 9084824
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 49 deletions.
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ fn main() {
let families = family::group_by_family_sort_by_name(&font_set);

if families.is_empty() {
println!("No font support this character.");
eprintln!("No font support this character {}.", argument.char.description());
return;
}

if argument.tui {
let ui = UI::new(families).unwrap();
ui.show().unwrap_or_else(|err| {
let ui = UI::new(families).expect("family length checked before, must not empty");
if let Err(err) = ui.show() {
eprintln!("{:?}", err);
});
};
} else {
let builder = if argument.preview {
Some(PreviewServerBuilder::from_iter(families.iter()))
Expand All @@ -72,10 +72,10 @@ fn show_preview_addr_and_wait(addr: SocketAddr) {
println!("{}", "-".repeat(40));
println!("Please visit http://{}/ in your browser for preview", addr);
print!("And press Enter after your finish...");
std::io::stdout().flush().unwrap();
std::io::stdout().flush().expect("flush stdout should not fail");

// Wait until user input any character before stop the server
let _ = std::io::stdin().read(&mut [0u8]).unwrap();
let _ = std::io::stdin().read(&mut [0u8]).expect("read from stdout should not fail");
}

fn show_font_list(families: Vec<Family<'_>>, verbose: u8) {
Expand Down
5 changes: 3 additions & 2 deletions src/one_char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ impl OneChar {
})
.take_while(|(c1, _)| c1.is_some())
.map(|(c1, c2)| -> Result<u8, ParseError> {
let c1 = c1.unwrap(); // at least one char because of the `take_while`
let c1 = c1.expect("at least one char because of the take_while");
let c2 = c2.ok_or(ParseError::UTF8BytesStrCantAlignToBytes)?;
if c1.is_ascii_hexdigit() && c2.is_ascii_hexdigit() {
Ok((c1.to_digit(16).unwrap() << 4 | c2.to_digit(16).unwrap()) as u8)
Ok((c1.to_digit(16).expect("hexdigit") << 4
| c2.to_digit(16).expect("hexdigit")) as u8)
} else {
Err(ParseError::InvalidDigitInRadix(16))
}
Expand Down
38 changes: 22 additions & 16 deletions src/preview/browser/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ enum CheckRequestResult {
}

impl SingleThread {
pub const fn new(html: String) -> Self {
pub fn new(html: String) -> Self {
Self { html }
}

Expand Down Expand Up @@ -183,36 +183,42 @@ impl SingleThread {
addr_tx: Sender<SocketAddr>, exit_rx: Receiver<()>, content: String,
) -> Result<(), IOError> {
let loopback = Ipv4Addr::new(127, 0, 0, 1);
let server = TcpListener::bind((loopback, 0)).unwrap_or_else(|err| {
eprintln!("Error when start http server: {:?}", err);
std::process::exit(-1)
});
let server = match TcpListener::bind((loopback, 0)) {
Ok(server) => server,
Err(err) => {
eprintln!("Error when start http server: {:?}", err);
std::process::exit(-1)
}
};

let addr = server.local_addr().unwrap();
addr_tx.send(addr).unwrap();
let addr = server.local_addr().expect("tcp listener must have a local addr");
if addr_tx.send(addr).is_err() {
return Ok(());
}

// set non-blocking mode to give chance to receive exit message
server.set_nonblocking(true)?;

for stream in server.incoming() {
match stream {
Ok(stream) => match Self::handler(stream, &content, &exit_rx) {
Ok(false) => return Ok(()),
Ok(true) => {}
Ok(false) => break,
Err(err) => {
eprintln!("Error when process request: {:?}", err);
}
Ok(..) => (),
},
Err(err) if err.kind() == IOErrorKind::WouldBlock => {
thread::sleep(Duration::from_millis(100));
match exit_rx.try_recv() {
Ok(()) | Err(TryRecvError::Disconnected) => break,
_ => {
continue;
}
}
}
Err(_) => break,
Err(err) => {
eprintln!("Error when listening: {:?}", err);
}
}

match exit_rx.try_recv() {
Ok(_) | Err(TryRecvError::Disconnected) => break,
Err(TryRecvError::Empty) => {}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/preview/terminal/ui/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ fn keyboard_event_generator(tick_interval: Duration, tx: mpsc::Sender<IoResult<T
loop {
match event::poll(tick_interval) {
Ok(true) => {
if let Event::Key(key) = event::read().unwrap() {
let ev = event::read().expect("read terminal event should not fail");
if let Event::Key(key) = ev {
#[allow(clippy::collapsible_if)]
if key.kind != KeyEventKind::Release {
if tx.send(Ok(TerminalEvent::Key(key))).is_err() {
Expand Down
50 changes: 26 additions & 24 deletions src/preview/terminal/ui/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ impl<'a> State<'a> {
self.cache.borrow_mut().entry(key).or_insert_with(|| Rc::new(self.real_render())).clone()
}

fn real_render(&self) -> Result<GlyphCache, &'static str> {
fn rasterize(&self) -> Result<Bitmap, &'static str> {
let info = self.font_faces_info[self.index()];
let glyph = DATABASE

DATABASE
.with_face_data(info.id, |data, index| -> Option<Bitmap> {
let mut face = FontFace::new(data, index).ok()?;
face.set_size(self.height.get(), self.width.get());
Expand All @@ -92,35 +93,36 @@ impl<'a> State<'a> {
_ => PixelFormat::Gray,
})
})
.unwrap();

match glyph {
Some(bitmap) => {
let cache = match self.rt {
RenderType::Mono => GlyphCache::Canvas(GlyphCanvasShape::new(
MONO_RENDER.render(&bitmap),
self.width.get() as f64,
self.height.get() as f64,
)),
rt => GlyphCache::Paragraph(GlyphParagraph::new(
CHAR_RENDERS.get(&rt).expect("all render must be exist").render(&bitmap),
)),
};
Ok(cache)
}
None => Err("Can't get glyph from this font"),
}
.ok_or("Can't read this font file")?
.ok_or("Can't get glyph from this font")
}

fn real_render(&self) -> Result<GlyphCache, &'static str> {
let bitmap = self.rasterize()?;

let cache = match self.rt {
RenderType::Mono => GlyphCache::Canvas(GlyphCanvasShape::new(
MONO_RENDER.render(&bitmap),
self.width.get() as f64,
self.height.get() as f64,
)),
rt => GlyphCache::Paragraph(GlyphParagraph::new(
CHAR_RENDERS.get(&rt).expect("all render must be exist").render(&bitmap),
)),
};

Ok(cache)
}

pub fn current_name(&self) -> &'a str {
self.font_faces_name[self.index()]
}

pub const fn name_width_max(&self) -> usize {
pub fn name_width_max(&self) -> usize {
self.name_width_max
}

pub const fn family_names(&self) -> &Vec<&'a str> {
pub fn family_names(&self) -> &Vec<&'a str> {
&self.font_faces_name
}

Expand All @@ -129,7 +131,7 @@ impl<'a> State<'a> {
}

pub fn index(&self) -> usize {
self.list_state.borrow().selected().unwrap()
self.list_state.borrow().selected().expect("always has a selected item")
}

pub fn move_up(&mut self) {
Expand All @@ -145,7 +147,7 @@ impl<'a> State<'a> {
self.list_state.borrow_mut().select(changed);
}

pub const fn get_render_type(&self) -> &RenderType {
pub fn get_render_type(&self) -> &RenderType {
&self.rt
}

Expand Down

0 comments on commit 9084824

Please sign in to comment.