Custom Error Type Problem - the trait bound std::io::Error: From is not satisfied
⚓ Rust 📅 2026-03-03 👤 surdeus 👁️ 3I have written a function that both reads in a CSV file line by line and inserts the data into an SQLite database. The potential exists of having an io or an rusqlite error. To allow my passing either error back to main.rs via std::io::Result with a concrete error type, I am attempting to use a custom error that will handle both error types. My code appears to contend with an io error but not rusqlite errors from conn.execute. The error message is "the trait bound 'std::io::Error: Fromrusqilte::Error is not satisfied. The trait 'Fromrusqlite::Error is not implemented for std::io::Error". In the associated Err(e: Error) line the error type is rusqlite::Error. Any insight / direction will be appreciated.
use std::fs::File;
use std::io::{BufReader, BufRead};
use rusqlite::{Connection, named_params};
use std::io::Result;
#[derive(Debug)]
pub enum IoSqlError {
IoError(std::io::Error),
SqlError(rusqlite::Error),
}
impl From<std::io::Error> for IoSqlError {
fn from(error: std::io::Error) -> Self {
IoSqlError::IoError(error)
}
}
impl From<rusqlite::Error> for IoSqlError {
fn from(error: rusqlite::Error) -> Self {
IoSqlError::SqlError(error)
}
}
pub fn csv_to_sql(csv_file: &File, conn : &Connection) -> Result<((), IoSqlError)> {
let reader = BufReader::new(csv_file);
let mut lines = reader.lines().skip(1);
let mut fields: Vec<String>;
while let Some(line) = lines.next() {
match line {
Ok(lineitem) => {
fields = lineitem.split(',').map(|s| s.trim().to_string()).collect();
},
Err(e) => { // Error is of type std::io::Error
eprintln!("Error reading line: {}", e);
return Err(From::from(e));
}
}
match conn.execute (
"Insert INTO data (date, open, high, low, close, volume)
values (:date, :open, :high, :low, :close, :volume)",
named_params! {"date": fields[0],
"open": fields[1].parse::<f64>().unwrap_or(0.0),
"high": fields[2].parse::<f64>().unwrap_or(0.0),
"low": fields[3].parse::<f64>().unwrap_or(0.0),
"close": fields[4].parse::<f64>().unwrap_or(0.0),
"volume": fields[5].parse::<i64>().unwrap_or(0)},
){
Ok(_) => (),
Err(e) => { // Error is of type rusqlite::Error
eprintln!("Error inserting into database: {}", e);
return Err(From::from(e));
// error for e
// the trait bound `std::io::Error: From<rusqlite::Error>` is not satisfied
// the trait `From<rusqlite::Error>` is not implemented for `std::io::Error`
}
};
} // end of while loop
Ok(((), IoSqlError::IoError(std::io::Error::new(std::io::ErrorKind::Other, "No error"))))
} // end of function
3 posts - 2 participants
🏷️ Rust_feed