rust-lang-nursery / rust-cookbook

https://rust-lang-nursery.github.io/rust-cookbook
Creative Commons Zero v1.0 Universal
2.25k stars 284 forks source link

Consume a paginated restful - faulty #620

Open mgabs opened 3 years ago

mgabs commented 3 years ago

Link - https://rust-lang-nursery.github.io/rust-cookbook/web/clients/apis.html#consume-a-paginated-restful-api

When i run the example at retrieves the dependencies however, the crate_id is populated with the source crate name.. example.

reverse dependency: Dependencies {
    id: 1512403,
    version_id: 260859,
    crate_id: "serde",
    req: "^1.0",
    optional: false,
}
reverse dependency: Dependencies {
    id: 1171189,
    version_id: 214959,
    crate_id: "serde",
    req: "^1",
    optional: false,
}
reverse dependency: Dependencies {
    id: 1679135,
    version_id: 282866,
    crate_id: "serde",
    req: "^1.0",
    optional: true,
}
reverse dependency: Dependencies {
    id: 1513367,
    version_id: 260957,
    crate_id: "serde",
    req: "^1.0",
    optional: false,
}
reverse dependency: Dependencies {
    id: 1509548,
    version_id: 260570,
    crate_id: "serde",
    req: "^1.0",
    optional: false,
}
reverse dependency: Dependencies {
    id: 1510400,
    version_id: 260674,
    crate_id: "serde",
    req: "^1.0",
    optional: false,
}
reverse dependency: Dependencies {
    id: 1857747,
    version_id: 306796,
    crate_id: "serde",
    req: "^1.0",
    optional: false,
}
reverse dependency: Dependencies {
    id: 221948,
    version_id: 56468,
    crate_id: "serde",
    req: "^1.0",
    optional: false,
}
reverse dependency: Dependencies {
    id: 1005933,
    version_id: 191390,
    crate_id: "serde",
    req: "^1.0.101",
    optional: false,
}
reverse dependency: Dependencies {
    id: 1718978,
    version_id: 288126,
    crate_id: "serde",
    req: "^1.0",
    optional: false,
}
reverse dependency: Dependencies {
    id: 201348,
    version_id: 52572,
    crate_id: "serde",
    req: "^1.0",
    optional: false,
}
reverse dependency: Dependencies {
    id: 502365,
    version_id: 108551,
    crate_id: "serde",
    req: "^1.0",
    optional: false,
}
reverse dependency: Dependencies {
    id: 734888,
    version_id: 149808,
    crate_id: "serde",
    req: "^1.0.91",
    optional: false,
}
reverse dependency: Dependencies {
    id: 1511500,
    version_id: 260772,
    crate_id: "serde",
    req: "^1.0",
    optional: false,
}
reverse dependency: Dependencies {
    id: 656418,
    version_id: 135925,
    crate_id: "serde",
    req: "^1.0",
    optional: false,
}
reverse dependency: Dependencies {
    id: 1482273,
    version_id: 257090,
    crate_id: "serde",
    req: "^1.0.114",
    optional: false,
}
reverse dependency: Dependencies {
    id: 1197240,
    version_id: 218162,
    crate_id: "serde",
    req: "^1.0",
    optional: false,
}
reverse dependency: Dependencies {
    id: 1567895,
    version_id: 268290,
    crate_id: "serde",
    req: "^1.0",
    optional: false,
}
reverse dependency: Dependencies {
    id: 1341760,
    version_id: 237984,
    crate_id: "serde",
    req: "^1.0.106",
    optional: false,
}
reverse dependency: Dependencies {
    id: 1510609,
    version_id: 260697,
    crate_id: "serde",
    req: "^1.0",
    optional: false,
}
reverse dependency: Dependencies {
    id: 1513547,
    version_id: 260972,
    crate_id: "serde",
    req: "^1.0",
    optional: false,
}
reverse dependency: Dependencies {
    id: 682833,
    version_id: 140883,
    crate_id: "serde",
    req: "^1.0",
    optional: false,
}
reverse dependency: Dependencies {
    id: 1368898,
    version_id: 241717,
    crate_id: "serde",
    req: "^1.0.107",
    optional: true,
}
reverse dependency: Dependencies {
    id: 619659,
    version_id: 129517,
    crate_id: "serde",
    req: "^1.0",
    optional: false,
}
reverse dependency: Dependencies {
    id: 989622,
    version_id: 188910,
    crate_id: "serde",
    req: "^1.0.101",
    optional: false,
}
AndyGauge commented 3 years ago

Yep, that's what the api is returning https://crates.io/api/v1/crates/error-chain/reverse_dependencies I thing that's strange field name for the name of the crate.

mgabs commented 3 years ago

Sorry, I don't get your comment. What I am saying is the struct is storing the source crate as a member of ReverDep structs. Which is a faulty behaviour.

Further, I couldn't consume elements from the iter. same example:

   for dep in ReverseDependencies::of("tokio")? {
        // println!("reverse dependency: {:#?}", dep?);
        println!("{:#?}", dep?.crate_id);
        println!("{:#?}", dep?.req);
    }

error[E0382]: use of moved value: `dep`
   --> src/main.rs:109:27
    |
106 |     for dep in ReverseDependencies::of("tokio")? {
    |         --- move occurs because `dep` has type `std::result::Result<Dependencies, reqwest::Error>`, which does not implement the `Copy` trait
107 |         // println!("reverse dependency: {:#?}", dep?);
108 |         println!("{:#?}", dep?.crate_id);
    |                           ---- `dep` moved due to this method call
109 |         println!("{:#?}", dep?.req);
    |                           ^^^ value used here after move
    |
note: this function consumes the receiver `self` by taking ownership of it, which moves `dep`

What is the best way to address that ?

AndyGauge commented 3 years ago

I mean if you go to the link I provided you will see this: {"dependencies":[{"id":987053,"version_id":188578,"crate_id":"error-chain","req":"^0.12","optional":false,"default_features":false,"features":[],"target":null,"kind":"normal","downloads":3135857}

The API returns crate_id = crate name

"crate_id":"error-chain"

error[E0382]: use of moved value: dep When you follow dep by the ? you are performing an operation on it. You can do let depends=dep?; and then println! it twice

mgabs commented 3 years ago
  1. When I run the program it doesn't return the value of the crate_id, instead it returns a copy of the calling argument that had been stored in the RD vector. (Duplicate value).

  2. I tried that approach but the compiler complained about the clone trait not available for type iter.

⁣Regards, Mohammed​

On Dec 1, 2020, 5:37 PM, at 5:37 PM, Andrew Gauger notifications@github.com wrote:

I mean if you go to the link I provided you will see this: {"dependencies":[{"id":987053,"version_id":188578,"crate_id":"error-chain","req":"^0.12","optional":false,"default_features":false,"features":[],"target":null,"kind":"normal","downloads":3135857}

The API returns crate_id = crate name

"crate_id":"error-chain"

error[E0382]: use of moved value: dep When you follow dep by the ? you are performing an operation on it. You can do let depends=dep?; and then println! it twice

-- You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/rust-lang-nursery/rust-cookbook/issues/620#issuecomment-736631875