Skip to content

Instantly share code, notes, and snippets.

@Checksum
Created September 13, 2020 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Checksum/6b5f49bdda69f415d6018f59cfab808d to your computer and use it in GitHub Desktop.
Save Checksum/6b5f49bdda69f415d6018f59cfab808d to your computer and use it in GitHub Desktop.
Syscheck rust lifetime issue
use crate::core::{Check, Error as CheckError};
use iced::{Application, Command, Container, Element, Settings, Text};
fn main() -> iced::Result {
Syscheck::run(Settings::default())
}
#[derive(Debug)]
pub enum Syscheck {
Setup(SetupState),
Running(RunningState),
}
#[derive(Debug, Default)]
pub struct SetupState {}
#[derive(Debug)]
pub struct RunningState {
// checks
network: checks::Network,
}
impl RunningState {
async fn start() -> Message {
Message::StartChecks
}
}
#[derive(Debug, Clone)]
pub enum Message {
// setup
SetupComplete,
// checks
StartChecks,
NetworkCheckComplete(Result<(), CheckError>),
}
impl Application for Syscheck {
type Executor = iced::executor::Default;
type Message = Message;
type Flags = ();
fn new(_flags: ()) -> (Syscheck, Command<Message>) {
(Syscheck::Setup(SetupState::default()), Command::none())
}
fn title(&self) -> String {
String::from("Syscheck")
}
fn update(&mut self, message: Message) -> Command<Message> {
match self {
Syscheck::Setup(state) => match message {
Message::SetupComplete => {
*self = Syscheck::Running(RunningState {
network: checks::Network::new(),
});
Command::from(RunningState::start())
}
_ => Command::none(),
},
Syscheck::Running(state) => {
match message {
// Checks
Message::StartChecks => {
println!("starting checks...");
Command::batch(vec![Command::perform(
state.network.run(),
Message::NetworkCheckComplete,
)])
}
_ => Command::none(),
}
}
}
}
fn view(&mut self) -> Element<Message> {
Container::new(Text::new("Running")).into()
}
}
pub mod core {
use async_trait::async_trait;
pub use String as Error;
#[async_trait]
pub trait Check {
fn new() -> Self;
async fn run(&self) -> Result<(), Error>;
}
}
pub mod checks {
use crate::core::{Check, Error};
use async_trait::async_trait;
#[derive(Debug)]
pub struct Network {
url: &'static str,
}
#[async_trait]
impl Check for Network {
fn new() -> Network {
let url: &'static str = format!("https://google.com").as_str();
Network { url: url }
}
async fn run(&self) -> Result<(), Error> {
reqwest::blocking::get(&self.url.to_string()).map_err(|_| "Network error".to_string());
Ok(())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment