-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement list subcommand and installed tools storage, various other …
…improvements
- Loading branch information
1 parent
1d6e53b
commit 2681e2b
Showing
10 changed files
with
272 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,143 @@ | ||
#![allow(clippy::should_implement_trait)] | ||
#![allow(clippy::inherent_to_string)] | ||
|
||
use std::{collections::BTreeSet, convert::Infallible, str::FromStr, sync::Arc}; | ||
|
||
use dashmap::DashSet; | ||
use semver::Version; | ||
|
||
use crate::tool::{ToolId, ToolSpec}; | ||
|
||
/** | ||
Storage for installed tool specifications. | ||
Can be cheaply cloned while still | ||
referring to the same underlying data. | ||
*/ | ||
#[derive(Debug, Default, Clone)] | ||
pub struct InstalledStorage { | ||
tools: Arc<DashSet<ToolSpec>>, | ||
} | ||
|
||
impl InstalledStorage { | ||
/** | ||
Create a new, **empty** `InstalledStorage`. | ||
*/ | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
/** | ||
Parse the contents of a string into a `InstalledStorage`. | ||
Note that this is not fallible - any invalid | ||
lines or tool specifications will simply be ignored. | ||
This means that, worst case, if the installed storage file is corrupted, | ||
the user will simply have to re-install the tools they want to use. | ||
*/ | ||
pub fn from_str(s: impl AsRef<str>) -> Self { | ||
let tools = s | ||
.as_ref() | ||
.lines() | ||
.filter_map(|line| line.parse::<ToolSpec>().ok()) | ||
.collect::<DashSet<_>>(); | ||
Self { | ||
tools: Arc::new(tools), | ||
} | ||
} | ||
|
||
/** | ||
Add a tool to this `InstalledStorage`. | ||
Returns `true` if the tool was added and not already trusted. | ||
*/ | ||
pub fn add_spec(&self, tool: ToolSpec) -> bool { | ||
self.tools.insert(tool) | ||
} | ||
|
||
/** | ||
Remove a tool from this `InstalledStorage`. | ||
Returns `true` if the tool was previously trusted and has now been removed. | ||
*/ | ||
pub fn remove_spec(&self, tool: &ToolSpec) -> bool { | ||
self.tools.remove(tool).is_some() | ||
} | ||
|
||
/** | ||
Check if a tool is cached in this `InstalledStorage`. | ||
*/ | ||
pub fn is_installed(&self, tool: &ToolSpec) -> bool { | ||
self.tools.contains(tool) | ||
} | ||
|
||
/** | ||
Get a sorted copy of the installed tools in this `InstalledStorage`. | ||
*/ | ||
pub fn all_specs(&self) -> Vec<ToolSpec> { | ||
let mut sorted_tools = self.tools.iter().map(|id| id.clone()).collect::<Vec<_>>(); | ||
sorted_tools.sort(); | ||
sorted_tools | ||
} | ||
|
||
/** | ||
Get a sorted list of all unique tool identifiers in this `InstalledStorage`. | ||
*/ | ||
pub fn all_ids(&self) -> Vec<ToolId> { | ||
let sorted_set = self | ||
.all_specs() | ||
.into_iter() | ||
.map(ToolId::from) | ||
.collect::<BTreeSet<_>>(); | ||
sorted_set.into_iter().collect() | ||
} | ||
|
||
/** | ||
Get a sorted list of all unique versions for a | ||
given tool identifier in this `InstalledStorage`. | ||
*/ | ||
pub fn all_versions_for_id(&self, id: &ToolId) -> Vec<Version> { | ||
let sorted_set = self | ||
.all_specs() | ||
.into_iter() | ||
.filter_map(|spec| { | ||
if ToolId::from(spec.clone()) == *id { | ||
Some(spec.version().clone()) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect::<BTreeSet<_>>(); | ||
sorted_set.into_iter().collect() | ||
} | ||
|
||
/** | ||
Render the contents of this `InstalledStorage` to a string. | ||
This will be a sorted list of all tool specifications, separated by newlines. | ||
*/ | ||
pub fn to_string(&self) -> String { | ||
let mut contents = self | ||
.all_specs() | ||
.into_iter() | ||
.map(|id| id.to_string()) | ||
.collect::<Vec<_>>() | ||
.join("\n"); | ||
contents.push('\n'); | ||
contents | ||
} | ||
} | ||
|
||
impl FromStr for InstalledStorage { | ||
type Err = Infallible; | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
Ok(InstalledStorage::from_str(s)) | ||
} | ||
} | ||
|
||
impl ToString for InstalledStorage { | ||
fn to_string(&self) -> String { | ||
self.to_string() | ||
} | ||
} |
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,8 +1,10 @@ | ||
mod home; | ||
mod installed; | ||
mod load_and_save; | ||
mod result; | ||
mod trust; | ||
|
||
pub use home::Home; | ||
pub use installed::InstalledStorage; | ||
pub use result::{StorageError, StorageResult}; | ||
pub use trust::TrustStorage; |
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
Oops, something went wrong.