use bytesize::ByteSize;
use serde::Deserialize;
use std::cmp::Ordering;
use url::Url;
#[derive(Deserialize, Debug)]
pub struct ServerVersion {
pub version: String,
}
#[derive(Deserialize, Debug)]
pub struct User {
pub login: String,
}
#[cfg(test)]
impl User {
pub fn new<S: ToString>(login: S) -> Self {
User {
login: login.to_string(),
}
}
}
#[derive(Deserialize, Debug)]
pub struct UsersSearch {
pub data: Vec<User>,
}
#[derive(Deserialize, Debug)]
pub struct Repo {
pub default_branch: String,
pub html_url: Url,
pub name: String,
pub description: String,
pub website: String,
pub owner: User,
pub updated_at: String,
pub private: bool,
pub parent: Option<Box<Repo>>,
}
#[derive(Deserialize, Debug)]
pub struct RepoSearch {
pub data: Vec<Repo>,
}
#[derive(Deserialize, Debug)]
pub struct Branch {
pub name: String,
}
#[cfg(test)]
impl Branch {
pub fn new<S: ToString>(name: S) -> Self {
Branch {
name: name.to_string(),
}
}
}
#[derive(Deserialize, PartialEq, Eq, Debug)]
#[serde(rename_all = "lowercase")]
pub enum FileEncoding {
Base64,
}
#[derive(Deserialize, PartialEq, Eq, Debug)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum Item {
File {
content: Option<String>,
encoding: Option<FileEncoding>,
name: String,
path: String,
size: ByteSize,
html_url: Url,
},
Dir {
name: String,
path: String,
size: ByteSize,
html_url: Url,
},
Symlink {
target: String,
name: String,
path: String,
html_url: Url,
},
Submodule {
submodule_git_url: String,
name: String,
path: String,
html_url: Url,
},
}
impl Item {
pub fn name(&self) -> &str {
match self {
Self::File { name, .. } => name,
Self::Dir { name, .. } => name,
Self::Symlink { name, .. } => name,
Self::Submodule { name, .. } => name,
}
}
pub fn path(&self) -> &String {
match self {
Self::File { path, .. } => path,
Self::Dir { path, .. } => path,
Self::Symlink { path, .. } => path,
Self::Submodule { path, .. } => path,
}
}
}
impl Ord for Item {
fn cmp(&self, other: &Self) -> Ordering {
match self {
Item::Dir { name, .. } | Item::Submodule { name, .. } => {
let a_name = name;
match other {
Item::File { .. } | Item::Symlink { .. } => Ordering::Less,
Item::Dir { name, .. } | Item::Submodule { name, .. } => {
let b_name = name;
a_name.to_lowercase().cmp(&b_name.to_lowercase())
}
}
}
Item::File { name, .. } | Item::Symlink { name, .. } => {
let a_name = name;
match other {
Item::Dir { .. } | Item::Submodule { .. } => Ordering::Greater,
Item::File { name, .. } | Item::Symlink { name, .. } => {
let b_name = name;
a_name.to_lowercase().cmp(&b_name.to_lowercase())
}
}
}
}
}
}
impl PartialOrd for Item {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub enum Contents<T> {
Single(T),
Multiple(Vec<T>),
}