use std::fmt;
use serde::Deserialize;
use crate::{Future, Github};
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum TimeUnit {
Week,
Day,
}
impl fmt::Display for TimeUnit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
TimeUnit::Week => "week",
TimeUnit::Day => "day",
}
.fmt(f)
}
}
pub struct Traffic {
github: Github,
owner: String,
repo: String,
}
impl Traffic {
#[doc(hidden)]
pub fn new<O, R>(github: Github, owner: O, repo: R) -> Self
where
O: Into<String>,
R: Into<String>,
{
Traffic {
github,
owner: owner.into(),
repo: repo.into(),
}
}
fn path(&self, more: &str) -> String {
format!("/repos/{}/{}/traffic{}", self.owner, self.repo, more)
}
pub fn referrers(&self) -> Future<Vec<Referrer>> {
self.github.get(&self.path("/popular/referrers"))
}
pub fn paths(&self) -> Future<Vec<Path>> {
self.github.get(&self.path("/popular/paths"))
}
pub fn views(&self, unit: TimeUnit) -> Future<Views> {
let path = match unit {
TimeUnit::Week => "/views?per=week",
TimeUnit::Day => "/views?per=day",
};
self.github.get(&self.path(path))
}
pub fn clones(&self, unit: TimeUnit) -> Future<Clones> {
let path = match unit {
TimeUnit::Week => "/clones?per=week",
TimeUnit::Day => "/clones?per=day",
};
self.github.get(&self.path(path))
}
}
#[derive(Debug, Deserialize)]
pub struct Referrer {
pub referrer: String,
pub count: u32,
pub uniques: u32,
}
#[derive(Debug, Deserialize)]
pub struct Path {
pub path: String,
pub title: String,
pub count: u32,
pub uniques: u32,
}
#[derive(Debug, Deserialize)]
pub struct Views {
pub count: u32,
pub uniques: u32,
pub views: Vec<DataPoint>,
}
#[derive(Debug, Deserialize)]
pub struct Clones {
pub count: u32,
pub uniques: u32,
pub clones: Vec<DataPoint>,
}
#[derive(Debug, Deserialize)]
pub struct DataPoint {
pub timestamp: String,
pub count: u32,
pub uniques: u32,
}