Skip to content

Commit

Permalink
build(version): update
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjani committed Jan 17, 2023
1 parent 3609244 commit 1461663
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 88 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.19.1] - 2023-01-17

- Fix config file default location
- Throw errors if the-way gist has duplicates

## [0.19.0] - 2023-01-17

- Fix gist syncing bug (Issue #[143](https://github.com/out-of-cheese-error/the-way/issues/143))
Expand Down Expand Up @@ -444,6 +449,8 @@ This uses the correct release directory now based on the TARGET environment vari
- A first working version of the-way
- cargo install option

[0.19.1]: https://github.com/out-of-cheese-error/the-way/compare/v0.19.0...v0.19.1

[0.19.0]: https://github.com/out-of-cheese-error/the-way/compare/v0.18.0...v0.19.0

[0.18.0]: https://github.com/out-of-cheese-error/the-way/compare/v0.17.1...v0.18.0
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "the-way"
version = "0.19.0"
version = "0.19.1"
edition = "2021"
authors = ["Ninjani"]
description = "A code snippets manager for your terminal"
Expand Down
20 changes: 8 additions & 12 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl TheWayConfig {
"theme = 'base16-ocean.dark'\ndb_dir = 'the_way_db'\nthemes_dir = 'the_way_themes'\ncopy_cmd = '{}'",
copy_cmd
);
write!(&mut buffered, "{}", contents)?;
write!(&mut buffered, "{contents}")?;
Ok(())
}

Expand All @@ -98,13 +98,13 @@ impl TheWayConfig {
fn make_dirs(&self) -> color_eyre::Result<()> {
if !self.db_dir.exists() {
fs::create_dir_all(&self.db_dir).map_err(|e: io::Error| LostTheWay::ConfigError {
message: format!("Couldn't create db dir {:?}, {}", self.db_dir, e),
message: format!("Couldn't create db dir {:?}, {e}", self.db_dir),
})?;
}
if !self.themes_dir.exists() {
fs::create_dir_all(&self.themes_dir).map_err(|e: io::Error| {
LostTheWay::ConfigError {
message: format!("Couldn't create themes dir {:?}, {}", self.themes_dir, e),
message: format!("Couldn't create themes dir {:?}, {e}", self.themes_dir),
}
})?;
}
Expand All @@ -113,9 +113,7 @@ impl TheWayConfig {

/// Get default configuration file location according to XDG specification
fn get_default_config_file() -> color_eyre::Result<PathBuf> {
let dir = get_project_dir()?;
let config_dir = dir.config_dir();
Ok(config_dir.join(format!("{}.toml", NAME)))
Ok(confy::get_configuration_file_path(NAME, None)?)
}

/// Gets the current config file location
Expand All @@ -128,12 +126,11 @@ impl TheWayConfig {
Ok(path)
} else {
let error: color_eyre::Result<PathBuf> = Err(LostTheWay::ConfigError {
message: format!("No such file {}", file),
message: format!("No such file {file}"),
}
.into());
error.suggestion(format!(
"Use `the-way config default {}` to write out the default configuration",
file
"Use `the-way config default {file}` to write out the default configuration",
))
}
}
Expand All @@ -154,12 +151,11 @@ impl TheWayConfig {
Ok(config)
} else {
let error: color_eyre::Result<Self> = Err(LostTheWay::ConfigError {
message: format!("No such file {}", file),
message: format!("No such file {file}"),
}
.into());
error.suggestion(format!(
"Use `the-way config default {}` to write out the default configuration",
file
"Use `the-way config default {file}` to write out the default configuration",
))
}
}
Expand Down
27 changes: 12 additions & 15 deletions src/gist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<'a> GistClient<'a> {
.set("user-agent", USER_AGENT)
.set("content-type", ACCEPT);
if let Some(access_token) = &self.access_token {
request = request.set("Authorization", &format!("token {}", access_token));
request = request.set("Authorization", &format!("token {access_token}"));
}
request
}
Expand All @@ -85,11 +85,11 @@ impl<'a> GistClient<'a> {
Ok(response
.into_json::<Gist>()
.map_err(|e| LostTheWay::SyncError {
message: format!("{}", e),
message: format!("{e}"),
})?)
}
Err(ureq::Error::Status(code, response)) => Err(LostTheWay::SyncError {
message: format!("{} {}", code, response.into_string()?),
message: format!("{code} {}", response.into_string()?),
})
.suggestion(
"Make sure your GitHub access token is valid.\n\
Expand All @@ -109,7 +109,7 @@ impl<'a> GistClient<'a> {

/// Create a new Gist with the given payload
pub fn create_gist(&self, payload: &CreateGistPayload<'_>) -> color_eyre::Result<Gist> {
let url = format!("{}{}/gists", GITHUB_API_URL, GITHUB_BASE_PATH);
let url = format!("{GITHUB_API_URL}{GITHUB_BASE_PATH}/gists");
let response = self
.add_headers(self.client.post(&url))
.send_json(serde_json::to_value(payload)?);
Expand All @@ -122,20 +122,17 @@ impl<'a> GistClient<'a> {
gist_id: &str,
payload: &UpdateGistPayload<'_>,
) -> color_eyre::Result<Gist> {
let url = format!("{}{}/gists", GITHUB_API_URL, GITHUB_BASE_PATH);
let url = format!("{GITHUB_API_URL}{GITHUB_BASE_PATH}/gists");
let response = self
.add_headers(
self.client
.request("PATCH", &format!("{}/{}", url, gist_id)),
)
.add_headers(self.client.request("PATCH", &format!("{url}/{gist_id}")))
.send_json(serde_json::to_value(payload)?);
Self::get_response(response)
}

/// Retrieve a Gist by ID
pub fn get_gist(&self, gist_id: &str) -> color_eyre::Result<Gist> {
let url = format!("{}{}/gists", GITHUB_API_URL, GITHUB_BASE_PATH);
let response = self.add_headers(self.client.get(&format!("{}/{}", url, gist_id)));
let url = format!("{GITHUB_API_URL}{GITHUB_BASE_PATH}/gists");
let response = self.add_headers(self.client.get(&format!("{url}/{gist_id}")));
Self::get_response(response.call())
}

Expand All @@ -145,19 +142,19 @@ impl<'a> GistClient<'a> {
match gist_id {
Some(gist_id) => self.get_gist(gist_id),
None => Err(LostTheWay::GistUrlError {
message: format!("Problem extracting gist ID from {}", gist_url),
message: format!("Problem extracting gist ID from {gist_url}"),
})
.suggestion("The URL should look like https://gist.github.com/<user>/<gist_id>."),
}
}

/// Delete Gist by ID
pub fn delete_gist(&self, gist_id: &str) -> color_eyre::Result<()> {
let url = format!("{}{}/gists", GITHUB_API_URL, GITHUB_BASE_PATH);
let status = self.add_headers(self.client.delete(&format!("{}/{}", url, gist_id)));
let url = format!("{GITHUB_API_URL}{GITHUB_BASE_PATH}/gists");
let status = self.add_headers(self.client.delete(&format!("{url}/{gist_id}")));
if status.call().is_err() {
Err(LostTheWay::GistUrlError {
message: format!("Couldn't delete gist with ID {}", gist_id),
message: format!("Couldn't delete gist with ID {gist_id}"),
}
.into())
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ impl CodeHighlight {
})
.suggestion("Something's fishy with the filename, valid Unicode?")?;
// Copy theme to theme file directory
let new_theme_file = self.syntect_dir.join(format!("{}.tmTheme", basename));
let new_theme_file = self.syntect_dir.join(format!("{basename}.tmTheme"));
fs::copy(theme_file, new_theme_file)?;
self.theme_set.themes.insert(basename.to_owned(), theme);
Ok(basename.to_owned())
Expand Down
62 changes: 37 additions & 25 deletions src/the_way/gist.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Code related to dealing with Gists
use chrono::Utc;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};

use color_eyre::Help;

Expand All @@ -26,7 +26,7 @@ pub(crate) fn parse_index_line(
let caps = re
.captures(index_line)
.ok_or(LostTheWay::GistFormattingError {
message: format!("Index line isn't formatted correctly:\n{}", index_line),
message: format!("Index line isn't formatted correctly:\n{index_line}"),
})?;
let description = caps[1].to_owned();
let index = caps[2].parse::<usize>()?;
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Snippet {
let mut snippets = Vec::new();
for (file_name, gist_file) in &gist.files {
let code = &gist_file.content;
let description = format!("{} - {} - {}", gist.description, gist.id, file_name);
let description = format!("{} - {} - {file_name}", gist.description, gist.id);
let language = &gist_file.language.to_ascii_lowercase();
let tags = "gist";
let extension = Language::get_extension(language, languages);
Expand All @@ -108,12 +108,12 @@ impl Snippet {
.split('.')
.next()
.ok_or(LostTheWay::GistFormattingError {
message: format!("Filename {} missing extension", file_name),
message: format!("Filename {file_name} missing extension"),
})?
.split('_')
.nth(1)
.ok_or(LostTheWay::GistFormattingError {
message: format!("Filename {} missing index", file_name),
message: format!("Filename {file_name} missing index"),
})?
.parse()?
};
Expand Down Expand Up @@ -149,23 +149,35 @@ impl Snippet {
let mut index_mapping = HashMap::new();
for line in index_snippet.code.trim().split('\n').skip(1) {
let (index, description, tags) = parse_index_line(line)?;
index_mapping.insert(index, (description, tags));
let val = index_mapping.insert(index, (description, tags));
if val.is_some() {
return Err(LostTheWay::GistFormattingError {
message: format!("Index file contains duplicate index {index}"),
}
.into());
}
}
Ok(snippets
.into_iter()
.filter(|s| s.index != 0)
.map(|mut snippet| {
if let Some((description, tags)) = index_mapping.get(&snippet.index) {
snippet.description = description.clone();
snippet.tags = tags.clone();
Ok(snippet)
} else {
Err(LostTheWay::GistFormattingError {
message: format!("Snippet index {} not found in index file", snippet.index),
})
let mut found_indices = HashSet::new();
let mut gist_snippets = Vec::new();
for mut snippet in snippets.into_iter().filter(|s| s.index != 0) {
if found_indices.contains(&snippet.index) {
return Err(LostTheWay::GistFormattingError {
message: format!("Gist contains duplicate entry for index {}", snippet.index),
}
})
.collect::<Result<Vec<_>, _>>()?)
.into());
}
let (description, tags) =
index_mapping
.get(&snippet.index)
.ok_or(LostTheWay::GistFormattingError {
message: format!("Index file doesn't contain index {}", snippet.index),
})?;
snippet.description = description.clone();
snippet.tags = tags.clone();
found_indices.insert(snippet.index);
gist_snippets.push(snippet);
}
Ok(gist_snippets)
}
}

Expand Down Expand Up @@ -397,7 +409,7 @@ impl TheWay {
// add to local
let gist_snippet = gist_snippets.get(&snippet_index).ok_or(
LostTheWay::GistFormattingError {
message: format!("Invalid snippet index {}", snippet_index),
message: format!("Invalid snippet index {snippet_index}"),
},
)?;
add_snippets.push(gist_snippet);
Expand Down Expand Up @@ -458,7 +470,7 @@ impl TheWay {
if action == SyncAction::DeletedLocal && !delete {
continue;
}
self.color_print(&format!("{} snippet(s) {}\n", count, action))?;
self.color_print(&format!("{count} snippet(s) {action}\n"))?;
}
self.color_print(&format!("\nGist: {}\n", gist.html_url))?;
Ok(())
Expand All @@ -472,18 +484,18 @@ fn get_gist_snippet_index(file: &str) -> color_eyre::Result<usize> {
.split('.')
.next()
.ok_or(LostTheWay::GistFormattingError {
message: format!("Invalid filename {}: No .", file),
message: format!("Invalid filename {file}: No ."),
})
.suggestion(suggestion)?
.split('_')
.last()
.ok_or(LostTheWay::GistFormattingError {
message: format!("Invalid filename {}: No _", file),
message: format!("Invalid filename {file}: No _"),
})
.suggestion(suggestion)?
.parse::<usize>()
.map_err(|e| LostTheWay::GistFormattingError {
message: format!("Invalid filename {}: {}", file, e),
message: format!("Invalid filename {file}: {e}"),
})
.suggestion(suggestion)?;
Ok(snippet_index)
Expand Down
Loading

0 comments on commit 1461663

Please sign in to comment.