Skip to content

Commit dd1e480

Browse files
committed
Rework license module to avoid loading cache for each license
Also, forward store creation errors and use a more functional approach
1 parent 4d35e5a commit dd1e480

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

src/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub enum Error {
1616
ReferenceInfoError,
1717
/// Image probably doesn't exist or has wrong format
1818
ImageLoadError,
19+
/// Could not initialize the license detector
20+
LicenseDetectorError,
1921
}
2022

2123
impl std::fmt::Debug for Error {
@@ -29,6 +31,7 @@ impl std::fmt::Debug for Error {
2931
Error::BareGitRepo => "Unable to run onefetch on bare git repos",
3032
Error::ReferenceInfoError => "Error while retrieving reference information",
3133
Error::ImageLoadError => "Could not load the specified image",
34+
Error::LicenseDetectorError => "Could not initialize the license detector",
3235
};
3336
write!(f, "{}", content)
3437
}

src/info.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::process::Command;
66
use colored::{Color, ColoredString, Colorize};
77
use git2::Repository;
88
use image::DynamicImage;
9-
use license;
9+
use license::Detector;
1010

1111
use crate::image_backends;
1212
use crate::language::Language;
@@ -581,6 +581,8 @@ impl Info {
581581
}
582582

583583
fn get_project_license(dir: &str) -> Result<String> {
584+
let detector = Detector::new()?;
585+
584586
let output = fs::read_dir(dir)
585587
.map_err(|_| Error::ReadDirectory)?
586588
.filter_map(std::result::Result::ok)
@@ -596,7 +598,8 @@ impl Info {
596598
}, // TODO: multiple prefixes, like COPYING?
597599
)
598600
.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)
600603
})
601604
.collect::<Vec<_>>()
602605
.join(", ");

src/license.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
use askalono::{Store, TextData};
22

3+
use crate::Error;
4+
5+
type Result<T> = std::result::Result<T, Error>;
6+
37
static CACHE_DATA: &[u8] = include_bytes!("../resources/license-cache.bin.gz");
48

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)
1225
}
1326
}

0 commit comments

Comments
 (0)