Create TileResult type alias to clean up signatures

This commit is contained in:
Skye Jensen 2020-08-07 14:15:40 -04:00
parent 9ae994f637
commit 7d0c44bf5f
7 changed files with 20 additions and 16 deletions

View file

@ -13,6 +13,7 @@ use structopt::StructOpt;
use tokio::fs::File; use tokio::fs::File;
use tokio::prelude::*; use tokio::prelude::*;
use tokio::time::{self, Duration}; use tokio::time::{self, Duration};
use crate::tiles::TileResult;
#[derive(Deserialize, Clone, Debug, Default)] #[derive(Deserialize, Clone, Debug, Default)]
#[serde(default)] #[serde(default)]
@ -116,7 +117,7 @@ pub async fn read_config() -> Result<Config, Box<dyn std::error::Error>> {
pub fn process_tile( pub fn process_tile(
tile: &TileConfig, tile: &TileConfig,
connection: &Arc<SyncConnection>, connection: &Arc<SyncConnection>,
) -> BoxStream<'static, Result<Block, Box<dyn std::error::Error + Send + Sync>>> { ) -> BoxStream<'static, TileResult> {
let five_secs = Duration::from_secs(5); let five_secs = Duration::from_secs(5);
match &tile.config_type { match &tile.config_type {
TileConfigType::Battery => wrap(tiles::battery_stream(), tile.update.or(Some(five_secs))), TileConfigType::Battery => wrap(tiles::battery_stream(), tile.update.or(Some(five_secs))),
@ -130,9 +131,9 @@ pub fn process_tile(
fn wrap<'a, S>( fn wrap<'a, S>(
stream: S, stream: S,
duration: Option<Duration>, duration: Option<Duration>,
) -> BoxStream<'a, Result<Block, Box<dyn Error + Send + Sync>>> ) -> BoxStream<'a, TileResult>
where where
S: Stream<Item = Result<Block, Box<dyn Error + Send + Sync>>> + Send + 'a, S: Stream<Item = TileResult> + Send + 'a,
{ {
match duration { match duration {
Some(duration) => Box::pin(time::throttle(duration, stream)), Some(duration) => Box::pin(time::throttle(duration, stream)),

View file

@ -1,3 +1,4 @@
use super::TileResult;
use crate::tile::Block; use crate::tile::Block;
use futures::{future::try_join3, stream, Stream}; use futures::{future::try_join3, stream, Stream};
use futures_util::StreamExt; use futures_util::StreamExt;
@ -5,7 +6,7 @@ use std::error::Error;
use tokio::fs::File; use tokio::fs::File;
use tokio::prelude::*; use tokio::prelude::*;
pub fn battery_stream() -> impl Stream<Item = Result<Block, Box<dyn Error + Send + Sync>>> { pub fn battery_stream() -> impl Stream<Item = TileResult> {
stream::repeat(()).then(|()| async { stream::repeat(()).then(|()| async {
let charge_now = async { let charge_now = async {
let mut raw = String::new(); let mut raw = String::new();

View file

@ -1,13 +1,12 @@
use crate::tile::Block; use crate::tile::Block;
use crate::tiles::TileResult;
use dbus::nonblock::stdintf::org_freedesktop_dbus::Properties; use dbus::nonblock::stdintf::org_freedesktop_dbus::Properties;
use dbus::nonblock::{Proxy, SyncConnection}; use dbus::nonblock::{Proxy, SyncConnection};
use futures::{FutureExt, Stream}; use futures::{FutureExt, Stream};
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
pub fn hostname_stream( pub fn hostname_stream(connection: Arc<SyncConnection>) -> impl Stream<Item = TileResult> {
connection: Arc<SyncConnection>,
) -> impl Stream<Item = Result<Block, Box<dyn std::error::Error + Send + Sync>>> {
let proxy = Proxy::new( let proxy = Proxy::new(
"org.freedesktop.hostname1", "org.freedesktop.hostname1",
"/org/freedesktop/hostname1", "/org/freedesktop/hostname1",

View file

@ -1,11 +1,11 @@
use super::TileResult;
use crate::tile::Block; use crate::tile::Block;
use futures::stream::StreamExt; use futures::stream::StreamExt;
use tokio::fs::File; use tokio::fs::File;
use tokio::prelude::*; use tokio::prelude::*;
use tokio::stream::Stream; use tokio::stream::Stream;
pub fn load_stream() -> impl Stream<Item = Result<Block, Box<dyn std::error::Error + Send + Sync>>> pub fn load_stream() -> impl Stream<Item = TileResult> {
{
futures::stream::repeat(()).then(|()| async { futures::stream::repeat(()).then(|()| async {
let mut raw = String::new(); let mut raw = String::new();
File::open("/proc/loadavg") File::open("/proc/loadavg")

View file

@ -1,3 +1,4 @@
use super::TileResult;
use crate::tile::Block; use crate::tile::Block;
use futures::{stream, Stream, StreamExt}; use futures::{stream, Stream, StreamExt};
use std::{io, str, u64}; use std::{io, str, u64};
@ -40,8 +41,7 @@ fn extract_value(line: &str) -> Result<u64, Box<dyn std::error::Error + Send + S
.parse()?) .parse()?)
} }
pub fn memory_stream() -> impl Stream<Item = Result<Block, Box<dyn std::error::Error + Send + Sync>>> pub fn memory_stream() -> impl Stream<Item = TileResult> {
{
stream::repeat(()).then(|_| async { stream::repeat(()).then(|_| async {
let mut raw = [0u8; 256]; let mut raw = [0u8; 256];
File::open("/proc/meminfo") File::open("/proc/meminfo")

View file

@ -8,3 +8,8 @@ pub use hostname::hostname_stream;
pub use load::load_stream; pub use load::load_stream;
pub use memory::memory_stream; pub use memory::memory_stream;
pub use time::time_stream; pub use time::time_stream;
use crate::tile::Block;
use std::error::Error;
pub type TileResult = Result<Block, Box<dyn Error + Send + Sync>>;

View file

@ -1,3 +1,4 @@
use super::TileResult;
use crate::config::TimeConfig; use crate::config::TimeConfig;
use crate::tile::Block; use crate::tile::Block;
use chrono::prelude::*; use chrono::prelude::*;
@ -6,14 +7,11 @@ use futures::future::Future;
use futures::stream::Stream; use futures::stream::Stream;
use futures_util::ready; use futures_util::ready;
use pin_project::pin_project; use pin_project::pin_project;
use std::error::Error;
use std::pin::Pin; use std::pin::Pin;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use tokio::time::{delay_for, delay_until, Delay, Instant}; use tokio::time::{delay_for, delay_until, Delay, Instant};
pub fn time_stream( pub fn time_stream(config: TimeConfig) -> impl Stream<Item = TileResult> {
config: TimeConfig,
) -> impl Stream<Item = Result<Block, Box<dyn std::error::Error + Send + Sync>>> {
TimeStream { TimeStream {
config, config,
delay: delay_until(Instant::now()), delay: delay_until(Instant::now()),
@ -46,7 +44,7 @@ impl TimeStream {
} }
impl Stream for TimeStream { impl Stream for TimeStream {
type Item = Result<Block, Box<dyn Error + Send + Sync>>; type Item = TileResult;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let project = Pin::as_mut(&mut self).project(); let project = Pin::as_mut(&mut self).project();
ready!(Future::poll(project.delay, cx)); ready!(Future::poll(project.delay, cx));