use std::collections::BTreeMap;
use async_trait::async_trait;
use serde::Deserialize;
use crate::options::OptionSchema;
use crate::payload::{Bar, BarsData, Body, EntriesData, Entry, Payload};
use crate::render::Shape;
use crate::samples;
use super::super::{FetchContext, FetchError, Fetcher, Safety};
use super::client::rest_get;
use super::common::{RepoSlug, cache_key, parse_options, payload, resolve_repo};
const SHAPES: &[Shape] = &[Shape::Bars, Shape::Entries];
const OPTION_SCHEMAS: &[OptionSchema] = &[
OptionSchema {
name: "repo",
type_hint: "\"owner/name\"",
required: false,
default: Some("git remote of cwd"),
description: "Repository to query. Falls back to the current directory's github remote.",
},
OptionSchema {
name: "limit",
type_hint: "integer (1..=20)",
required: false,
default: Some("6"),
description: "Maximum number of languages to surface. Smaller slices collapse into the tail.",
},
];
const DEFAULT_LIMIT: usize = 6;
pub struct GithubLanguages;
#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Options {
#[serde(default)]
pub repo: Option<String>,
#[serde(default)]
pub limit: Option<u32>,
}
#[async_trait]
impl Fetcher for GithubLanguages {
fn name(&self) -> &str {
"github_languages"
}
fn safety(&self) -> Safety {
Safety::Safe
}
fn shapes(&self) -> &[Shape] {
SHAPES
}
fn default_shape(&self) -> Shape {
Shape::Bars
}
fn option_schemas(&self) -> &[OptionSchema] {
OPTION_SCHEMAS
}
fn cache_key(&self, ctx: &FetchContext) -> String {
let extra = repo_for_key(ctx);
cache_key(self.name(), ctx, &extra)
}
fn sample_body(&self, shape: Shape) -> Option<Body> {
Some(match shape {
Shape::Bars => samples::bars(&[("Rust", 87_000), ("TOML", 8_000), ("Shell", 5_000)]),
Shape::Entries => samples::entries(&[("Rust", "87%"), ("TOML", "8%"), ("Shell", "5%")]),
_ => return None,
})
}
async fn fetch(&self, ctx: &FetchContext) -> Result<Payload, FetchError> {
let opts: Options = parse_options(ctx.options.as_ref()).map_err(FetchError::Failed)?;
let slug = resolve_repo(opts.repo.as_deref())?;
let limit = opts
.limit
.map(|n| n as usize)
.unwrap_or(DEFAULT_LIMIT)
.max(1);
let raw: BTreeMap<String, u64> =
rest_get(&format!("/repos/{}/{}/languages", slug.owner, slug.name)).await?;
let body = build_body(raw, ctx.shape.unwrap_or(Shape::Bars), limit);
Ok(payload(body))
}
}
fn build_body(raw: BTreeMap<String, u64>, shape: Shape, limit: usize) -> Body {
let sorted = top_n(raw, limit);
match shape {
Shape::Entries => Body::Entries(EntriesData {
items: to_entries(&sorted),
}),
_ => Body::Bars(BarsData {
bars: sorted
.into_iter()
.map(|(label, value)| Bar { label, value })
.collect(),
}),
}
}
fn top_n(raw: BTreeMap<String, u64>, limit: usize) -> Vec<(String, u64)> {
let mut all: Vec<(String, u64)> = raw.into_iter().collect();
all.sort_by_key(|entry| std::cmp::Reverse(entry.1));
if all.len() <= limit {
return all;
}
let (head, tail) = all.split_at(limit.saturating_sub(1));
let other: u64 = tail.iter().map(|(_, v)| v).sum();
let mut out: Vec<(String, u64)> = head.to_vec();
if other > 0 {
out.push(("other".into(), other));
}
out
}
fn to_entries(sorted: &[(String, u64)]) -> Vec<Entry> {
let total: u64 = sorted.iter().map(|(_, v)| v).sum();
sorted
.iter()
.map(|(label, value)| Entry {
key: label.clone(),
value: Some(format_percent(*value, total)),
status: None,
})
.collect()
}
fn format_percent(value: u64, total: u64) -> String {
if total == 0 {
return "0%".into();
}
let pct = (value as f64 / total as f64) * 100.0;
format!("{pct:.1}%")
}
fn repo_for_key(ctx: &FetchContext) -> String {
ctx.options
.as_ref()
.and_then(|v| v.get("repo"))
.and_then(|v| v.as_str())
.map(String::from)
.or_else(|| resolve_repo(None).ok().map(|s: RepoSlug| s.as_path()))
.unwrap_or_default()
}
#[cfg(test)]
mod tests {
use super::*;
fn raw(pairs: &[(&str, u64)]) -> BTreeMap<String, u64> {
pairs.iter().map(|(k, v)| ((*k).into(), *v)).collect()
}
#[test]
fn top_n_sorts_by_size_descending() {
let input = raw(&[("TOML", 100), ("Rust", 1000), ("Shell", 50)]);
let sorted = top_n(input, 10);
assert_eq!(
sorted,
vec![
("Rust".into(), 1000),
("TOML".into(), 100),
("Shell".into(), 50)
]
);
}
#[test]
fn top_n_folds_tail_into_other() {
let input = raw(&[("A", 1000), ("B", 500), ("C", 100), ("D", 50)]);
let sorted = top_n(input, 3);
assert_eq!(sorted[0], ("A".into(), 1000));
assert_eq!(sorted[1], ("B".into(), 500));
assert_eq!(sorted[2], ("other".into(), 150));
}
#[test]
fn top_n_skips_other_when_no_tail() {
let input = raw(&[("A", 100), ("B", 50)]);
let sorted = top_n(input, 5);
assert_eq!(sorted.len(), 2);
assert!(sorted.iter().all(|(k, _)| k != "other"));
}
#[test]
fn format_percent_renders_one_decimal() {
assert_eq!(format_percent(87, 100), "87.0%");
assert_eq!(format_percent(1, 3), "33.3%");
}
#[test]
fn format_percent_guards_zero_total() {
assert_eq!(format_percent(0, 0), "0%");
}
#[test]
fn entries_include_percent_values() {
let sorted = vec![("Rust".into(), 750), ("TOML".into(), 250)];
let entries = to_entries(&sorted);
assert_eq!(entries[0].value.as_deref(), Some("75.0%"));
assert_eq!(entries[1].value.as_deref(), Some("25.0%"));
}
#[test]
fn build_body_bars_preserves_top_n_order() {
let body = build_body(raw(&[("A", 500), ("B", 1000), ("C", 100)]), Shape::Bars, 10);
match body {
Body::Bars(d) => {
assert_eq!(d.bars[0].label, "B");
assert_eq!(d.bars[1].label, "A");
assert_eq!(d.bars[2].label, "C");
}
_ => panic!("expected bars"),
}
}
}