@@ -75,31 +75,34 @@ impl LintPlugins {
7575 }
7676}
7777
78- impl From < & str > for LintPlugins {
79- fn from ( value : & str ) -> Self {
78+ impl TryFrom < & str > for LintPlugins {
79+ type Error = ( ) ;
80+
81+ fn try_from ( value : & str ) -> Result < Self , Self :: Error > {
8082 match value {
81- "react" | "react-hooks" | "react_hooks" => LintPlugins :: REACT ,
82- "unicorn" => LintPlugins :: UNICORN ,
83+ "react" | "react-hooks" | "react_hooks" => Ok ( LintPlugins :: REACT ) ,
84+ "unicorn" => Ok ( LintPlugins :: UNICORN ) ,
8385 "typescript" | "typescript-eslint" | "typescript_eslint" | "@typescript-eslint" => {
84- LintPlugins :: TYPESCRIPT
86+ Ok ( LintPlugins :: TYPESCRIPT )
8587 }
8688 // deepscan for backwards compatibility. Those rules have been moved into oxc
87- "oxc" | "deepscan" => LintPlugins :: OXC ,
89+ "oxc" | "deepscan" => Ok ( LintPlugins :: OXC ) ,
8890 // import-x has the same rules but better performance
89- "import" | "import-x" => LintPlugins :: IMPORT ,
90- "jsdoc" => LintPlugins :: JSDOC ,
91- "jest" => LintPlugins :: JEST ,
92- "vitest" => LintPlugins :: VITEST ,
93- "jsx-a11y" | "jsx_a11y" => LintPlugins :: JSX_A11Y ,
94- "nextjs" => LintPlugins :: NEXTJS ,
95- "react-perf" | "react_perf" => LintPlugins :: REACT_PERF ,
96- "promise" => LintPlugins :: PROMISE ,
97- "node" => LintPlugins :: NODE ,
98- "regex" => LintPlugins :: REGEX ,
99- "vue" => LintPlugins :: VUE ,
91+ "import" | "import-x" => Ok ( LintPlugins :: IMPORT ) ,
92+ "jsdoc" => Ok ( LintPlugins :: JSDOC ) ,
93+ "jest" => Ok ( LintPlugins :: JEST ) ,
94+ "vitest" => Ok ( LintPlugins :: VITEST ) ,
95+ "jsx-a11y" | "jsx_a11y" => Ok ( LintPlugins :: JSX_A11Y ) ,
96+ "nextjs" => Ok ( LintPlugins :: NEXTJS ) ,
97+ "react-perf" | "react_perf" => Ok ( LintPlugins :: REACT_PERF ) ,
98+ "promise" => Ok ( LintPlugins :: PROMISE ) ,
99+ "node" => Ok ( LintPlugins :: NODE ) ,
100+ "regex" => Ok ( LintPlugins :: REGEX ) ,
101+ "vue" => Ok ( LintPlugins :: VUE ) ,
100102 // "eslint" is not really a plugin, so it's 'empty'. This has the added benefit of
101103 // making it the default value.
102- _ => LintPlugins :: empty ( ) ,
104+ "eslint" => Ok ( LintPlugins :: ESLINT ) ,
105+ _ => Err ( ( ) ) ,
103106 }
104107 }
105108}
@@ -134,15 +137,11 @@ impl<'de> Deserialize<'de> for LintPlugins {
134137 let mut lint_plugins = LintPlugins :: empty ( ) ;
135138
136139 for plugin in & plugin_names {
137- if plugin == "eslint" {
138- continue ;
139- }
140-
141- let plugin_flag = LintPlugins :: from ( plugin. as_str ( ) ) ;
142- if plugin_flag == LintPlugins :: empty ( ) {
140+ if let Ok ( plugin_flag) = LintPlugins :: try_from ( plugin. as_str ( ) ) {
141+ lint_plugins |= plugin_flag;
142+ } else {
143143 return Err ( serde:: de:: Error :: custom ( format ! ( "Unknown plugin: '{plugin}'." ) ) ) ;
144144 }
145- lint_plugins |= plugin_flag;
146145 }
147146
148147 Ok ( lint_plugins)
@@ -243,10 +242,10 @@ mod tests {
243242
244243 #[ test]
245244 fn test_plugin_from_str ( ) {
246- assert_eq ! ( LintPlugins :: from ( "react" ) , LintPlugins :: REACT ) ;
247- assert_eq ! ( LintPlugins :: from ( "typescript-eslint" ) , LintPlugins :: TYPESCRIPT ) ;
248- assert_eq ! ( LintPlugins :: from ( "deepscan" ) , LintPlugins :: OXC ) ;
249- assert_eq ! ( LintPlugins :: from ( "unknown" ) , LintPlugins :: empty ( ) ) ;
245+ assert_eq ! ( LintPlugins :: try_from ( "react" ) , Ok ( LintPlugins :: REACT ) ) ;
246+ assert_eq ! ( LintPlugins :: try_from ( "typescript-eslint" ) , Ok ( LintPlugins :: TYPESCRIPT ) ) ;
247+ assert_eq ! ( LintPlugins :: try_from ( "deepscan" ) , Ok ( LintPlugins :: OXC ) ) ;
248+ assert_eq ! ( LintPlugins :: try_from ( "unknown" ) , Err ( ( ) ) ) ;
250249 }
251250
252251 #[ test]
0 commit comments