Skip to content

Commit

Permalink
Make sources case-insensitive; closes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
LPGhatguy committed Feb 19, 2020
1 parent 4cca3ba commit a57ba51
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
3 changes: 2 additions & 1 deletion foreman.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[tools]
rojo = { source = "rojo-rbx/rojo", version = "0.5.0" }
rojo = { source = "rojo-rbx/rojo", version = "0.5.0" }
rojo2 = { source = "rOjO-rBx/rOjo", version = "0.5.0" }
45 changes: 45 additions & 0 deletions src/ci_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::{
fmt,
hash::{Hash, Hasher},
};

use serde::{Deserialize, Serialize};

/// Case-insensitive string.
///
/// A string that acts case-insensitive when compared or hashed, but preserves
/// its case.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(transparent)]
pub struct CiString(pub String);

impl fmt::Display for CiString {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(&self.0)
}
}

impl Hash for CiString {
fn hash<H: Hasher>(&self, state: &mut H) {
let mut buf = [0; 4];
for outer in self.0.chars() {
for normalized in outer.to_lowercase() {
normalized.encode_utf8(&mut buf);
state.write(&buf);
}
}

state.write_u8(0xff);
}
}

impl PartialEq for CiString {
fn eq(&self, other: &Self) -> bool {
self.0
.chars()
.flat_map(char::to_lowercase)
.eq(other.0.chars().flat_map(char::to_lowercase))
}
}

impl Eq for CiString {}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod aliaser;
mod artifact_choosing;
mod auth_store;
mod ci_string;
mod config;
mod fs;
mod github;
Expand Down
7 changes: 4 additions & 3 deletions src/tool_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use zip::ZipArchive;

use crate::{
artifact_choosing::platform_keywords,
ci_string::CiString,
fs::{self, File},
github, paths,
};
Expand All @@ -25,7 +26,7 @@ fn index_file() -> PathBuf {
/// Contains the current state of all of the tools that Foreman manages.
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct ToolCache {
pub tools: HashMap<String, ToolEntry>,
pub tools: HashMap<CiString, ToolEntry>,
}

impl ToolCache {
Expand All @@ -47,7 +48,7 @@ impl ToolCache {
pub fn download_if_necessary(source: &str, version_req: &VersionReq) -> Option<Version> {
let cache = Self::load().unwrap();

if let Some(tool) = cache.tools.get(source) {
if let Some(tool) = cache.tools.get(&CiString(source.to_owned())) {
log::debug!("Tool has some versions installed");

let matching_version = tool
Expand Down Expand Up @@ -133,7 +134,7 @@ impl ToolCache {

log::trace!("Updating tool cache");
let mut cache = Self::load().unwrap();
let tool = cache.tools.entry(source.to_owned()).or_default();
let tool = cache.tools.entry(CiString(source.to_owned())).or_default();
tool.versions.insert(version.clone());
cache.save().unwrap();

Expand Down

0 comments on commit a57ba51

Please sign in to comment.