Rust 误输入 csv 文件

Rust mistyping for csv reading

use walkdir::WalkDir;
use std::ffi::OsStr;
use rusqlite::{params, Connection, Result};
use std::error::Error;
use std::io;
use std::process;
use std::path::Path;
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Trade {
    id: usize,
    price: f64,
    quantity: f64,
    quoted_quantity: f64,
    time: i64,
    is_buyer_maker: bool,
    is_best_match: bool,
}

fn process_csv(file: &Path, db: &Connection) -> Result<(), Box<dyn Error>> {
    let mut rdr = csv::Reader::from_path(file)?;
    for result in rdr.records() {
        match result.deserialize::<Trade>() {
            Ok(s) => {

                db.execute("INSERT INTO trades (id, price, quantity, quoted_quantity, time, is_buyer_maker, is_best_match) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
                           params![s.id, s.price, s.quantity, s.quoted_quantity, s.time, s.is_buyer_maker, s.is_best_match])?;
            },
            Err(e) => println!("Failed to deserialize: {}", e),
        }
    }
}

fn main () -> Result<(), Box<dyn Error>> {
    let conn = Connection::open("bot.db")?;
    conn.execute(
        "CREATE TABLE trades (
                  id              INTEGER PRIMARY KEY,
                  price           REAL NOT NULL,
                  quanity         REAL NOT NULL,
                  quoted_quatity  REAL NOT NULL,
                  time            INTEGER NOT NULL,
                  is_buyer_maker  INTEGER NOT NULL,
                  is_best_match   INTEGER NOT NULL,
                  )",
        [],
    )?;
    for entry in WalkDir::new("E:/binance-public-data/python/data/spot/monthly/trades/").into_iter().filter_map(|e| e.ok()) {
        println!("Processing: {}", entry.path().display());
        if let Err(e) = process_csv(entry.path(), &conn) {
            println!("Processing failed: {}", e);
        }
    }
    Ok(())
}
error[E0599]: no method named `deserialize` found for enum `Result` in the current scope
   --> src/lib.rs:24:22
    |
24  |         match result.deserialize::<Trade>() {
    |                      ^^^^^^^^^^^ this is an associated function, not a method
    |
    = note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: the candidate is defined in the trait `_::_serde::Deserialize`
   --> /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.127/src/de/mod.rs:537:5
    |
537 | /     fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
538 | |     where
539 | |         D: Deserializer<'de>;
    | |_____________________________^
help: use associated function syntax instead
    |
24  |         match Result::<StringRecord, csv::Error>::deserialize::<Trade>() {
    |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: disambiguate the associated function for the candidate
    |
24  |         match _::_serde::Deserialize::deserialize(result) {
    |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
  --> src/lib.rs:23:5
   |
21 |   fn process_csv(file: &Path, db: &Connection) -> Result<(), Box<dyn Error>> {
   |                                                   -------------------------- expected `Result<(), Box<(dyn StdError + 'static)>>` because of return type
22 |       let mut rdr = csv::Reader::from_path(file)?;
23 | /     for result in rdr.records() {
24 | |         match result.deserialize::<Trade>() {
25 | |             Ok(s) => {
26 | |
...  |
31 | |         }
32 | |     }
   | |_____^ expected enum `Result`, found `()`
   |
   = note:   expected enum `Result<(), Box<(dyn StdError + 'static)>>`
           found unit type `()`
help: try using a variant of the expected enum
   |
23 |     Ok(for result in rdr.records() {
24 |         match result.deserialize::<Trade>() {
25 |             Ok(s) => {
26 | 
27 |                 db.execute("INSERT INTO trades (id, price, quantity, quoted_quantity, time, is_buyer_maker, is_best_match) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
28 |                            params![s.id, s.price, s.quantity, s.quoted_quantity, s.time, s.is_buyer_maker, s.is_best_match])?;
 ...

我想将我的数据放入sqlite 数据库中。我有一堆 csvs 需要浏览并读取它们的数据。我不知道如何解决这些错误。我已经尝试阅读 csv 文档,但我仍然遇到 return 类型的错误。也不确定如何处理反序列化。

您的第一个错误是因为您在错误的变量上调用了 csv.deserialize(),它应该在 reader[=27 上调用=].变化:

for result in rdr.records() {
    match result.deserialize::<Trade>() {

至:

for result in rdr.deserialize::<Trade>() {
    match result {

你的第二个错误是在你的 process_csv 函数结束时没有返回成功:

fn process_csv(file: &Path, db: &Connection) -> Result<(), Box<dyn Error>> {
    // ...snip...
    
    Ok(()) // <--------
}

Playground