@@ -36,12 +36,14 @@ use async_lsp::lsp_types::{
3636use async_lsp:: router:: Router ;
3737use async_lsp:: { ClientSocket , LanguageClient , LanguageServer , ResponseError } ;
3838use futures:: future:: BoxFuture ;
39- use serde :: Deserialize ;
39+ use serde_json :: from_value ;
4040
4141use crate :: documents:: storage:: DocumentStorage ;
4242use crate :: features:: code_action:: code_actions;
4343use crate :: features:: completion:: completion;
44- use crate :: features:: diagnostics:: diagnostics;
44+ use crate :: features:: diagnostics:: {
45+ diagnostics, MetadataValidationRule , Settings ,
46+ } ;
4547use crate :: features:: document_highlight:: document_highlight;
4648use crate :: features:: document_symbol:: document_symbol;
4749use crate :: features:: formatting:: formatting;
@@ -55,20 +57,6 @@ use crate::features::semantic_tokens::{
5557 semantic_tokens, SEMANTIC_TOKEN_MODIFIERS , SEMANTIC_TOKEN_TYPES ,
5658} ;
5759
58- /// Rule that describes a how to validate a metadata entry in a rule.
59- #[ derive( Deserialize , Debug , Clone , Default ) ]
60- #[ serde( rename_all = "camelCase" ) ]
61- pub struct MetadataValidationRule {
62- /// Metadata identifier
63- pub identifier : String ,
64- /// Whether the metadata entry is required or not.
65- #[ serde( default ) ]
66- pub required : bool ,
67- /// Type of the metadata entry.
68- #[ serde( rename = "type" ) ]
69- pub ty : Option < String > ,
70- }
71-
7260/// Represents a YARA language server.
7361pub struct YARALanguageServer {
7462 /// Client socket for communication with the Development Tool.
@@ -426,17 +414,15 @@ impl LanguageServer for YARALanguageServer {
426414 let client = self . client . clone ( ) ;
427415
428416 Box :: pin ( async move {
429- let meta_specs =
430- Self :: get_meta_validation_rules ( client. clone ( ) , uri. clone ( ) )
431- . await ;
417+ let settings = Self :: get_settings ( client, uri. clone ( ) ) . await ;
432418
433419 Ok ( DocumentDiagnosticReportResult :: Report (
434420 async_lsp:: lsp_types:: DocumentDiagnosticReport :: Full (
435421 RelatedFullDocumentDiagnosticReport {
436422 full_document_diagnostic_report :
437423 FullDocumentDiagnosticReport {
438424 result_id : None ,
439- items : diagnostics ( documents, uri, meta_specs ) ,
425+ items : diagnostics ( documents, uri, settings ) ,
440426 } ,
441427 related_documents : None ,
442428 } ,
@@ -563,23 +549,39 @@ impl YARALanguageServer {
563549 } )
564550 }
565551
566- async fn get_meta_validation_rules (
552+ async fn get_settings (
567553 mut client : ClientSocket ,
568554 scope_uri : Url ,
569- ) -> Vec < MetadataValidationRule > {
555+ ) -> Settings {
570556 client
571557 . configuration ( ConfigurationParams {
572- items : vec ! [ ConfigurationItem {
573- scope_uri: Some ( scope_uri) ,
574- section: Some ( "YARA.metadataValidation" . to_string( ) ) ,
575- } ] ,
558+ items : vec ! [
559+ ConfigurationItem {
560+ scope_uri: Some ( scope_uri. clone( ) ) ,
561+ section: Some ( "YARA.metadataValidation" . to_string( ) ) ,
562+ } ,
563+ ConfigurationItem {
564+ scope_uri: Some ( scope_uri) ,
565+ section: Some ( "YARA.ruleNameValidation" . to_string( ) ) ,
566+ } ,
567+ ] ,
576568 } )
577569 . await
578570 . ok ( )
579- . and_then ( |mut v| v. pop ( ) )
580- . and_then ( |value| {
581- serde_json:: from_value :: < Vec < MetadataValidationRule > > ( value)
582- . ok ( )
571+ . map ( |mut v| {
572+ let rule_name_validation = v
573+ . pop ( )
574+ . and_then ( |value| from_value :: < Option < String > > ( value) . ok ( ) )
575+ . unwrap_or_default ( ) ;
576+
577+ let metadata_validation = v
578+ . pop ( )
579+ . and_then ( |value| {
580+ from_value :: < Vec < MetadataValidationRule > > ( value) . ok ( )
581+ } )
582+ . unwrap_or_default ( ) ;
583+
584+ Settings { metadata_validation, rule_name_validation }
583585 } )
584586 . unwrap_or_default ( )
585587 }
@@ -592,19 +594,12 @@ impl YARALanguageServer {
592594 let uri = uri. clone ( ) ;
593595
594596 tokio:: spawn ( async move {
595- let meta_validation_rules = Self :: get_meta_validation_rules (
596- client. clone ( ) ,
597- uri. clone ( ) ,
598- )
599- . await ;
597+ let settings =
598+ Self :: get_settings ( client. clone ( ) , uri. clone ( ) ) . await ;
600599
601600 client. publish_diagnostics ( PublishDiagnosticsParams {
602601 uri : uri. clone ( ) ,
603- diagnostics : diagnostics (
604- documents,
605- uri,
606- meta_validation_rules,
607- ) ,
602+ diagnostics : diagnostics ( documents, uri, settings) ,
608603 version : None ,
609604 } )
610605 } ) ;
0 commit comments