1- use std:: collections:: HashMap ;
2-
31use 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,7 +33,7 @@ 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}
@@ -49,16 +50,11 @@ impl LocallyDescribableProposalAction for ApproveGenesisKyc {
4950
5051 fn to_value ( & self ) -> Value {
5152 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- )
53+ . add_array_field ( "principals" , self . principals . clone ( ) )
5954 . build ( )
6055 }
6156}
57+
6258/// A builder for `Value` objects.
6359pub ( crate ) struct ValueBuilder {
6460 fields : HashMap < String , Value > ,
@@ -71,20 +67,22 @@ impl ValueBuilder {
7167 }
7268 }
7369
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- ) ;
70+ pub fn add_field ( mut self , key : impl ToString , value : impl Into < Value > ) -> Self {
71+ self . fields . insert ( key. to_string ( ) , value. into ( ) ) ;
72+ self
8173 }
8274
83- pub fn add_array_field ( mut self , key : impl ToString , values : Vec < Value > ) -> Self {
75+ pub fn add_array_field (
76+ mut self ,
77+ key : impl ToString ,
78+ values : impl IntoIterator < Item = impl Into < Value > > ,
79+ ) -> Self {
8480 self . fields . insert (
85- key,
81+ key. to_string ( ) ,
8682 Value {
87- value : Some ( Array ( ValueArray { values } ) ) ,
83+ value : Some ( Array ( ValueArray {
84+ values : values. into_iter ( ) . map ( Into :: into) . collect ( ) ,
85+ } ) ) ,
8886 } ,
8987 ) ;
9088 self
@@ -98,10 +96,19 @@ impl ValueBuilder {
9896 }
9997}
10098
101- /// Converts a string to a `Value` object.
102- fn string_to_value ( value : String ) -> Value {
103- Value {
104- value : Some ( Text ( value) ) ,
99+ impl From < String > for Value {
100+ fn from ( value : String ) -> Self {
101+ Value {
102+ value : Some ( Text ( value) ) ,
103+ }
104+ }
105+ }
106+
107+ impl From < PrincipalId > for Value {
108+ fn from ( value : PrincipalId ) -> Self {
109+ Value {
110+ value : Some ( Text ( value. to_string ( ) ) ) ,
111+ }
105112 }
106113}
107114
0 commit comments