0

I'm trying to deserialize a json string to a struct but I'm having trouble with getting it to work. All I want to do is print out the status and or result of the first one to match run["meta"]["username"] against a given username.

However despite having followed the given example I can't seem to get it to work. Now I'd much rather do this with untyped values as I'm not intending to store the values but when I do this I can't easily iterate over the initial array, doing runs.as_array().iter() seems to return a Vec with a single String element containing the entire json string, hence needing to add typing but now this isn't working either.

The current problem I get is this:

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`

Which doesn't make sense, because if you remove the ? it then complains that runs was expecting a Runs and instead received a Result, so I'm very confused, how can it complain that it's returning a Result and then complain that it's returning a ()?

Obviously I'm missing something and I'm just not seeing it.

The second issue is that I can't use .iter() on:

struct Runs(Vec<Run>);

I know it's a wrapped Vec, but shouldn't it delegate or something automatically? Because I can't use a type alias or just do:

let runs: Vec<Run> = 

because I need to have:

#[derive(Debug,Serialize, Deserialize)]

before it to allow it to work with serde-rs.

So how do I solve the two issues that will allow me to get the data I need from the json?

Here is a minimal (for rust) example:

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)

    }
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Thermatix
  • 2,757
  • 21
  • 51
  • [One question per post, please](https://meta.stackexchange.com/questions/39223/one-post-with-multiple-questions-or-multiple-posts). It's also highly recommended that you search for any error messages you receive. – Shepmaster Apr 02 '18 at 12:48
  • related: [How do I deserialize a JSON array without needing a wrapper type?](https://stackoverflow.com/q/49139196/155423) – Shepmaster Apr 02 '18 at 12:50
  • 1
    TL;DR: `let runs: Vec = serde_json::from_str(data).unwrap();` – Shepmaster Apr 02 '18 at 12:51

0 Answers0