Skip to content

Commit

Permalink
refactor(term): use FeedUrl instead of String
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Apr 20, 2024
1 parent 95bb5ea commit 7503ae0
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 33 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

20 changes: 15 additions & 5 deletions crates/synd_term/gql/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"name": "FeedUrl",
"ofType": null
}
}
Expand Down Expand Up @@ -751,7 +751,7 @@
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"name": "FeedUrl",
"ofType": null
}
}
Expand All @@ -771,7 +771,7 @@
{
"args": [],
"deprecationReason": null,
"description": null,
"description": "Category of the feed",
"isDeprecated": false,
"name": "category",
"type": {
Expand Down Expand Up @@ -828,6 +828,16 @@
"name": "FeedType",
"possibleTypes": null
},
{
"description": null,
"enumValues": null,
"fields": null,
"inputFields": null,
"interfaces": null,
"kind": "SCALAR",
"name": "FeedUrl",
"possibleTypes": null
},
{
"description": "The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).",
"enumValues": null,
Expand Down Expand Up @@ -1504,7 +1514,7 @@
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"name": "FeedUrl",
"ofType": null
}
}
Expand Down Expand Up @@ -1741,7 +1751,7 @@
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"name": "FeedUrl",
"ofType": null
}
}
Expand Down
22 changes: 17 additions & 5 deletions crates/synd_term/src/application/input_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ mod feed {
sequence::{delimited, Tuple},
Finish, IResult, Parser,
};
use synd_feed::types::Category;
use synd_feed::types::{Category, FeedUrl};
use url::Url;

use super::NomError;
use crate::{
Expand Down Expand Up @@ -129,11 +130,22 @@ mod feed {
))
}

fn url(s: &str) -> IResult<&str, String> {
map(take_while(|c: char| !c.is_whitespace()), |s: &str| {
fn url(s: &str) -> IResult<&str, FeedUrl> {
let (remain, url) = map(take_while(|c: char| !c.is_whitespace()), |s: &str| {
s.to_owned()
})
.parse(s)
.parse(s)?;
match Url::parse(&url) {
Ok(url) => Ok((remain, FeedUrl::from(url))),
Err(err) => {
// TODO: represents parse error as type
tracing::warn!("Invalid url: {err}");
Err(nom::Err::Failure(nom::error::Error::new(
remain,
nom::error::ErrorKind::TakeWhile1,
)))
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -166,7 +178,7 @@ mod feed {
Ok((
"",
SubscribeFeedInput {
url: "https://example.ymgyt.io/atom.xml".into(),
url: "https://example.ymgyt.io/atom.xml".try_into().unwrap(),
requirement: Some(Requirement::MUST),
category: Some(Category::new("rust").unwrap())
}
Expand Down
3 changes: 2 additions & 1 deletion crates/synd_term/src/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ratatui::{style::palette::tailwind, widgets::Widget};
use synd_auth::device_flow::{
self, DeviceAccessTokenResponse, DeviceAuthorizationResponse, DeviceFlow,
};
use synd_feed::types::FeedUrl;
use tokio::time::{Instant, Sleep};

use crate::{
Expand Down Expand Up @@ -630,7 +631,7 @@ impl Application {
self.jobs.futures.push(fut);
}

fn unsubscribe_feed(&mut self, url: String) {
fn unsubscribe_feed(&mut self, url: FeedUrl) {
let client = self.client.clone();
let request_seq = self.in_flight.add(RequestId::UnsubscribeFeed);
let fut = async move {
Expand Down
4 changes: 2 additions & 2 deletions crates/synd_term/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub mod query;
#[derive(Error, Debug)]
pub enum SubscribeFeedError {
#[error("invalid feed url: `{feed_url}` ({message})`")]
InvalidFeedUrl { feed_url: String, message: String },
InvalidFeedUrl { feed_url: FeedUrl, message: String },
#[error("internal error: {0}")]
Internal(anyhow::Error),
}
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Client {
}

#[tracing::instrument(skip(self))]
pub async fn unsubscribe_feed(&self, url: String) -> anyhow::Result<()> {
pub async fn unsubscribe_feed(&self, url: FeedUrl) -> anyhow::Result<()> {
let var = mutation::unsubscribe_feed::Variables {
unsubscribe_input: mutation::unsubscribe_feed::UnsubscribeFeedInput { url },
};
Expand Down
8 changes: 5 additions & 3 deletions crates/synd_term/src/client/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod subscribe_feed {
#[allow(dead_code)]
type ID = String;
type Category = crate::client::scalar::Category;
type FeedUrl = crate::client::scalar::FeedUrl;
type Rfc3339Time = crate::client::scalar::Rfc3339Time;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum FeedType {
Expand Down Expand Up @@ -112,7 +113,7 @@ pub mod subscribe_feed {
}
#[derive(Serialize, Debug, Clone, PartialEq, Eq)]
pub struct SubscribeFeedInput {
pub url: String,
pub url: FeedUrl,
pub requirement: Option<Requirement>,
pub category: Option<Category>,
}
Expand All @@ -128,7 +129,7 @@ pub mod subscribe_feed {
#[serde(rename = "type")]
pub type_: FeedType,
pub title: Option<String>,
pub url: String,
pub url: FeedUrl,
pub updated: Option<Rfc3339Time>,
#[serde(rename = "websiteUrl")]
pub website_url: Option<String>,
Expand Down Expand Up @@ -227,6 +228,7 @@ pub mod unsubscribe_feed {
type Int = i64;
#[allow(dead_code)]
type ID = String;
type FeedUrl = crate::client::scalar::FeedUrl;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ResponseCode {
OK,
Expand Down Expand Up @@ -260,7 +262,7 @@ pub mod unsubscribe_feed {
}
#[derive(Serialize, Debug, Clone, PartialEq, Eq)]
pub struct UnsubscribeFeedInput {
pub url: String,
pub url: FeedUrl,
}
#[derive(Serialize, Debug, Clone, PartialEq, Eq)]
pub struct Variables {
Expand Down
9 changes: 6 additions & 3 deletions crates/synd_term/src/client/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod subscription {
#[allow(dead_code)]
type ID = String;
type Category = crate::client::scalar::Category;
type FeedUrl = crate::client::scalar::FeedUrl;
type Rfc3339Time = crate::client::scalar::Rfc3339Time;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum FeedType {
Expand Down Expand Up @@ -91,7 +92,7 @@ pub mod subscription {
#[serde(rename = "type")]
pub type_: FeedType,
pub title: Option<String>,
pub url: String,
pub url: FeedUrl,
pub updated: Option<Rfc3339Time>,
#[serde(rename = "websiteUrl")]
pub website_url: Option<String>,
Expand Down Expand Up @@ -184,6 +185,7 @@ pub mod entries {
#[allow(dead_code)]
type ID = String;
type Category = crate::client::scalar::Category;
type FeedUrl = crate::client::scalar::FeedUrl;
type Rfc3339Time = crate::client::scalar::Rfc3339Time;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Requirement {
Expand Down Expand Up @@ -233,7 +235,7 @@ pub mod entries {
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct FeedMeta {
pub title: Option<String>,
pub url: String,
pub url: FeedUrl,
pub requirement: Option<Requirement>,
pub category: Option<Category>,
}
Expand Down Expand Up @@ -288,6 +290,7 @@ pub mod export_subscription {
type Int = i64;
#[allow(dead_code)]
type ID = String;
type FeedUrl = crate::client::scalar::FeedUrl;
#[derive(Serialize, Debug, Clone, PartialEq, Eq)]
pub struct Variables {
pub after: Option<String>,
Expand Down Expand Up @@ -318,7 +321,7 @@ pub mod export_subscription {
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct ExportSubscriptionOutputFeedsNodes {
pub title: Option<String>,
pub url: String,
pub url: FeedUrl,
}
}
impl graphql_client::GraphQLQuery for ExportSubscription {
Expand Down
1 change: 1 addition & 0 deletions crates/synd_term/src/client/scalar.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub type Category = synd_feed::types::Category<'static>;
pub type FeedUrl = synd_feed::types::FeedUrl;
pub type Rfc3339Time = String;
6 changes: 3 additions & 3 deletions crates/synd_term/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Display;
use synd_auth::device_flow::{DeviceAccessTokenResponse, DeviceAuthorizationResponse};
use synd_feed::types::Category;
use synd_feed::types::{Category, FeedUrl};

use crate::{
application::{Direction, ListAction, RequestSequence},
Expand Down Expand Up @@ -48,14 +48,14 @@ pub enum Command {
input: SubscribeFeedInput,
},
UnsubscribeFeed {
url: String,
url: FeedUrl,
},
CompleteSubscribeFeed {
feed: Feed,
request_seq: RequestSequence,
},
CompleteUnsubscribeFeed {
url: String,
url: FeedUrl,
request_seq: RequestSequence,
},
FetchSubscription {
Expand Down
8 changes: 4 additions & 4 deletions crates/synd_term/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chrono::DateTime;
use schemars::JsonSchema;
use serde::Serialize;
use synd_feed::types::{Category, FeedType, Requirement};
use synd_feed::types::{Category, FeedType, FeedUrl, Requirement};

use crate::{
client::{
Expand Down Expand Up @@ -92,7 +92,7 @@ impl EntryMeta {
pub struct Feed {
pub r#type: Option<FeedType>,
pub title: Option<String>,
pub url: String,
pub url: FeedUrl,
pub updated: Option<Time>,
pub links: Vec<Link>,
pub website_url: Option<String>,
Expand Down Expand Up @@ -184,7 +184,7 @@ pub struct Entry {
pub website_url: Option<String>,
pub summary: Option<String>,
pub feed_title: Option<String>,
pub feed_url: String,
pub feed_url: FeedUrl,
requirement: Option<Requirement>,
category: Option<Category<'static>>,
}
Expand Down Expand Up @@ -238,7 +238,7 @@ impl From<query::export_subscription::ExportSubscriptionOutputFeedsNodes> for Ex
fn from(v: query::export_subscription::ExportSubscriptionOutputFeedsNodes) -> Self {
Self {
title: v.title,
url: v.url,
url: v.url.to_string(),
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/synd_term/src/ui/components/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use ratatui::{
ScrollbarState, StatefulWidget, Table, TableState, Widget, Wrap,
},
};
use synd_feed::types::FeedUrl;

pub struct Entries {
selected_entry_index: usize,
Expand Down Expand Up @@ -59,8 +60,8 @@ impl Entries {
.collect();
}

pub fn remove_unsubscribed_entries(&mut self, url: &str) {
self.entries.retain(|entry| entry.feed_url != url);
pub fn remove_unsubscribed_entries(&mut self, url: &FeedUrl) {
self.entries.retain(|entry| &entry.feed_url != url);
self.apply_filter();
}

Expand Down
10 changes: 5 additions & 5 deletions crates/synd_term/src/ui/components/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use ratatui::{
ScrollbarOrientation, ScrollbarState, StatefulWidget, Table, TableState, Widget,
},
};
use synd_feed::types::FeedType;
use synd_feed::types::{FeedType, FeedUrl};

use crate::{
application::{Direction, IndexOutOfRange, ListAction},
Expand Down Expand Up @@ -45,8 +45,8 @@ impl Subscription {
!self.feeds.is_empty()
}

pub fn is_already_subscribed(&self, url: &str) -> bool {
self.feeds.iter().any(|feed| feed.url == url)
pub fn is_already_subscribed(&self, url: &FeedUrl) -> bool {
self.feeds.iter().any(|feed| &feed.url == url)
}

pub fn selected_feed(&self) -> Option<&types::Feed> {
Expand Down Expand Up @@ -87,8 +87,8 @@ impl Subscription {
self.apply_filter();
}

pub fn remove_unsubscribed_feed(&mut self, url: &str) {
self.feeds.retain(|feed_meta| feed_meta.url != url);
pub fn remove_unsubscribed_feed(&mut self, url: &FeedUrl) {
self.feeds.retain(|feed_meta| &feed_meta.url != url);
self.apply_filter();
self.move_selection(&Direction::Up);
}
Expand Down

0 comments on commit 7503ae0

Please sign in to comment.