Skip to content

Commit

Permalink
going async (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Nov 14, 2021
1 parent bc48bf2 commit 96efa71
Show file tree
Hide file tree
Showing 9 changed files with 443 additions and 120 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 6 additions & 25 deletions src/feed/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
use super::{Feed, FeedError, FeedResult, Kiosk};
use super::{Feed, FeedError, FeedResult};
use feed_rs::parser as feed_parser;
use std::io::Read;

Expand All @@ -36,24 +36,16 @@ use std::io::Read;
pub struct Client;

impl Client {
/// ### fetch
///
/// Fetch a source with the current configuration
pub fn fetch(&self, kiosk: &mut Kiosk, name: &str, url: &str) -> FeedResult<()> {
kiosk.insert_feed(name.to_string(), self.fetch_source(url)?);
Ok(())
}

// -- private

/// ### fetch_source
///
/// Fetch a single source from remote
fn fetch_source(&self, source: &str) -> FeedResult<Feed> {
pub fn fetch(&self, source: &str) -> FeedResult<Feed> {
let body = self.get_feed(source)?;
self.parse_feed(body)
}

// -- private

/// ### get_feed
///
/// Get feed via HTTP GET request
Expand Down Expand Up @@ -99,22 +91,11 @@ mod test {
#[test]
fn should_fetch_source() {
let client = Client::default();
let mut kiosk = Kiosk::default();
assert!(client
.fetch(
&mut kiosk,
"nytimes",
"https://rss.nytimes.com/services/xml/rss/nyt/World.xml",
)
.fetch("https://rss.nytimes.com/services/xml/rss/nyt/World.xml",)
.is_ok());
assert!(kiosk.get_feed("nytimes").is_some());
assert!(client
.fetch(
&mut kiosk,
"lefigaro",
"https://www.lefigaro.fr/rss/figaro_actualites.xml",
)
.fetch("https://www.lefigaro.fr/rss/figaro_actualites.xml",)
.is_ok());
assert!(kiosk.get_feed("lefigaro").is_some());
}
}
83 changes: 2 additions & 81 deletions src/feed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,20 @@ pub use result::{FeedError, FeedResult};
// -- deps
use chrono::{DateTime, Local};
use feed_rs::model::{Entry as RssEntry, Feed as RssFeed};
use std::collections::HashMap;
use std::slice::Iter;

/// ## Kiosk
///
/// Describes the current feed holder.
/// It contains different sources, each one with its own feed
#[derive(Debug, Default)]
pub struct Kiosk {
/// Association between Source name and Feed
feed: HashMap<String, Feed>,
}

/// ## Feed
///
/// Contains, for a feed source, the list of articles fetched from remote
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq)]
pub struct Feed {
articles: Vec<Article>,
}

/// ## Article
///
/// identifies a single article in the feed
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq)]
pub struct Article {
pub title: Option<String>,
pub authors: Vec<String>,
Expand All @@ -71,31 +60,6 @@ pub struct Article {
pub date: Option<DateTime<Local>>,
}

// -- impl

impl Kiosk {
/// ### insert_feed
///
/// Insert a feed into kiosk
pub fn insert_feed<S: AsRef<str>>(&mut self, source: S, feed: Feed) {
self.feed.insert(source.as_ref().to_string(), feed);
}

/// ### get_feed
///
/// Get feed from kiosk
pub fn get_feed(&self, source: &str) -> Option<&Feed> {
self.feed.get(source)
}

/// ### sources
///
/// Get sources in kiosk
pub fn sources(&self) -> Vec<&String> {
self.feed.keys().into_iter().collect()
}
}

impl Feed {
/// ### articles
///
Expand Down Expand Up @@ -142,49 +106,6 @@ mod test {
use feed_rs::model::FeedType;
use pretty_assertions::assert_eq;

#[test]
fn should_create_kiosk() {
let kiosk = Kiosk::default();
assert!(kiosk.feed.is_empty());
}

#[test]
fn should_insert_feed_into_kiosk() {
let mut kiosk = Kiosk::default();
kiosk.insert_feed(
"lefigaro",
Feed {
articles: Vec::default(),
},
);
assert_eq!(kiosk.feed.len(), 1);
}

#[test]
fn should_get_feed_from_kiosk() {
let mut kiosk = Kiosk::default();
kiosk.insert_feed(
"lefigaro",
Feed {
articles: Vec::default(),
},
);
assert!(kiosk.get_feed("lefigaro").is_some());
assert!(kiosk.get_feed("foobar").is_none());
}

#[test]
fn should_get_sources_from_kiosk() {
let mut kiosk = Kiosk::default();
kiosk.insert_feed(
"lefigaro",
Feed {
articles: Vec::default(),
},
);
assert_eq!(kiosk.sources(), vec![&String::from("lefigaro")]);
}

#[test]
fn should_get_feed_attributes() {
let feed = Feed {
Expand Down
45 changes: 45 additions & 0 deletions src/ui/components/lists/feed_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//! # Feed list
//!
//! Mock component to implement the feed list
/**
* MIT License
*
* tuifeed - Copyright (c) 2021 Christian Visintin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
use tui_realm_stdlib::{states::ListStates, List};
use tuirealm::command::{Cmd, CmdResult, Direction, Position};
use tuirealm::props::{
Alignment, AttrValue, Attribute, Borders, Color, Props, Style, Table, TextModifiers,
};
use tuirealm::tui::{
layout::{Corner, Rect},
text::{Span, Spans},
widgets::{List as TuiList, ListItem, ListState},
};
use tuirealm::{Frame, MockComponent, State, StateValue};

/// ## FeedList
///
/// A list which prepend the fetch state for each source for the feed
pub struct FeedList {
list: List,
}
6 changes: 4 additions & 2 deletions src/ui/components/lists.rs → src/ui/components/lists/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
mod feed_list;

use super::Msg;

use tui_realm_stdlib::List;
Expand All @@ -35,13 +37,13 @@ use tuirealm::{Component, Event, MockComponent, NoUserEvent, State, StateValue};

#[derive(MockComponent)]
pub struct FeedList {
component: List,
component: feed_list::FeedList,
}

impl FeedList {
pub fn new(sources: &[&String]) -> Self {
Self {
component: List::default()
component: feed_list::FeedList::default()
.highlighted_color(Color::LightBlue)
.highlighted_str("➤ ")
.rewind(true)
Expand Down
Loading

0 comments on commit 96efa71

Please sign in to comment.