@@ -13,6 +13,7 @@ use crate::{
1313 from_env:: { EnvItemInfo , FromEnv , FromEnvErr , FromEnvVar } ,
1414 } ,
1515} ;
16+ use serde:: { Deserialize , Deserializer } ;
1617
1718/// The builder list env var.
1819const BUILDERS : & str = "PERMISSIONED_BUILDERS" ;
@@ -57,12 +58,19 @@ pub enum BuilderConfigError {
5758}
5859
5960/// An individual builder.
60- #[ derive( Clone , Debug ) ]
61+ #[ derive( Clone , Debug , serde:: Deserialize ) ]
62+ #[ serde( from = "String" ) ]
6163pub struct Builder {
6264 /// The sub of the builder.
6365 pub sub : String ,
6466}
6567
68+ impl From < String > for Builder {
69+ fn from ( sub : String ) -> Self {
70+ Self { sub }
71+ }
72+ }
73+
6674impl Builder {
6775 /// Create a new builder.
6876 pub fn new ( sub : impl AsRef < str > ) -> Self {
@@ -78,19 +86,32 @@ impl Builder {
7886}
7987
8088/// Builders struct to keep track of the builders that are allowed to perform actions.
81- #[ derive( Clone , Debug ) ]
89+ #[ derive( Clone , Debug , serde :: Deserialize ) ]
8290pub struct Builders {
8391 /// The list of builders.
8492 ///
8593 /// This is configured in the environment variable `PERMISSIONED_BUILDERS`,
8694 /// as a list of comma-separated UUIDs.
95+ #[ serde( deserialize_with = "deser_builders" ) ]
8796 pub builders : Vec < Builder > ,
8897
8998 /// The slot authorization configuration. See [`SlotAuthzConfig`] for more
9099 /// information and env vars
91100 config : SlotAuthzConfig ,
92101}
93102
103+ fn deser_builders < ' de , D > ( deser : D ) -> Result < Vec < Builder > , D :: Error >
104+ where
105+ D : Deserializer < ' de > ,
106+ {
107+ let s = String :: deserialize ( deser) ?;
108+ Ok ( split_builders ( & s) )
109+ }
110+
111+ fn split_builders ( s : & str ) -> Vec < Builder > {
112+ s. split ( ',' ) . map ( Builder :: new) . collect ( )
113+ }
114+
94115impl Builders {
95116 /// Create a new Builders struct.
96117 pub const fn new ( builders : Vec < Builder > , config : SlotAuthzConfig ) -> Self {
@@ -188,7 +209,7 @@ impl FromEnv for Builders {
188209 fn from_env ( ) -> Result < Self , FromEnvErr < Self :: Error > > {
189210 let s = String :: from_env_var ( BUILDERS )
190211 . map_err ( FromEnvErr :: infallible_into :: < BuilderConfigError > ) ?;
191- let builders = s . split ( ',' ) . map ( Builder :: new ) . collect ( ) ;
212+ let builders = split_builders ( & s ) ;
192213
193214 let config = SlotAuthzConfig :: from_env ( ) . map_err ( FromEnvErr :: from) ?;
194215
0 commit comments