@@ -47,7 +47,10 @@ mod storage;
4747mod telemetry;
4848mod token;
4949
50+ use std:: fmt;
51+
5052pub use sync15:: DeviceType ;
53+ use url:: Url ;
5154
5255pub use auth:: { AuthorizationInfo , FxaRustAuthState , MetricsParams } ;
5356pub use device:: { AttachedClient , Device , DeviceCapability , LocalDevice } ;
@@ -109,7 +112,7 @@ pub struct FxaConfig {
109112 pub token_server_url_override : Option < String > ,
110113}
111114
112- #[ derive( Clone , Debug ) ]
115+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
113116pub enum FxaServer {
114117 Release ,
115118 Stable ,
@@ -132,6 +135,47 @@ impl FxaServer {
132135 }
133136}
134137
138+ impl From < & Url > for FxaServer {
139+ fn from ( url : & Url ) -> Self {
140+ let origin = url. origin ( ) ;
141+ // Note: we can call unwrap() below because parsing content_url for known servers should
142+ // never fail and `test_from_url` tests this.
143+ if origin == Url :: parse ( Self :: Release . content_url ( ) ) . unwrap ( ) . origin ( ) {
144+ Self :: Release
145+ } else if origin == Url :: parse ( Self :: Stable . content_url ( ) ) . unwrap ( ) . origin ( ) {
146+ Self :: Stable
147+ } else if origin == Url :: parse ( Self :: Stage . content_url ( ) ) . unwrap ( ) . origin ( ) {
148+ Self :: Stage
149+ } else if origin == Url :: parse ( Self :: China . content_url ( ) ) . unwrap ( ) . origin ( ) {
150+ Self :: China
151+ } else if origin == Url :: parse ( Self :: LocalDev . content_url ( ) ) . unwrap ( ) . origin ( ) {
152+ Self :: LocalDev
153+ } else {
154+ Self :: Custom {
155+ url : origin. ascii_serialization ( ) ,
156+ }
157+ }
158+ }
159+ }
160+
161+ /// Display impl
162+ ///
163+ /// This identifies the variant, without recording the URL for custom servers. It's good for
164+ /// Sentry reports when we don't want to give away any PII.
165+ impl fmt:: Display for FxaServer {
166+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
167+ let variant_name = match self {
168+ Self :: Release => "Release" ,
169+ Self :: Stable => "Stable" ,
170+ Self :: Stage => "Stage" ,
171+ Self :: China => "China" ,
172+ Self :: LocalDev => "LocalDev" ,
173+ Self :: Custom { .. } => "Custom" ,
174+ } ;
175+ write ! ( f, "{variant_name}" )
176+ }
177+ }
178+
135179impl FxaConfig {
136180 pub fn release ( client_id : impl ToString , redirect_uri : impl ToString ) -> Self {
137181 Self {
@@ -180,3 +224,29 @@ impl FxaConfig {
180224}
181225
182226uniffi:: include_scaffolding!( "fxa_client" ) ;
227+
228+ #[ cfg( test) ]
229+ mod tests {
230+ use super :: * ;
231+
232+ #[ test]
233+ fn test_from_url ( ) {
234+ let test_cases = [
235+ ( "https://accounts.firefox.com" , FxaServer :: Release ) ,
236+ ( "https://stable.dev.lcip.org" , FxaServer :: Stable ) ,
237+ ( "https://accounts.stage.mozaws.net" , FxaServer :: Stage ) ,
238+ ( "https://accounts.firefox.com.cn" , FxaServer :: China ) ,
239+ ( "http://127.0.0.1:3030" , FxaServer :: LocalDev ) ,
240+ (
241+ "http://my-fxa-server.com" ,
242+ FxaServer :: Custom {
243+ url : "http://my-fxa-server.com" . to_owned ( ) ,
244+ } ,
245+ ) ,
246+ ] ;
247+ for ( content_url, expected_result) in test_cases {
248+ let url = Url :: parse ( content_url) . unwrap ( ) ;
249+ assert_eq ! ( FxaServer :: from( & url) , expected_result) ;
250+ }
251+ }
252+ }
0 commit comments