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;
|
use status::Status;
|
||||||
|
|
||||||
// Global dependencies
|
// Global dependencies
|
||||||
use gtk::{gio::Cancellable, glib::DateTime, prelude::CancellableExt};
|
use gtk::{gio::Cancellable, prelude::CancellableExt};
|
||||||
use std::{
|
use std::{
|
||||||
cell::{Cell, RefCell},
|
cell::{Cell, RefCell},
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
@ -45,7 +45,7 @@ impl Client {
|
|||||||
Self {
|
Self {
|
||||||
cancellable: Cell::new(Cancellable::new()),
|
cancellable: Cell::new(Cancellable::new()),
|
||||||
redirect: Rc::new(Redirect::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(),
|
gemini: gemini::Client::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -63,9 +63,9 @@ impl Client {
|
|||||||
let previous = self.cancellable.replace(cancellable.clone());
|
let previous = self.cancellable.replace(cancellable.clone());
|
||||||
if !previous.is_cancelled() {
|
if !previous.is_cancelled() {
|
||||||
previous.cancel();
|
previous.cancel();
|
||||||
self.status.replace(Status::Cancelled(now()));
|
self.status.replace(Status::cancelled());
|
||||||
} else {
|
} else {
|
||||||
self.status.replace(Status::Cancellable(now()));
|
self.status.replace(Status::cancellable());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Done
|
// Done
|
||||||
@ -75,20 +75,14 @@ impl Client {
|
|||||||
/// Begin new request
|
/// Begin new request
|
||||||
/// * the `query` as string, to support system routing requests (e.g. `source:`)
|
/// * the `query` as string, to support system routing requests (e.g. `source:`)
|
||||||
pub fn request(&self, query: &str) {
|
pub fn request(&self, query: &str) {
|
||||||
self.status
|
self.status.replace(Status::request(query.to_string()));
|
||||||
.replace(Status::Request((now(), query.to_string())));
|
|
||||||
|
|
||||||
// Forcefully prevent infinitive redirection
|
// Forcefully prevent infinitive redirection
|
||||||
// * this condition just to make sure that client will never stuck by driver implementation issue
|
// * this condition just to make sure that client will never stuck by driver implementation issue
|
||||||
if self.redirect.count() > redirect::LIMIT {
|
if self.redirect.count() > redirect::LIMIT {
|
||||||
self.status
|
self.status
|
||||||
.replace(Status::GlobalRedirectLimit((now(), redirect::LIMIT)));
|
.replace(Status::failure_redirect_limit(redirect::LIMIT));
|
||||||
// @TODO return;
|
// @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 gtk::glib::{DateTime, GString};
|
||||||
use std::fmt::{Display, Formatter, Result};
|
use std::fmt::{Display, Formatter, Result};
|
||||||
|
|
||||||
@ -5,38 +11,67 @@ use std::fmt::{Display, Formatter, Result};
|
|||||||
/// * not same as the Gemini status!
|
/// * not same as the Gemini status!
|
||||||
pub enum Status {
|
pub enum Status {
|
||||||
/// Ready to use (or cancel from outside)
|
/// Ready to use (or cancel from outside)
|
||||||
Cancellable(DateTime),
|
Cancellable { event: DateTime },
|
||||||
/// Operation cancelled, new `Cancellable` required to continue
|
/// Operation cancelled, new `Cancellable` required to continue
|
||||||
Cancelled(DateTime),
|
Cancelled { event: DateTime },
|
||||||
/// Redirection count limit reached by protocol driver or global settings
|
/// Something went wrong
|
||||||
GlobalRedirectLimit((DateTime, usize)),
|
Failure { event: DateTime, failure: Failure },
|
||||||
/// New `request` begin
|
/// 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 {
|
impl Display for Status {
|
||||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||||
match self {
|
match self {
|
||||||
Self::Cancellable(t) => {
|
Self::Cancellable { event } => {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"[{}] Ready to use (or cancel from outside)",
|
"[{}] Ready to use (or cancel from outside)",
|
||||||
format_time(t)
|
format_time(event)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Self::Cancelled(t) => {
|
Self::Cancelled { event } => {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"[{}] Operation cancelled, new `Cancellable` required to continue",
|
"[{}] Operation cancelled, new `Cancellable` required to continue",
|
||||||
format_time(t)
|
format_time(event)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Self::GlobalRedirectLimit((t, count)) => {
|
Self::Failure { event, failure } => {
|
||||||
write!(f, "[{}] Global redirection limit ({count}) reached by protocol driver or global settings",
|
write!(f, "[{}] Failure: {failure}", format_time(event))
|
||||||
format_time(t))
|
|
||||||
}
|
}
|
||||||
Self::Request((t, value)) => {
|
Self::Request { event, value } => {
|
||||||
write!(f, "[{}] Request `{value}`...", format_time(t))
|
write!(f, "[{}] Request `{value}`...", format_time(event))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -46,3 +81,8 @@ impl Display for Status {
|
|||||||
fn format_time(t: &DateTime) -> GString {
|
fn format_time(t: &DateTime) -> GString {
|
||||||
t.format_iso8601().unwrap() // @TODO handle?
|
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