如何转换选项<& str>到BSON?

时间:2016-03-24 18:48:55

标签: rust

这是我的Rust计划:

#[macro_use(bson, doc)]
extern crate bson;
extern crate mongodb;

extern crate hyper;
extern crate regex;

use std::io::Read;

// Hyper
use hyper::Client;
use hyper::header::Connection;

// Regex
use regex::Regex;

// MongoDB
use mongodb::{Client as MongoClient, ThreadedClient};
use mongodb::db::ThreadedDatabase;
use mongodb::error::Result as MongoResult;

// bson
use bson::{Bson, Document};
use bson::oid::ObjectId;

fn main() {
    // Create a client.
    let client = Client::new();

    // Creating an outgoing request.
    let mut res = client.get("https://en.wikipedia.org/wiki/List_of_films:_J%E2%80%93K")
        // set a header
        .header(Connection::close())
        // let 'er go!
        .send().unwrap();

    // Read the Response.
    let mut body = String::new();
    res.read_to_string(&mut body).unwrap();

    // Regex out the li tags with movies names
    let re = Regex::new("<li><i><a href=\"[^\"]+\" title=\"[^\"]+\">(?P<title>[^<]+)").unwrap();
    for caps in re.captures_iter(&body) {
        // Connect to database
        let mclient = MongoClient::connect("localhost", 27017)
                          .ok()
                          .expect("Error establishing connection.");

        // The titles collection
        let coll = mclient.db("rust-test-titles").collection("titles");

        let title = caps.name("title");

        let doc = doc!{"value" => title};

        // Insert our title
        coll.insert_one(doc.clone(), None)
            .ok()
            .expect("Failed to insert document.");
    }
}

当我尝试编译时,我收到错误:

error: the trait `core::convert::From<core::option::Option<&str>>` is not implemented for the type `bson::bson::Bson` [E0277]
<bson macros>:10 $ val : expr ) => { { :: std:: convert:: From:: from ( $ val ) } } ;

如何转换title以便bson不会抛出错误?

0 个答案:

没有答案