无法用Serde反序列化:`?`运算符只能在返回`Result`

时间:2018-04-02 12:09:53

标签: rust deserialization serde serde-json

我正在尝试将json字符串反序列化为结构但是我无法使其工作。我想要做的就是打印出第一个的状态和/或结果,以便与给定的用户名匹配run["meta"]["username"]

然而,尽管已经跟随given example我似乎无法让它发挥作用。现在我宁愿用untyped values做这个,因为我不想存储值,但是当我这样做时,我不能轻易地遍历初始数组,做runs.as_array().iter()似乎返回一个Vec包含一个包含整个json字符串的String元素,因此需要添加输入,但现在这也不起作用。

我得到的当前问题是:

error[E0277]: the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)
  --> src/main.rs:60:30
   |
60 |               let runs: Runs = serde_json::from_str(get_runs(&mut client,
   |  ______________________________^
61 | |                                 &settings["pipeline_id"]).as_str())?;
   | |____________________________________________________________________^ cannot use the `?` operator in a function that returns `()`
   |
   = help: the trait `std::ops::Try` is not implemented for `()`
   = note: required by `std::ops::Try::from_error`

哪个没有意义,因为如果您删除了?,那么会抱怨runs期待Runs,而是收到Result,所以我'我很困惑,它怎么能抱怨它返回Result,然后抱怨它返回()

显然我错过了什么,我只是没有看到它。

第二个问题是我无法使用.iter()

struct Runs(Vec<Run>);

我知道这是一个包裹的Vec,但它不应该自动委托或其他什么?因为我不能使用类型别名或只是:

let runs: Vec<Run> = 

因为我需要:

#[derive(Debug,Serialize,Deserialize)]

之前允许它与serde-rs一起使用。

那么如何解决这两个问题,让我能从json中获取所需的数据呢?

这是一个最小的(生锈)示例:

extern crate serde_json;

#[macro_use]
extern crate serde_derive;

use std::collections::HashMap;


use serde_json::{Value, Error};


#[derive(Debug,Serialize, Deserialize)]
struct Runs(Vec<Run>);

#[derive(Debug,Serialize, Deserialize)]
struct Meta {
    username: String
}

#[derive(Debug,Serialize, Deserialize)]
struct User {
    meta: Meta,
}

#[derive(Debug,Serialize, Deserialize)]
struct Run {
    status: String,
    result: String
    user: User,
}

fn main() {

    let data = r#"
    [
        {
            "id": "577a4dbc3ec144923a02b6f9",
            "url": "https://app.wercker.com/api/v3/runs/577a4dbc3ec144923a02b6f9",
            "branch": "master",
            "commitHash": "cd74a9994712960f19b4b563491f860f98fa7bb5",
            "createdAt": "2016-07-04T11:51:24.851Z",
            "finishedAt": "2016-07-04T11:51:34.284Z",
            "message": "My custom message!",
            "progress": 57,
            "result": "aborted",
            "startedAt": "2016-07-04T11:51:25.996Z",
            "status": "finished",
            "user": {
              "meta": {
                "werckerEmployee": true,
                "username": "tonnu"
              },
              "userId": "549101f36b3ba8733d88ce96",
              "avatar": {
                "gravatar": "bb95c388b227da2ce2f33c0811478095"
              },
              "name": "Toon",
              "type": "wercker"
            },
            "pipeline": {
              "id": "5751377e7e1bd5f17f050bc4",
              "url": "https://app.wercker.com/api/v3/pipelines/5751377e7e1bd5f17f050bc4",
              "createdAt": "2016-06-03T07:53:34.294Z",
              "name": "initial-build",
              "permissions": "public",
              "pipelineName": "initial-build",
              "setScmProviderStatus": true,
              "type": "git"
            }
        }
    ]"#;
    let runs: Runs = serde_json::from_str(data).as_str()?;
    for run in runs.iter() {
        println!("{}","------------");
        println!("{:?}",run)

    }
}

0 个答案:

没有答案