11use std:: path:: PathBuf ;
22
33use rust_code_analysis:: { guess_language, metrics, Callback , FuncSpace , ParserTrait , action} ;
4- use serde:: Serialize ;
54
65
76/// Payload containing source code used to compute metrics.
87#[ derive( Debug ) ]
98pub struct MetricsPayload {
10- /// Payload identifier.
11- pub id : String ,
129 /// Source code filename.
1310 pub file_name : String ,
1411 /// Source code used to compute metrics.
@@ -19,29 +16,15 @@ pub struct MetricsPayload {
1916
2017/// Server response containing metrics for every space present in
2118/// the requested source code.
22- #[ derive( Debug , Serialize ) ]
23- pub struct MetricsResponse {
24- /// Server response identifier.
25- pub id : String ,
26- /// Source code programming language.
27- pub language : String ,
28- /// Metrics for every space contained in the requested source code.
29- ///
30- /// If `None`, an error occurred processing the request.
31- pub spaces : Option < FuncSpace > ,
32- }
19+ pub type MetricsResponse = Result < FuncSpace , String > ;
3320
3421/// Server request configuration.
3522#[ derive( Debug ) ]
3623pub struct MetricsCfg {
37- /// Request identifier.
38- pub id : String ,
3924 /// Path to the source file.
4025 pub path : PathBuf ,
4126 /// Flag to consider only unit space metrics.
4227 pub unit : bool ,
43- /// Source code programming language.
44- pub language : String ,
4528}
4629
4730/// Unit structure to implement the `Callback` trait.
@@ -53,7 +36,7 @@ impl Callback for MetricsCallback {
5336
5437 fn call < T : ParserTrait > ( cfg : Self :: Cfg , parser : & T ) -> Self :: Res {
5538 let spaces = metrics ( parser, & cfg. path ) ;
56- let spaces = if cfg. unit {
39+ let spaces =if cfg. unit {
5740 if let Some ( mut spaces) = spaces {
5841 spaces. spaces . clear ( ) ;
5942 Some ( spaces)
@@ -63,43 +46,28 @@ impl Callback for MetricsCallback {
6346 } else {
6447 spaces
6548 } ;
66-
67- MetricsResponse {
68- id : cfg. id ,
69- language : cfg. language ,
70- spaces,
71- }
49+ spaces. map_or ( Err ( "Failed to compute metrics" . to_string ( ) ) , Ok )
7250 }
7351}
7452
75- #[ derive( Debug ) ]
76- pub struct Error {
77- pub id : String ,
78- pub error : & ' static str ,
79- }
8053
81- pub fn metrics_rust ( payload : MetricsPayload ) -> Result < MetricsResponse , Error > {
54+ pub fn metrics_rust ( payload : MetricsPayload ) -> MetricsResponse {
8255 let path = PathBuf :: from ( & payload. file_name ) ;
8356 let buf = payload. code . into_bytes ( ) ;
84- let ( language, name ) = guess_language ( & buf, & path) ;
57+ let ( language, _name ) = guess_language ( & buf, & path) ;
8558 if let Some ( language) = language {
8659 let cfg = MetricsCfg {
87- id : payload. id ,
8860 path,
8961 unit : payload. unit ,
90- language : name. to_string ( ) ,
9162 } ;
92- Ok ( action :: < MetricsCallback > (
63+ action :: < MetricsCallback > (
9364 & language,
9465 buf,
9566 & PathBuf :: from ( "" ) ,
9667 None ,
9768 cfg,
98- ) )
69+ )
9970 } else {
100- Err ( Error {
101- id : payload. id ,
102- error : "The file extension doesn't correspond to a valid language" ,
103- } )
71+ Err ( "The file extension doesn't correspond to a valid language" . to_string ( ) )
10472 }
10573}
0 commit comments