如何为Struct实现`std :: iter :: FromIterator <_>'?

问题描述

当我尝试使用Diesel vie MysqL对图进行一对多关系查询时,使用fromIterator查询枚举失败。 帮助显示我需要实现`std :: iter :: FromIterator <_>,但是我不知道如何精确实现该结构。 有帮助吗? 预先感谢!

错误日志

[package]
name = "lead_generation_api"
version = "0.1.0"
authors = ["ARBOR.JP"]
edition = "2018"

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

[dependencies]
diesel = { version = "1.4.5",features = ["MysqL","r2d2","chrono"] }
dotenv = "~0.15"
serde = "~1.0"
serde_derive = "~1.0"
serde_json = "~1.0"
chrono = "~0.4"
rand = "0.7.3"
actix-web = "1.0.9"
actix-cors = "0.1.0"
juniper = "0.14.1"
juniper-from-schema = "0.5.1"
juniper-eager-loading = "0.5.0"
r2d2_MysqL = "*"
r2d2-diesel = "0.16.0"
MysqL = "*"
r2d2 = "*"
futures01 = "0.1.29"
itertools = "0.8.2"

[[bin]]
name = "main"
path = "src/main.rs"

cargo.toml

use std::convert::From;
use std::sync::Arc;
use chrono::NaiveDateTime; 
use actix_web::{web,Error,HttpResponse};
use futures01::future::Future;

use juniper::http::playground::playground_source;
use juniper::{http::GraphQLRequest,GraphQLObject,Executor,FieldResult,FieldError,ID};
use juniper_from_schema::graphql_schema_from_file;

use diesel::prelude::*;

use itertools::Itertools;
use crate::schema::{customers,contact_contents};

use crate::{DbCon,DbPool};

graphql_schema_from_file!("src/schema.graphql");

pub struct Context {
    db_con: DbCon,}
impl juniper::Context for Context {}

pub struct Query;
pub struct Mutation;

impl QueryFields for Query {
    fn field_customers(
        &self,executor: &Executor<'_,Context>,_trail: &QueryTrail<'_,Customer,Walked>,) -> FieldResult<Vec<Customer>> {
        //type FieldResult<T> = Result<T,String>;

        customers::table
            .load::<crate::models::Customer>(&executor.context().db_con)
            .and_then(|customers| Ok(customers.into_iter().map_into().collect()))
            .map_err(Into::into)
    }
}

impl MutationFields for Mutation {
    fn field_create_customer(
        &self,name: String,email: String,contact_contents: Vec<String>,) -> FieldResult<Customer> {
        //type FieldResult<T> = Result<T,String>;

        let new_customer = crate::models::NewCustomer { name: name,email: email};

        diesel::insert_into(customers::table)
            .values(&new_customer)
            .execute(&executor.context().db_con);
            
        customers::table
            .first::<crate::graphql::Customer>(&executor.context().db_con)
            .map_err(Into::into)
    }
}

//#[derive(GraphQLObject)]
//#[graphql_object(name="customer name",email ="mail address")]
#[derive(Queryable)]
pub struct Customer {
    id: u64,created_at: NaiveDateTime,updated_at: NaiveDateTime,}

#[derive(Queryable)]
pub struct ContactContent {
        id: u64,customer_id: u64,}
    
impl ContactContentFields for ContactContent {
    fn field_id(&self,_: &Executor<'_,Context>) -> FieldResult<juniper::ID> {
        Ok(juniper::ID::new(self.id.to_string()))
    }

    fn field_customer_id(&self,Context>) -> FieldResult<juniper::ID> {
        Ok(juniper::ID::new(self.customer_id.to_string()))
    }

    fn field_email(&self,Context>) -> FieldResult<&String> {
        Ok(&self.email)
    }
}

impl From<crate::models::ContactContent> for ContactContent {
    fn from(contact_contents: crate::models::ContactContent) -> Self {
        Self {
            id: contact_contents.id,customer_id: contact_contents.customer_id,email: contact_contents.email,}
    }
}
    
//#[juniper::object(Context = Context)]
//#[graphql(description = "A Photo returns struct")]
impl CustomerFields for Customer {
    fn field_id(&self,Context>) -> FieldResult<juniper::ID> {
        Ok(juniper::ID::new(self.id.to_string()))
    }

    fn field_name(&self,Context>) -> FieldResult<&String> {
        Ok(&self.name)
    }
    fn field_email(&self,Context>) -> FieldResult<&String> {
        Ok(&self.email)
    }
    fn field_contact_contents(
            &self,ContactContent,) -> FieldResult<&Vec<ContactContent>> {
            contact_contents::table
                .filter(contact_contents::customer_id.eq(&self.id))
                .load::<crate::models::ContactContent>(&executor.context().db_con)
                .and_then(|contact_contents| Ok(contact_contents.into_iter().map_into().collect()))
                .map_err(Into::into)
    }
}

impl From<crate::models::Customer> for Customer {
    fn from(customer: crate::models::Customer) -> Self {
        Self {
            id: customer.id,name: customer.name,email: customer.email,created_at: customer.created_at,updated_at: customer.updated_at,}
    }
}

fn playground() -> HttpResponse {
    let html = playground_source("");
    HttpResponse::Ok()
        .content_type("text/html; charset=utf-8")
        .body(html)
}

fn graphql(
    schema: web::Data<Arc<Schema>>,data: web::Json<GraphQLRequest>,db_pool: web::Data<DbPool>,) -> impl Future<Item = HttpResponse,Error = Error> {
    let ctx = Context {
        db_con: db_pool.get().unwrap(),};

    web::block(move || {
        let res = data.execute(&schema,&ctx);
        Ok::<_,serde_json::error::Error>(serde_json::to_string(&res)?)
    })
    .map_err(Error::from)
    .and_then(|customer| {
        Ok(HttpResponse::Ok()
            .content_type("application/json")
            .body(customer))
    })
}

pub fn register(config: &mut web::ServiceConfig) {
    let schema = std::sync::Arc::new(Schema::new(Query,Mutation));

    config
        .data(schema)
        .route("/",web::post().to_async(graphql))
        .route("/",web::get().to(playground));
}

src / graphql.rs

table! {
    contact_contents (id) {
        id -> Unsigned<Bigint>,customer_id -> Unsigned<Bigint>,email -> Varchar,}
}

table! {
    customers (id) {
        id -> Unsigned<Bigint>,name -> Varchar,created_at -> Timestamp,updated_at -> Timestamp,}
}

src / schema.rs

schema {
  query: Query
  mutation: Mutation
}

type Query {
  customers: [Customer!]! @juniper(ownership: "owned")
}

type Mutation {
  createCustomer(
    name: String!
    email: String!
    contactContents: [String!]! 
  ): Customer! @juniper(ownership: "owned")
}

type Customer {
  id: ID! @juniper(ownership: "owned")
  name: String!
  email: String!
  contactContents: [ContactContent!]!
}

type ContactContent {
  id: ID! @juniper(ownership: "owned")
  customerId: ID! @juniper(ownership: "owned")
  email: String!
 }

src / schema.graphql

use chrono::NaiveDateTime; 
pub struct Query;
pub struct Mutation;


#[derive(Queryable,Identifiable,AsChangeset,Clone,PartialEq,Debug)]
pub struct Customer {
    pub id: u64,pub name: String,pub email: String,pub created_at: NaiveDateTime,pub updated_at: NaiveDateTime,}

use super::schema::customers;
#[derive(Queryable,Insertable,AsChangeset)]
#[table_name="customers"]
pub struct NewCustomer {
    pub name: String,}

use super::schema::contact_contents;
#[derive(Queryable,Identifiable)]
#[table_name="contact_contents"]
pub struct ContactContent {
    pub id: u64,pub customer_id: u64,}

src / models.rs

#[macro_use]
extern crate diesel;
extern crate r2d2;
use actix_cors::Cors;
use actix_web::{web,App,HttpServer};

use diesel::{
    prelude::*,};
use diesel::r2d2::ConnectionManager;

pub mod graphql;
pub mod models;
pub mod schema;

pub type DbPool = r2d2::Pool<ConnectionManager<MysqLConnection>>;
pub type DbCon = r2d2::PooledConnection<ConnectionManager<MysqLConnection>>;

fn main() {
    let db_pool = create_db_pool();
    let port: u16 = std::env::var("PORT")
        .ok()
        .and_then(|p| p.parse().ok())
        .unwrap_or(8080);

    let addr = std::net::SocketAddr::from(([0,0],port));

    HttpServer::new(move || {
        App::new()
            .data(db_pool.clone())
            .wrap(Cors::new())
            .configure(graphql::register)
            .default_service(web::to(|| "404"))
    })
    .bind(addr)
    .unwrap()
    .run()
    .unwrap();
}

pub fn create_db_pool() -> DbPool {
    let manager = ConnectionManager::<MysqLConnection>::new("MysqL://root:example@MysqL/test");
    r2d2::Pool::new(manager).expect("Failed to create DB Pool")
}

src / main.rs

RXCIE0

解决方法

没有什么可实现的,这里的问题是from azure.storage.blob import BlobClient,BlobServiceClient import pandas as pd from io import StringIO account_url = "XXXXXX" containerName = "XXXXXX" blob_name = 'Abc.xlsx' storageKey = "XXXXX" connection_string = "XXXX" blob_service_client = BlobServiceClient.from_connection_string(connection_string) blob_client = blob_service_client.get_blob_client(container=containerName,blob=blob_name) blob = blob_client.download_blob().content_as_text(encoding=None) engine = 'xlrd' df = pd.read_excel(io = blob,engine = engine) 的键入不正确:raise ValueError('Must explicitly set engine if not passing in' ValueError: Must explicitly set engine if not passing in buffer or path for io. 意味着它将以某种方式返回其参数之一的成员。

由于您不是从某个地方借用vec,而是从方法内部的整个布料创建vec,这没有任何意义,因此该方法应返回正确的field_contact_contents

这是一个无关紧要的复制案例:https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8f0b6341f7ffd72fe8de334a098295a1

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...