Skip to content

Commit

Permalink
feat(term): normalize category
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Apr 13, 2024
1 parent 40dd049 commit b25a147
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
20 changes: 15 additions & 5 deletions crates/synd_term/src/application/input_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use thiserror::Error;

use crate::{
client::mutation::subscribe_feed::SubscribeFeedInput,
config::Categories,
types::{self},
ui,
};

pub use feed::requirement as parse_requirement;
Expand Down Expand Up @@ -42,16 +42,26 @@ impl<'a> InputParser<'a> {
Self { input }
}

pub(super) fn parse_feed_subscription(&self) -> Result<SubscribeFeedInput, ParseFeedError> {
feed::parse(self.input).map_err(|e| ParseFeedError::Parse(e.to_string()))
pub(super) fn parse_feed_subscription(
&self,
categories: &Categories,
) -> Result<SubscribeFeedInput, ParseFeedError> {
feed::parse(self.input)
.map(|mut input| {
if let Some(category) = input.category {
input.category = Some(categories.normalize(category));
}
input
})
.map_err(|e| ParseFeedError::Parse(e.to_string()))
}

pub(super) fn edit_feed_prompt(feed: &types::Feed) -> String {
format!(
"{}\n{requirement} {category} {feed_url}",
Self::SUSBSCRIBE_FEED_PROMPT,
requirement = feed.requirement.unwrap_or(ui::DEFAULT_REQUIREMNET),
category = feed.category.as_ref().unwrap_or(ui::default_category()),
requirement = feed.requirement(),
category = feed.category(),
feed_url = feed.url,
)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/synd_term/src/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ impl Application {
// the terminal state becomes strange after editing in the editor
self.terminal.force_redraw();

let fut = match InputParser::new(input.as_str()).parse_feed_subscription() {
let fut = match InputParser::new(input.as_str()).parse_feed_subscription(&self.categories) {
Ok(input) => {
// Check for the duplicate subscription
if self
Expand Down Expand Up @@ -534,7 +534,7 @@ impl Application {
// the terminal state becomes strange after editing in the editor
self.terminal.force_redraw();

let fut = match InputParser::new(input.as_str()).parse_feed_subscription() {
let fut = match InputParser::new(input.as_str()).parse_feed_subscription(&self.categories) {
// Strictly, if the URL of the feed changed before and after an update
// it is not considered an edit, so it could be considered an error
// but currently we are allowing it
Expand Down
29 changes: 28 additions & 1 deletion crates/synd_term/src/config/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,51 @@ use synd_feed::types::Category;
#[derive(Deserialize)]
pub struct Categories {
categories: HashMap<String, Entry>,
#[serde(skip)]
aliases: HashMap<String, String>,
}

impl Categories {
pub fn default_toml() -> Self {
let s = include_str!("../../../../categories.toml");
toml::from_str(s).unwrap()
let mut c: Self = toml::from_str(s).unwrap();
c.update_aliases();
c
}

pub fn icon(&self, category: &Category<'_>) -> Option<&Icon> {
self.categories
.get(category.as_str())
.map(|entry| &entry.icon)
}

pub fn normalize(&self, category: Category<'static>) -> Category<'static> {
match self.aliases.get(category.as_str()) {
Some(normalized) => Category::new(normalized.to_owned()).unwrap_or(category),
None => category,
}
}

fn update_aliases(&mut self) {
let new_map = self.categories.iter().fold(
HashMap::with_capacity(self.categories.len()),
|mut m, (category, entry)| {
entry.aliases.iter().for_each(|alias| {
m.insert(alias.to_lowercase(), category.to_lowercase());
});
m
},
);

self.aliases = new_map;
}
}

#[derive(Deserialize)]
struct Entry {
icon: Icon,
#[serde(default)]
aliases: Vec<String>,
}

#[derive(Deserialize)]
Expand Down

0 comments on commit b25a147

Please sign in to comment.