This commit is contained in:
Thomas Eppers 2021-09-11 11:07:56 +02:00
parent ebf1a7726e
commit fadbe48b05
3 changed files with 150 additions and 0 deletions

5
src/common.rs Normal file
View File

@ -0,0 +1,5 @@
pub fn remove_last_char(input: &str) -> &str {
let mut chars = input.chars();
chars.next_back();
chars.as_str()
}

View File

@ -1,3 +1,5 @@
mod common;
mod repo;
mod tags;
mod ui;
mod widget;

143
src/repo.rs Normal file
View File

@ -0,0 +1,143 @@
use crate::common;
#[derive(Debug, PartialEq)]
pub enum Error {
Conversion,
Empty,
NoTagFound,
InvalidChar,
MisformedInput,
}
#[derive(Debug, PartialEq)]
pub enum Repo {
WithServer(String, String, String),
WithOrga(String, String),
Project(String),
}
// pub fn extract_yaml(repo: &str) -> (String, String, String, String) {
// let regex = regex::Regex::new(r"( *image *:) *([^/])??/([^/:]):?(.*)?");
// }
pub fn split_repo(repo: &str) -> Result<Repo, Error> {
let split_tag: Vec<&str> = repo.split(":").collect();
if split_tag.len() == 2 && split_tag[0].len() != 0 && split_tag[1].len() != 0 {
//
}
Ok(Repo::Project("".into()))
}
pub fn split_repo_without_tag(mut repo: &str) -> Result<Repo, Error> {
repo.trim();
let split_repo: Vec<&str> = repo.split("/").collect();
match split_repo.len() {
1 => {
let regex = regex::Regex::new(r"[a-z0-9]+").unwrap();
match regex.is_match(repo) {
false => Err(Error::MisformedInput),
true => Ok(Repo::Project(split_repo[0].into())),
}
}
2 => {
let regex = regex::Regex::new(r"[a-z0-9]+/[a-z0-9]+").unwrap();
match regex.is_match(repo) {
false => Err(Error::MisformedInput),
true => Ok(Repo::WithOrga(split_repo[0].into(), split_repo[1].into())),
}
}
3 => {
let regex = regex::Regex::new(r"[a-z0-9\.]+/[a-z0-9]+/[a-z0-9]+").unwrap();
match regex.is_match(repo) {
false => Err(Error::MisformedInput),
true => Ok(Repo::WithServer(
split_repo[0].into(),
split_repo[1].into(),
split_repo[2].into(),
)),
}
}
_ => Err(Error::MisformedInput),
}
}
pub fn split_tag(repo: &str) -> Result<(&str, &str), Error> {
let split_tag: Vec<&str> = repo.split(":").collect();
if split_tag.len() == 2 && split_tag[0].len() != 0 && split_tag[1].len() != 0 {
Ok((split_tag[0], split_tag[1]))
} else {
Err(Error::NoTagFound)
}
}
// fn
pub fn extract(repo: &str) -> Result<(Option<&str>, Option<&str>, &str), Error> {
if repo.len() == 0 {
return Err(Error::Empty);
}
let regex = regex::Regex::new(r"([^/:]*?/)??([^/:]*?/)?([^/:]*):?(.*)?").unwrap();
let caps = match regex.captures(repo) {
None => return Err(Error::Conversion),
Some(cap) => cap,
};
let server = match caps.get(1) {
None => None,
Some(cap) => Some(common::remove_last_char(cap.as_str())),
};
let orga = match caps.get(2) {
None => None,
Some(cap) => Some(common::remove_last_char(cap.as_str())),
};
Ok((server, orga, caps.get(3).unwrap().as_str()))
}
#[cfg(test)]
mod tests {
use crate::repo;
use crate::repo::{Error, Repo};
// #[test]
fn test_repo_regex() {
assert_eq!(repo::extract(""), Err(repo::Error::Empty));
assert_eq!(
repo::extract("ghcr.io/library/nginx"),
Ok((Some("ghcr.io"), Some("library"), "nginx"))
);
assert_eq!(
repo::extract("library/nginx"),
Ok((None, Some("library"), "nginx"))
);
assert_eq!(repo::extract("nginx"), Ok((None, None, "nginx")));
}
#[test]
fn split_tag() {
assert_eq!(repo::split_tag("nginx:v1"), Ok(("nginx", "v1")));
assert_eq!(repo::split_tag("dsfsdf"), Err(repo::Error::NoTagFound));
assert_eq!(repo::split_tag("nginx:"), Err(repo::Error::NoTagFound));
assert_eq!(repo::split_tag(":v1"), Err(repo::Error::NoTagFound));
assert_eq!(repo::split_tag(":"), Err(repo::Error::NoTagFound));
}
#[test]
fn test_split_repo_without_tag() {
use crate::repo::split_repo_without_tag as test_fn;
assert_eq!(test_fn(""), Err(Error::MisformedInput));
assert_eq!(test_fn("NGINX"), Err(Error::MisformedInput));
assert_eq!(test_fn("nginx"), Ok(Repo::Project("nginx".into())));
assert_eq!(
test_fn("library/nginx"),
Ok(Repo::WithOrga("library".into(), "nginx".into()))
);
assert_eq!(
test_fn("ghcr.io/library/nginx"),
Ok(Repo::WithServer(
"ghcr.io".into(),
"library".into(),
"nginx".into(),
))
);
}
}