forked from o2sh/onefetch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework license module to avoid loading cache for each license
Also, forward store creation errors and use a more functional approach
- Loading branch information
Showing
3 changed files
with
28 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,26 @@ | ||
use askalono::{Store, TextData}; | ||
|
||
use crate::Error; | ||
|
||
type Result<T> = std::result::Result<T, Error>; | ||
|
||
static CACHE_DATA: &[u8] = include_bytes!("../resources/license-cache.bin.gz"); | ||
|
||
pub fn from_text(text: &str) -> Option<String> { | ||
match Store::from_cache(CACHE_DATA) { | ||
Ok(store) => match store.analyze(&TextData::from(text)) { | ||
Ok(license) => Some(license.name), | ||
Err(_) => None, | ||
}, | ||
Err(_) => None, | ||
pub struct Detector { | ||
store: Store, | ||
} | ||
|
||
impl Detector { | ||
pub fn new() -> Result<Self> { | ||
Store::from_cache(CACHE_DATA) | ||
.map(|store| Self { store }) | ||
.map_err(|_| Error::LicenseDetectorError) | ||
} | ||
|
||
pub fn analyze(&self, text: &str) -> Option<String> { | ||
self.store | ||
.analyze(&TextData::from(text)) | ||
.ok() | ||
.map(|license| license.name) | ||
} | ||
} |