Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
REVIEW: Use iterator
Erik Kundt committed 6 months ago
commit 08c29fb4bd138c5e1c1db67506ca957e011ce0dd
parent a2b57fb7af81903fd33288fd3234d98af0218de8
2 files changed +15 -15
modified crates/radicle-cli/src/commands/id.rs
@@ -4,6 +4,8 @@ use std::collections::BTreeSet;

use anyhow::{anyhow, Context};

+
use itertools::Itertools;
+

use radicle::cob::identity::{self, IdentityMut, Revision, RevisionId};
use radicle::cob::Title;
use radicle::identity::doc::update;
@@ -161,12 +163,11 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {

                // TODO(erikli): whenever `clap` starts supporting custom value parsers
                // for a series of values, we can parse into `Payload` implicitly.
-
                let payloads = args::Payload::try_parse_many(&payload)?
-
                    .into_iter()
-
                    .map(|p| (p.id, p.key, p.value))
-
                    .collect::<Vec<_>>();
+
                let payloads: Result<_, _> = args::Payload::try_parse_many(&payload)
+
                    .map_ok(|p| (p.id, p.key, p.value))
+
                    .collect();

-
                update::payload(proposal, payloads)?
+
                update::payload(proposal, payloads?)?
            };

            // If `--edit` is specified, the document can also be edited via a text edit.
modified crates/radicle-cli/src/commands/id/args.rs
@@ -43,19 +43,18 @@ impl Payload {
    /// Parses the list of all payload values that were aggregated by `clap`.
    /// E.g. `--payload key name value --payload key name value` will result
    /// in `["key","name","value","key","name","value"]`.
-
    pub(super) fn try_parse_many(values: &[String]) -> Result<Vec<Self>, PayloadParseError> {
+
    pub(super) fn try_parse_many(
+
        values: &[String],
+
    ) -> impl Iterator<Item = Result<Self, PayloadParseError>> + use<'_> {
        // `clap` makes sure we don't have 3 values per option occurence, so we can just
        // chunk the aggregated list
-
        values
-
            .chunks(3)
-
            .map(|chunk| {
-
                Ok(Payload {
-
                    id: PayloadId::from_str(&chunk[0])?,
-
                    key: chunk[1].to_owned(),
-
                    value: json::from_str(&chunk[2].to_owned())?,
-
                })
+
        values.chunks(3).map(|chunk| {
+
            Ok(Payload {
+
                id: PayloadId::from_str(&chunk[0])?,
+
                key: chunk[1].to_owned(),
+
                value: json::from_str(&chunk[2].to_owned())?,
            })
-
            .collect()
+
        })
    }
}