remove warnings of dead code

This commit is contained in:
Thomas Eppers 2021-09-24 16:36:16 +02:00
parent a068e3f192
commit 0a844c42ee
5 changed files with 9 additions and 79 deletions

View File

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

View File

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

View File

@ -33,16 +33,16 @@ pub enum Repo {
}
/// check if yaml line matches and returns the split of repo string and rest
pub fn match_yaml_image(input: &str) -> Option<(&str, &str)> {
pub fn match_yaml_image(input: &str) -> Result<(&str, &str), Error> {
lazy_static::lazy_static! {
static ref REGEX: Regex = Regex::new(r"^( +image *: *)([a-z0-9\./:]+)").unwrap();
}
let caps = match REGEX.captures(input) {
Some(caps) => caps,
None => return None,
None => return Err(Error::NoTagFound),
};
Some((caps.get(1).unwrap().as_str(), caps.get(2).unwrap().as_str()))
Ok((caps.get(1).unwrap().as_str(), caps.get(2).unwrap().as_str()))
}
pub fn split_tag_from_repo(input: &str) -> Result<(&str, &str), Error> {
@ -67,14 +67,6 @@ pub fn split_tag_from_repo(input: &str) -> Result<(&str, &str), Error> {
Ok((front, back))
}
// 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(repo: &str) -> Result<Repo, Error> {
let repo = repo.trim();
let split_repo: Vec<&str> = repo.split("/").collect();
@ -108,64 +100,11 @@ pub fn split_repo_without_tag(repo: &str) -> Result<Repo, Error> {
}
}
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)
}
}
// 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;

View File

@ -37,8 +37,6 @@ pub struct Tags {
#[derive(Debug, PartialEq)]
pub enum Error {
/// repo string contains an illegal character
InvalidCharacter(char),
/// couldn't fetch json with reqwest
Fetching(String),
/// a serde error
@ -52,7 +50,6 @@ pub enum Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::InvalidCharacter(c) => write!(f, "Invalid Character: {}", c),
Error::Fetching(s) => write!(f, "Fetching error: {}", s),
Error::Converting(s) => write!(f, "Converting error: {}", s),
Error::NoNextPage => write!(f, "No next page available"),

View File

@ -115,7 +115,7 @@ impl ServiceSwitcher<'_> {
}
//check if line matches
if repo::match_yaml_image(&self.list[i]).is_some() {
if repo::match_yaml_image(&self.list[i]).is_ok() {
self.state.select(Some(i));
return true;
}
@ -147,7 +147,7 @@ impl ServiceSwitcher<'_> {
}
//check if line matches
if repo::match_yaml_image(&self.list[i]).is_some() {
if repo::match_yaml_image(&self.list[i]).is_ok() {
self.state.select(Some(i));
return true;
}
@ -165,8 +165,8 @@ impl ServiceSwitcher<'_> {
match self.state.selected() {
None => return Err(Error::NoneSelected),
Some(i) => match repo::match_yaml_image(&self.list[i]) {
None => return Err(Error::Parsing(String::from("Nothing found"))),
Some((_, repo)) => return Ok(repo.to_string()),
Err(_) => return Err(Error::Parsing(String::from("Nothing found"))),
Ok((_, repo)) => return Ok(repo.to_string()),
},
}
}
@ -176,8 +176,8 @@ impl ServiceSwitcher<'_> {
match self.state.selected() {
None => (),
Some(i) => match repo::match_yaml_image(&self.list[i]) {
None => return,
Some((front, _)) => self.list[i] = format!("{}{}", front, repo_with_tag),
Err(_) => return,
Ok((front, _)) => self.list[i] = format!("{}{}", front, repo_with_tag),
},
}
self.changed = true;