0

私はさびが初めてで、actix_web と sqlx を試し始めました。目標は、シンプルなオープンソースのブログ エンジンを作成することですが、CLI 引数パーサーと基本的な SQL 接続プールを実装した後、コードがコンパイルされなくなりました。次のエラーが表示されます。

error[E0308]: mismatched types
  --> src/main.rs:17:1
   |
17 | #[actix_web::main]
   | ^^^^^^^^^^^^^^^^^^
   | |
   | expected enum `std::result::Result`, found `()`
   | help: try using a variant of the expected enum: `Ok(#[actix_web::main])`
18 | async fn main() -> std::io::Result<()> {
   |                    ------------------- expected `std::result::Result<(), std::io::Error>` because of return type
   |
   = note:   expected enum `std::result::Result<(), std::io::Error>`
           found unit type `()`
   = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `rusty_read`.

To learn more, run the command again with --verbose.

エラーは戻り値の型として使用することを示唆していますstd::result::Result<(), std::io::Error>が、現在の戻り値の型をそれに置き換えると同じエラーが発生します:

error[E0308]: mismatched types
  --> src/main.rs:17:1
   |
17 | #[actix_web::main]
   | ^^^^^^^^^^^^^^^^^^
   | |
   | expected enum `std::result::Result`, found `()`
   | help: try using a variant of the expected enum: `Ok(#[actix_web::main])`
18 | async fn main() -> std::result::Result<(), std::io::Error> {
   |                    --------------------------------------- expected `std::result::Result<(), std::io::Error>` because of return type
   |
   = note:   expected enum `std::result::Result<(), std::io::Error>`
           found unit type `()`
   = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `rusty_read`.

To learn more, run the command again with --verbose.

これまでのところ、同じ問題を抱えている人を見つけることができず、その理由を見つけることができませんでした.

私のコード:

/Cargo.toml

[package]
name = "rusty_read"
version = "0.1.0"
authors = ["LeMoonStar <webmaster@unitcore.de>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-web = "3.0.0-beta.1"
rust-ini = "0.15"
sqlx = { version = "0.4.0-beta.1",  features = [ "all-databases", "any", "tls" ] }
clap = "2"

/src/main.rs

use actix_web::{get, web, App, HttpServer, Responder};
use ini::Ini;
use sqlx::pool::Pool;
use sqlx::any;


#[get("/")]
async fn index() -> impl Responder {
    format!("index")
}

#[get("/article/{id}")]
async fn article(info: web::Path<u32>) -> impl Responder {
    format!("article nr.{}", info)
}

#[actix_web::main]
async fn main() -> std::result::Result<(), std::io::Error> {
    let conf = Ini::load_from_file("conf.ini").unwrap_or(Ini::load_from_str("[Server]
    bindAddress = \"127.0.0.1:8080\"
    
    [Database]
    url = \"mysql://user:password@localhost/blog\"
    ").unwrap());
    
    let matches = clap::App::new("Rusty Read")
        .version("0.1 INDEV")
        .author("LeMoonStar <webmaster@unitcore.de>")
        .about("a blog engine written in rust")
        .subcommand(clap::SubCommand::with_name("config")
            .about("sets various configurations for the blog")
            .arg(clap::Arg::with_name("address")
                .short("a")
                .long("adress")
                .help("sets the address the http server binds on (eg. 127.0.0.1:8080)")
                .takes_value(true))
            .arg(clap::Arg::with_name("database")
                .short("d")
                .long("database")
                .help("sets the url to the database (eg. mysql://user:password@localhost/blog)")
                .takes_value(true)))
        .subcommand(clap::SubCommand::with_name("init_database")
            .about("Initializes the database which is set in the conf.ini file (or with the config command)"))

        .get_matches();

    if let Some(matches) = matches.subcommand_matches("config") {
        if matches.is_present("address") {
            conf.section(Some("Server")).unwrap()
                .insert("bindAddress", "127.0.0.1:8080");
        }
        if matches.is_present("database") {
            conf.section(Some("Database")).unwrap()
                .insert("url", "mysql://user:password@localhost/blog");
        }
    } else if let Some(matches) = matches.subcommand_matches("init_database") {
        
    } else {
        let mut section = conf.section(Some("Server")).expect("conf.ini requires a [Server] section.");
        let bind_address = section.get("bindAddress").expect("conf.ini's [Server] section requires a bindAdress.");

        section = conf.section(Some("Database")).expect("conf.ini requires a [Database] section.");
        let db_url = section.get("url").expect("conf.ini's [Database] section requires a url.");

        let pool = Pool::<any::Any>::connect(db_url).await.expect("database connection pool could not be created.");

        HttpServer::new(move || {
            App::new()
                .service(article)
                .service(index)
                .data(pool.clone())
            })
            .bind(bind_address).expect("could not bind http server")
            .run()
            .await;
    }
}

この問題が存在する間はプロジェクトの作業を続けることができないため、誰かがこの問題を解決してくれることを願っています。

4

1 に答える 1