Skip to content

Commit

Permalink
fix(gists): language does not always exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Lioness100 committed Aug 22, 2022
1 parent 93fe9ac commit 88dc4be
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "guess-that-lang"
version = "1.0.19"
version = "1.0.20"
rust-version = "1.63"
authors = ["Lioness100 <[email protected]>"]
edition = "2021"
Expand Down
14 changes: 9 additions & 5 deletions src/providers/gists.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, result};
use std::{collections::BTreeMap, result};

use rand::{seq::SliceRandom, thread_rng, Rng};
use serde::Deserialize;
Expand All @@ -12,12 +12,12 @@ use crate::{

#[derive(Deserialize)]
pub struct Gist {
pub files: HashMap<String, GistFile>,
pub files: BTreeMap<String, GistFile>,
}

#[derive(Deserialize)]
pub struct GistFile {
pub language: String,
pub language: Option<String>,
pub raw_url: String,
}

Expand All @@ -35,12 +35,16 @@ impl TryFrom<Gist> for GistData {
let file = gist
.files
.into_values()
.find(|file| LANGUAGES.contains(&file.language.as_str()))
.find(|file| {
file.language
.as_ref()
.map_or(false, |language| LANGUAGES.contains(&language.as_str()))
})
.ok_or(())?;

Ok(Self {
url: file.raw_url.to_string(),
language: file.language,
language: file.language.unwrap(),
})
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/providers/repos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl RepositoryProvider<'_> {
/// Get a vec of random valid gists on Github. This is used with the assumption
/// that at least one valid gist will be found.
pub fn get_repos(&self, language: &str) -> Result<Vec<String>> {
let mut gists: Vec<_> = self
let mut repos: Vec<_> = self
.agent
.get(&format!("{GITHUB_BASE_URL}/search/repositories"))
.query("page", &thread_rng().gen_range(0..35).to_string())
Expand All @@ -55,9 +55,9 @@ impl RepositoryProvider<'_> {
.map(|repo| repo.full_name)
.collect();

gists.shuffle(&mut thread_rng());
repos.shuffle(&mut thread_rng());

Ok(gists)
Ok(repos)
}

pub fn get_file(&self, language: &str, name: &str) -> Result<RepositoryFile> {
Expand Down

0 comments on commit 88dc4be

Please sign in to comment.