mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-02-03 15:04:13 +00:00
struct enums
This commit is contained in:
parent
785ad7714b
commit
e48d425672
@ -6,7 +6,7 @@ use redirect::Redirect;
|
||||
use status::Status;
|
||||
|
||||
// Global dependencies
|
||||
use gtk::{gio::Cancellable, glib::DateTime, prelude::CancellableExt};
|
||||
use gtk::{gio::Cancellable, prelude::CancellableExt};
|
||||
use std::{
|
||||
cell::{Cell, RefCell},
|
||||
rc::Rc,
|
||||
@ -45,7 +45,7 @@ impl Client {
|
||||
Self {
|
||||
cancellable: Cell::new(Cancellable::new()),
|
||||
redirect: Rc::new(Redirect::new()),
|
||||
status: Rc::new(RefCell::new(Status::Cancellable(now()))), // e.g. "ready to use"
|
||||
status: Rc::new(RefCell::new(Status::cancellable())), // e.g. "ready to use"
|
||||
gemini: gemini::Client::new(),
|
||||
}
|
||||
}
|
||||
@ -63,9 +63,9 @@ impl Client {
|
||||
let previous = self.cancellable.replace(cancellable.clone());
|
||||
if !previous.is_cancelled() {
|
||||
previous.cancel();
|
||||
self.status.replace(Status::Cancelled(now()));
|
||||
self.status.replace(Status::cancelled());
|
||||
} else {
|
||||
self.status.replace(Status::Cancellable(now()));
|
||||
self.status.replace(Status::cancellable());
|
||||
}
|
||||
|
||||
// Done
|
||||
@ -75,20 +75,14 @@ impl Client {
|
||||
/// Begin new request
|
||||
/// * the `query` as string, to support system routing requests (e.g. `source:`)
|
||||
pub fn request(&self, query: &str) {
|
||||
self.status
|
||||
.replace(Status::Request((now(), query.to_string())));
|
||||
self.status.replace(Status::request(query.to_string()));
|
||||
|
||||
// Forcefully prevent infinitive redirection
|
||||
// * this condition just to make sure that client will never stuck by driver implementation issue
|
||||
if self.redirect.count() > redirect::LIMIT {
|
||||
self.status
|
||||
.replace(Status::GlobalRedirectLimit((now(), redirect::LIMIT)));
|
||||
.replace(Status::failure_redirect_limit(redirect::LIMIT));
|
||||
// @TODO return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get current [DateTime](https://docs.gtk.org/glib/struct.DateTime.html)
|
||||
fn now() -> DateTime {
|
||||
DateTime::now_local().unwrap() // @TODO handle?
|
||||
}
|
||||
|
@ -1,3 +1,9 @@
|
||||
mod failure;
|
||||
|
||||
// Children dependencies
|
||||
use failure::Failure;
|
||||
|
||||
// Global dependencies
|
||||
use gtk::glib::{DateTime, GString};
|
||||
use std::fmt::{Display, Formatter, Result};
|
||||
|
||||
@ -5,38 +11,67 @@ use std::fmt::{Display, Formatter, Result};
|
||||
/// * not same as the Gemini status!
|
||||
pub enum Status {
|
||||
/// Ready to use (or cancel from outside)
|
||||
Cancellable(DateTime),
|
||||
Cancellable { event: DateTime },
|
||||
/// Operation cancelled, new `Cancellable` required to continue
|
||||
Cancelled(DateTime),
|
||||
/// Redirection count limit reached by protocol driver or global settings
|
||||
GlobalRedirectLimit((DateTime, usize)),
|
||||
Cancelled { event: DateTime },
|
||||
/// Something went wrong
|
||||
Failure { event: DateTime, failure: Failure },
|
||||
/// New `request` begin
|
||||
Request((DateTime, String)),
|
||||
Request { event: DateTime, value: String },
|
||||
}
|
||||
|
||||
impl Status {
|
||||
// Constructors
|
||||
|
||||
/// Create new `Self::Cancellable`
|
||||
pub fn cancellable() -> Self {
|
||||
Self::Cancellable { event: now() }
|
||||
}
|
||||
|
||||
/// Create new `Self::Cancelled`
|
||||
pub fn cancelled() -> Self {
|
||||
Self::Cancelled { event: now() }
|
||||
}
|
||||
|
||||
/// Create new `Self::Failure` as `Failure::RedirectLimit`
|
||||
pub fn failure_redirect_limit(count: usize) -> Self {
|
||||
Self::Failure {
|
||||
event: now(),
|
||||
failure: Failure::redirect_limit(count),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create new `Self::Request`
|
||||
pub fn request(value: String) -> Self {
|
||||
Self::Request {
|
||||
event: now(),
|
||||
value,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Status {
|
||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||
match self {
|
||||
Self::Cancellable(t) => {
|
||||
Self::Cancellable { event } => {
|
||||
write!(
|
||||
f,
|
||||
"[{}] Ready to use (or cancel from outside)",
|
||||
format_time(t)
|
||||
format_time(event)
|
||||
)
|
||||
}
|
||||
Self::Cancelled(t) => {
|
||||
Self::Cancelled { event } => {
|
||||
write!(
|
||||
f,
|
||||
"[{}] Operation cancelled, new `Cancellable` required to continue",
|
||||
format_time(t)
|
||||
format_time(event)
|
||||
)
|
||||
}
|
||||
Self::GlobalRedirectLimit((t, count)) => {
|
||||
write!(f, "[{}] Global redirection limit ({count}) reached by protocol driver or global settings",
|
||||
format_time(t))
|
||||
Self::Failure { event, failure } => {
|
||||
write!(f, "[{}] Failure: {failure}", format_time(event))
|
||||
}
|
||||
Self::Request((t, value)) => {
|
||||
write!(f, "[{}] Request `{value}`...", format_time(t))
|
||||
Self::Request { event, value } => {
|
||||
write!(f, "[{}] Request `{value}`...", format_time(event))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -46,3 +81,8 @@ impl Display for Status {
|
||||
fn format_time(t: &DateTime) -> GString {
|
||||
t.format_iso8601().unwrap() // @TODO handle?
|
||||
}
|
||||
|
||||
/// Get current [DateTime](https://docs.gtk.org/glib/struct.DateTime.html)
|
||||
fn now() -> DateTime {
|
||||
DateTime::now_local().unwrap() // @TODO handle?
|
||||
}
|
||||
|
@ -0,0 +1,30 @@
|
||||
// Global dependencies
|
||||
use std::fmt::{Display, Formatter, Result};
|
||||
|
||||
/// Local `Failure` status for `Client`
|
||||
pub enum Failure {
|
||||
/// Redirection count limit reached by protocol driver or global settings
|
||||
RedirectLimit { count: usize },
|
||||
}
|
||||
|
||||
impl Failure {
|
||||
// Constructors
|
||||
|
||||
/// Create new `Self::RedirectLimit`
|
||||
pub fn redirect_limit(count: usize) -> Self {
|
||||
Self::RedirectLimit { count }
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Failure {
|
||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||
match self {
|
||||
Self::RedirectLimit { count } => {
|
||||
write!(
|
||||
f,
|
||||
"Redirection limit ({count}) reached by protocol driver or global settings"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user