mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-02 09:35:28 +00:00
struct enums
This commit is contained in:
parent
785ad7714b
commit
e48d425672
3 changed files with 90 additions and 26 deletions
|
|
@ -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…
Add table
Add a link
Reference in a new issue