added some tests; more robust error handling
This commit is contained in:
parent
dd261b0127
commit
918036f2f8
37
src/tags.rs
37
src/tags.rs
@ -29,13 +29,23 @@ pub struct Tags {
|
|||||||
pub results: Vec<Images>,
|
pub results: Vec<Images>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Display)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
InvalidCharacter(char),
|
InvalidCharacter(char),
|
||||||
Fetching(String),
|
Fetching(String),
|
||||||
Converting(String),
|
Converting(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Tags {
|
impl Tags {
|
||||||
pub fn new(repo: String) -> Result<Self, Error> {
|
pub fn new(repo: String) -> Result<Self, Error> {
|
||||||
// let repo = Self::check_repo(repo)?;
|
// let repo = Self::check_repo(repo)?;
|
||||||
@ -102,3 +112,28 @@ fn format_time_nice(time: chrono::Duration) -> String {
|
|||||||
format!("{} Sekunden", time.num_seconds())
|
format!("{} Sekunden", time.num_seconds())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::tags;
|
||||||
|
#[test]
|
||||||
|
fn test_check_repo() {
|
||||||
|
let check_eq = |s, s2| {
|
||||||
|
assert_eq!(&tags::Tags::check_repo(String::from(s)).unwrap(), s2);
|
||||||
|
};
|
||||||
|
let check_neq = |s, s2| {
|
||||||
|
assert_ne!(&tags::Tags::check_repo(String::from(s)).unwrap(), s2);
|
||||||
|
};
|
||||||
|
let check_err = |s: &str| {
|
||||||
|
assert_eq!(tags::Tags::check_repo(String::from(s)).is_err(), true);
|
||||||
|
};
|
||||||
|
|
||||||
|
check_eq("nginx", "library/nginx");
|
||||||
|
check_neq("nginx", "nginx");
|
||||||
|
check_eq("rocketchat/rocket.chat", "rocketchat/rocket.chat");
|
||||||
|
check_eq("mysql", "library/mysql");
|
||||||
|
check_neq("mysql", "mysql");
|
||||||
|
check_err("nginxä");
|
||||||
|
check_err("nginx²");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
17
src/ui.rs
17
src/ui.rs
@ -106,14 +106,14 @@ impl Ui {
|
|||||||
},
|
},
|
||||||
Ok(Key::Char(key)) => {
|
Ok(Key::Char(key)) => {
|
||||||
if ui.state == State::EditRepo {
|
if ui.state == State::EditRepo {
|
||||||
ui.tags = tag_list::TagList::with_status("Editing Repository");
|
ui.show_info("Editing Repository");
|
||||||
}
|
}
|
||||||
ui.repo.handle_input(&ui.state, Key::Char(key));
|
ui.repo.handle_input(&ui.state, Key::Char(key));
|
||||||
ui.tags.handle_input(&ui.state, Key::Char(key));
|
ui.tags.handle_input(&ui.state, Key::Char(key));
|
||||||
}
|
}
|
||||||
Ok(Key::Backspace) => {
|
Ok(Key::Backspace) => {
|
||||||
if ui.state == State::EditRepo {
|
if ui.state == State::EditRepo {
|
||||||
ui.tags = tag_list::TagList::with_status("Editing Repository");
|
ui.show_info("Editing Repository");
|
||||||
}
|
}
|
||||||
ui.repo.handle_input(&ui.state, Key::Backspace);
|
ui.repo.handle_input(&ui.state, Key::Backspace);
|
||||||
ui.tags.handle_input(&ui.state, Key::Backspace);
|
ui.tags.handle_input(&ui.state, Key::Backspace);
|
||||||
@ -121,7 +121,7 @@ impl Ui {
|
|||||||
Ok(Key::Up) => {
|
Ok(Key::Up) => {
|
||||||
if ui.state == State::SelectService && ui.services.find_previous_match() {
|
if ui.state == State::SelectService && ui.services.find_previous_match() {
|
||||||
match ui.services.extract_repo() {
|
match ui.services.extract_repo() {
|
||||||
Err(_) => ui.tags = tag_list::TagList::with_status("No image found"),
|
Err(_) => ui.show_info("No image found"),
|
||||||
Ok(s) => ui.repo.set(s),
|
Ok(s) => ui.repo.set(s),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -131,10 +131,13 @@ impl Ui {
|
|||||||
Ok(Key::Down) => match ui.state {
|
Ok(Key::Down) => match ui.state {
|
||||||
State::SelectService if ui.services.find_next_match() => {
|
State::SelectService if ui.services.find_next_match() => {
|
||||||
match ui.services.extract_repo() {
|
match ui.services.extract_repo() {
|
||||||
Err(_) => ui.tags = tag_list::TagList::with_status("No image found"),
|
Err(e) => ui.show_info(&format!("{}", e)),
|
||||||
Ok(s) => {
|
Ok(s) => {
|
||||||
let repo = match crate::tags::Tags::check_repo(s) {
|
let repo = match crate::tags::Tags::check_repo(s) {
|
||||||
Err(_) => continue,
|
Err(e) => {
|
||||||
|
ui.show_info(&format!("{}", e));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
};
|
};
|
||||||
ui.repo.set(repo);
|
ui.repo.set(repo);
|
||||||
@ -160,6 +163,10 @@ impl Ui {
|
|||||||
terminal.clear().unwrap();
|
terminal.clear().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn show_info(&mut self, error: &str) {
|
||||||
|
self.tags = tag_list::TagList::with_status(error);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn spawn_stdin_channel(&self) -> mpsc::Receiver<termion::event::Key> {
|
pub fn spawn_stdin_channel(&self) -> mpsc::Receiver<termion::event::Key> {
|
||||||
let (tx, rx) = mpsc::channel::<termion::event::Key>();
|
let (tx, rx) = mpsc::channel::<termion::event::Key>();
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use std::fmt;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::BufRead;
|
use std::io::BufRead;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
@ -9,12 +10,21 @@ use tui::widgets::{Block, Borders, List, ListState};
|
|||||||
|
|
||||||
use crate::ui::State;
|
use crate::ui::State;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Display)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
NoneSelected,
|
NoneSelected,
|
||||||
Parsing(String),
|
Parsing(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Error {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Error::NoneSelected => write!(f, "None selected"),
|
||||||
|
Error::Parsing(s) => write!(f, "Parsing error: {}", s),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct ServiceSwitcher {
|
pub struct ServiceSwitcher {
|
||||||
list: Vec<String>,
|
list: Vec<String>,
|
||||||
state: ListState,
|
state: ListState,
|
||||||
|
Loading…
Reference in New Issue
Block a user