Skip to content

Commit

Permalink
Simplify error handling and FFI interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreichold committed Apr 14, 2024
1 parent 75f7504 commit 3ed37f8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 24 deletions.
3 changes: 2 additions & 1 deletion internals/src/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ impl Compressor {
return Err("Cannot compress text containing nul character".into());
}

let offset: u32 = self.buf.len().try_into().unwrap();
let offset = self.buf.len().try_into().unwrap();

self.buf.extend_from_slice(text.as_bytes());
self.buf.push(b'\0');

Expand Down
4 changes: 2 additions & 2 deletions internals/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use time::Time;
use super::{
compressor::{BackgroundCompressor, Decompressor},
parser::Item,
Error, Fallible,
Fallible,
};

const TEXT_BLOB_LEN: usize = 256 * 1024;
Expand Down Expand Up @@ -393,7 +393,7 @@ impl BlobFetcher {
let mut rows = stmt.query(params![blob_id])?;
let row = rows
.next()?
.ok_or_else(|| Error::from(format!("No BLOB with ID {}", blob_id)))?;
.ok_or_else(|| format!("No BLOB with ID {}", blob_id))?;

let blob = row.get_ref_unwrap(0).as_blob()?;

Expand Down
22 changes: 7 additions & 15 deletions internals/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#![allow(improper_ctypes)]
#![allow(clippy::missing_safety_doc)]

mod compressor;
mod database;
mod parser;

use std::error::Error as StdError;
use std::error::Error;
use std::ffi::{CStr, CString, OsStr};
use std::os::{
raw::{c_char, c_void},
Expand All @@ -14,7 +13,7 @@ use std::os::{
use std::path::{Path, PathBuf};
use std::ptr::{null, null_mut};
use std::slice::from_raw_parts;
use std::str::from_utf8;
use std::str::from_utf8_unchecked;
use std::sync::mpsc::{sync_channel, Receiver};
use std::thread::spawn;

Expand All @@ -27,8 +26,7 @@ use self::database::{
};
use self::parser::{parse, Item};

pub type Error = Box<dyn StdError + Send + Sync>;
pub type Fallible<T = ()> = Result<T, Error>;
pub type Fallible<T = ()> = Result<T, Box<dyn Error + Send + Sync>>;

#[repr(C)]
pub enum SortColumn {
Expand Down Expand Up @@ -246,7 +244,7 @@ AND shows.id = ?
let mut rows = stmt.query([&id])?;
let row = rows
.next()?
.ok_or_else(|| Error::from(format!("No show with ID {}", id)))?;
.ok_or_else(|| format!("No show with ID {}", id))?;

let channel = row.get_ref_unwrap(0).as_str()?;
let topic = row.get_ref_unwrap(1).as_str()?;
Expand Down Expand Up @@ -304,7 +302,6 @@ extern "C" {
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct StringData {
ptr: *const c_char,
len: usize,
Expand Down Expand Up @@ -342,7 +339,7 @@ impl From<&str> for StringData {

impl StringData {
unsafe fn as_str(&self) -> &str {
from_utf8(from_raw_parts(self.ptr.cast(), self.len)).unwrap()
from_utf8_unchecked(from_raw_parts(self.ptr.cast(), self.len))
}
}

Expand Down Expand Up @@ -384,7 +381,6 @@ pub unsafe extern "C" fn internals_drop(internals: *mut Internals) {
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct Completion {
context: *mut c_void,
action: unsafe extern "C" fn(context: *mut c_void, error: *const c_char),
Expand Down Expand Up @@ -462,19 +458,15 @@ pub unsafe extern "C" fn internals_query(
title.as_str(),
sort_column,
sort_order,
|id| {
append_integer(ids, id);
},
|id| append_integer(ids, id),
) {
eprintln!("Failed to query shows: {}", err);
}
}

#[no_mangle]
pub unsafe extern "C" fn internals_fetch(internals: *mut Internals, id: i64, show: *mut c_void) {
if let Err(err) = (*internals).fetch(id, |data| {
fetch_show(show, &data);
}) {
if let Err(err) = (*internals).fetch(id, |data| fetch_show(show, &data)) {
eprintln!("Failed to fetch show: {}", err);
}
}
10 changes: 4 additions & 6 deletions internals/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::Deserialize;
use serde_json::{from_slice, from_str, value::RawValue};
use time::{macros::format_description, Date, Time};

use super::{Error, Fallible};
use super::Fallible;

pub struct Item {
pub channel: String,
Expand Down Expand Up @@ -131,11 +131,11 @@ fn parse_fields(fields: &[u8]) -> Fallible<Item> {
}

fn to_string(&self, index: usize) -> Fallible<String> {
from_str(self.get(index)).map_err(Error::from)
from_str(self.get(index)).map_err(Into::into)
}

fn as_str(&self, index: usize) -> Fallible<&str> {
from_str(self.get(index)).map_err(Error::from)
from_str(self.get(index)).map_err(Into::into)
}
}

Expand Down Expand Up @@ -186,9 +186,7 @@ fn parse_url_suffix(url: &str, mut field: String) -> Fallible<Option<String>> {

if let Some(pos) = field.find('|') {
let index = field[..pos].parse()?;
let url = url
.get(..index)
.ok_or_else(|| Error::from("Malformed URL suffix"))?;
let url = url.get(..index).ok_or("Malformed URL suffix")?;

field.replace_range(..=pos, url);
} else {
Expand Down

0 comments on commit 3ed37f8

Please sign in to comment.