@@ -8,6 +8,7 @@ use std::path::PathBuf;
88#[ derive( Debug , Clone , Deserialize ) ]
99pub struct Config {
1010 pub browsers : Vec < BrowserConfig > ,
11+ pub default_browser : Option < String > , // Label of the default browser
1112}
1213
1314#[ derive( Debug , Clone , Deserialize ) ]
@@ -80,6 +81,7 @@ fn create_default_config() -> Config {
8081 patterns: None ,
8182 } ,
8283 ] ,
84+ default_browser : None ,
8385 }
8486}
8587
@@ -103,12 +105,25 @@ fn validate_config(config: &Config) -> Result<(), ConfigError> {
103105 }
104106 }
105107
108+ // Validate default browser if specified
109+ if let Some ( ref default_label) = config. default_browser {
110+ let default_exists = config. browsers . iter ( ) . any ( |b| b. label == * default_label) ;
111+ if !default_exists {
112+ return Err ( ConfigError :: ValidationError ( format ! (
113+ "Default browser '{}' not found in browsers list" ,
114+ default_label
115+ ) ) ) ;
116+ }
117+ }
118+
106119 Ok ( ( ) )
107120}
108121
109122// Find the first browser that matches the given URL based on configured patterns
123+ // If no pattern matches and a default browser is configured, return the default browser
110124pub fn find_browser_for_url ( url : & str , config : & Config ) -> Option < BrowserConfig > {
111- config
125+ // First, try to find a browser with matching patterns
126+ let pattern_match = config
112127 . browsers
113128 . iter ( )
114129 . find ( |b| {
@@ -121,5 +136,22 @@ pub fn find_browser_for_url(url: &str, config: &Config) -> Option<BrowserConfig>
121136 } )
122137 } )
123138 } )
124- . cloned ( )
139+ . cloned ( ) ;
140+
141+ // If a pattern matched, return that browser
142+ if pattern_match. is_some ( ) {
143+ return pattern_match;
144+ }
145+
146+ // If no pattern matched, try to return the default browser
147+ if let Some ( ref default_label) = config. default_browser {
148+ return config
149+ . browsers
150+ . iter ( )
151+ . find ( |b| b. label == * default_label)
152+ . cloned ( ) ;
153+ }
154+
155+ // No pattern match and no default browser configured
156+ None
125157}
0 commit comments