File tree 3 files changed +28
-9
lines changed
3 files changed +28
-9
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,8 @@ pub enum Error {
16
16
ReferenceInfoError ,
17
17
/// Image probably doesn't exist or has wrong format
18
18
ImageLoadError ,
19
+ /// Could not initialize the license detector
20
+ LicenseDetectorError ,
19
21
}
20
22
21
23
impl std:: fmt:: Debug for Error {
@@ -29,6 +31,7 @@ impl std::fmt::Debug for Error {
29
31
Error :: BareGitRepo => "Unable to run onefetch on bare git repos" ,
30
32
Error :: ReferenceInfoError => "Error while retrieving reference information" ,
31
33
Error :: ImageLoadError => "Could not load the specified image" ,
34
+ Error :: LicenseDetectorError => "Could not initialize the license detector" ,
32
35
} ;
33
36
write ! ( f, "{}" , content)
34
37
}
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ use std::process::Command;
6
6
use colored:: { Color , ColoredString , Colorize } ;
7
7
use git2:: Repository ;
8
8
use image:: DynamicImage ;
9
- use license;
9
+ use license:: Detector ;
10
10
11
11
use crate :: image_backends;
12
12
use crate :: language:: Language ;
@@ -581,6 +581,8 @@ impl Info {
581
581
}
582
582
583
583
fn get_project_license ( dir : & str ) -> Result < String > {
584
+ let detector = Detector :: new ( ) ?;
585
+
584
586
let output = fs:: read_dir ( dir)
585
587
. map_err ( |_| Error :: ReadDirectory ) ?
586
588
. filter_map ( std:: result:: Result :: ok)
@@ -596,7 +598,8 @@ impl Info {
596
598
} , // TODO: multiple prefixes, like COPYING?
597
599
)
598
600
. filter_map ( |entry| {
599
- license:: from_text ( & fs:: read_to_string ( entry) . unwrap_or_else ( |_| "" . into ( ) ) )
601
+ let contents = fs:: read_to_string ( entry) . unwrap_or_default ( ) ;
602
+ detector. analyze ( & contents)
600
603
} )
601
604
. collect :: < Vec < _ > > ( )
602
605
. join ( ", " ) ;
Original file line number Diff line number Diff line change 1
1
use askalono:: { Store , TextData } ;
2
2
3
+ use crate :: Error ;
4
+
5
+ type Result < T > = std:: result:: Result < T , Error > ;
6
+
3
7
static CACHE_DATA : & [ u8 ] = include_bytes ! ( "../resources/license-cache.bin.gz" ) ;
4
8
5
- pub fn from_text ( text : & str ) -> Option < String > {
6
- match Store :: from_cache ( CACHE_DATA ) {
7
- Ok ( store) => match store. analyze ( & TextData :: from ( text) ) {
8
- Ok ( license) => Some ( license. name ) ,
9
- Err ( _) => None ,
10
- } ,
11
- Err ( _) => None ,
9
+ pub struct Detector {
10
+ store : Store ,
11
+ }
12
+
13
+ impl Detector {
14
+ pub fn new ( ) -> Result < Self > {
15
+ Store :: from_cache ( CACHE_DATA )
16
+ . map ( |store| Self { store } )
17
+ . map_err ( |_| Error :: LicenseDetectorError )
18
+ }
19
+
20
+ pub fn analyze ( & self , text : & str ) -> Option < String > {
21
+ self . store
22
+ . analyze ( & TextData :: from ( text) )
23
+ . ok ( )
24
+ . map ( |license| license. name )
12
25
}
13
26
}
You can’t perform that action at this time.
0 commit comments