Skip to content

Commit 42d350a

Browse files
Address feedback
1 parent 9c9d58c commit 42d350a

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

rs/nns/governance/src/proposals/self_describing.rs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use std::collections::HashMap;
2-
31
use crate::pb::v1::{
42
ApproveGenesisKyc, Motion, SelfDescribingProposalAction, Value, ValueArray, ValueMap,
53
value::Value::{Array, Map, Text},
64
};
75

6+
use ic_base_types::PrincipalId;
7+
use std::collections::HashMap;
8+
89
/// A proposal action that can be described locally, without having to call `canister_metadata`
910
/// management canister method to get the candid file of an external canister. Every proposal action
1011
/// except for `ExecuteNnsFunction` should implement this trait.
@@ -32,13 +33,14 @@ impl LocallyDescribableProposalAction for Motion {
3233

3334
fn to_value(&self) -> Value {
3435
ValueBuilder::new()
35-
.add_string_field("motion_text", self.motion_text.clone())
36+
.add_field("motion_text", self.motion_text.clone())
3637
.build()
3738
}
3839
}
3940

4041
impl LocallyDescribableProposalAction for ApproveGenesisKyc {
4142
const TYPE_NAME: &'static str = "Approve Genesis KYC";
43+
4244
const TYPE_DESCRIPTION: &'static str = "When new neurons are created at Genesis, they have \
4345
GenesisKYC=false. This restricts what actions they can perform. Specifically, they cannot spawn \
4446
new neurons, and once their dissolve delays are zero, they cannot be disbursed and their balances \
@@ -49,16 +51,11 @@ impl LocallyDescribableProposalAction for ApproveGenesisKyc {
4951

5052
fn to_value(&self) -> Value {
5153
ValueBuilder::new()
52-
.add_array_field(
53-
"principals".to_string(),
54-
self.principals
55-
.iter()
56-
.map(|principal| string_to_value(principal.to_string()))
57-
.collect(),
58-
)
54+
.add_array_field("principals", self.principals.clone())
5955
.build()
6056
}
6157
}
58+
6259
/// A builder for `Value` objects.
6360
pub(crate) struct ValueBuilder {
6461
fields: HashMap<String, Value>,
@@ -71,20 +68,22 @@ impl ValueBuilder {
7168
}
7269
}
7370

74-
pub fn add_string_field(mut self, key: impl ToString, value: String) -> Self {
75-
self.fields.insert(
76-
key.to_string(),
77-
Value {
78-
value: Some(Text(value.to_string())),
79-
},
80-
);
71+
pub fn add_field(mut self, key: impl ToString, value: impl Into<Value>) -> Self {
72+
self.fields.insert(key.to_string(), value.into());
73+
self
8174
}
8275

83-
pub fn add_array_field(mut self, key: impl ToString, values: Vec<Value>) -> Self {
76+
pub fn add_array_field(
77+
mut self,
78+
key: impl ToString,
79+
values: impl IntoIterator<Item = impl Into<Value>>,
80+
) -> Self {
8481
self.fields.insert(
85-
key,
82+
key.to_string(),
8683
Value {
87-
value: Some(Array(ValueArray { values })),
84+
value: Some(Array(ValueArray {
85+
values: values.into_iter().map(Into::into).collect(),
86+
})),
8887
},
8988
);
9089
self
@@ -98,10 +97,19 @@ impl ValueBuilder {
9897
}
9998
}
10099

101-
/// Converts a string to a `Value` object.
102-
fn string_to_value(value: String) -> Value {
103-
Value {
104-
value: Some(Text(value)),
100+
impl From<String> for Value {
101+
fn from(value: String) -> Self {
102+
Value {
103+
value: Some(Text(value)),
104+
}
105+
}
106+
}
107+
108+
impl From<PrincipalId> for Value {
109+
fn from(value: PrincipalId) -> Self {
110+
Value {
111+
value: Some(Text(value.to_string())),
112+
}
105113
}
106114
}
107115

0 commit comments

Comments
 (0)