Compare commits

...

2 Commits

Author SHA1 Message Date
Fries b7d11cb964 use async mutex
looks like the best way to do the state thing after all is global so i
should use async mutexes so stuff doesn't block.
2023-07-03 22:15:16 -07:00
Fries 6f9e896592 change spelling of seperator to separator
looks like i spelt that wrong..
2023-07-03 22:15:16 -07:00
11 changed files with 48 additions and 38 deletions

10
Cargo.lock generated
View File

@ -443,6 +443,15 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "fsevent-sys"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2"
dependencies = [
"libc",
]
[[package]]
name = "futures"
version = "0.3.28"
@ -910,6 +919,7 @@ checksum = "5738a2795d57ea20abec2d6d76c6081186709c0024187cd5977265eda6598b51"
dependencies = [
"bitflags 1.3.2",
"filetime",
"fsevent-sys",
"inotify",
"kqueue",
"libc",

View File

@ -50,3 +50,4 @@ default-features = false
[dependencies.notify]
version = "6"
default-features = false
features = ["macos_fsevent"]

View File

@ -8,9 +8,9 @@ pub(crate) struct Arguments {
#[arg(
long,
short,
help = "a seperator string to seperate the url from the name. defaults to : with a space after that."
help = "a separator string to seperate the url from the name. defaults to : with a space after that."
)]
pub(crate) seperator: Option<String>,
pub(crate) separator: Option<String>,
#[command(subcommand)]
pub(crate) command: Commands,
}

View File

@ -10,7 +10,7 @@ pub(crate) fn add(
path: &Path,
url: &String,
name: &Option<String>,
seperator: &String,
separator: &String,
) -> Result<(), Error> {
let names_file = names::read_names_file(path)?;
let mut names = names::load_names(names_file)?;
@ -36,7 +36,7 @@ pub(crate) fn add(
std::fs::write(path, json).unwrap();
println!(
"added {} to names.json",
site_string(&site, PrintOptions::All, seperator)
site_string(&site, PrintOptions::All, separator)
);
Ok(())

View File

@ -11,7 +11,7 @@ pub(crate) fn print(
path: &Path,
filter: &Option<String>,
group: &PrintGroup,
seperator: &String,
separator: &String,
json: bool,
) -> Result<(), Error> {
let names_file = names::read_names_file(path)?;
@ -25,7 +25,7 @@ pub(crate) fn print(
data: "this url was not found in names.json".into(),
});
}
return filter_site(&names[0], json, seperator, group);
return filter_site(&names[0], json, separator, group);
}
for site in names {
@ -34,7 +34,7 @@ pub(crate) fn print(
continue;
}
printing(seperator, &site, group);
printing(separator, &site, group);
}
Ok(())
@ -43,7 +43,7 @@ pub(crate) fn print(
fn filter_site(
site: &Site,
json: bool,
seperator: &String,
separator: &String,
group: &PrintGroup,
) -> Result<(), Error> {
if json {
@ -51,7 +51,7 @@ fn filter_site(
return Ok(());
}
return Ok(printing(seperator, site, group));
return Ok(printing(separator, site, group));
}
fn json_printing(site: &Site) -> Result<(), Error> {
@ -67,8 +67,8 @@ fn json_printing(site: &Site) -> Result<(), Error> {
}
}
fn printing(seperator: &String, site: &Site, group: &PrintGroup) {
let string = site_string(site, print_group_to_options(group), seperator);
fn printing(separator: &String, site: &Site, group: &PrintGroup) {
let string = site_string(site, print_group_to_options(group), separator);
println!("{}", string);
}

View File

@ -3,7 +3,7 @@ use std::path::Path;
use crate::commands::utils::{site_string, PrintOptions};
pub(crate) fn remove(path: &Path, url: &String, seperator: &String) -> Result<(), Error> {
pub(crate) fn remove(path: &Path, url: &String, separator: &String) -> Result<(), Error> {
let names_file = names::read_names_file(path)?;
let mut names = names::load_names(names_file)?;
@ -11,7 +11,7 @@ pub(crate) fn remove(path: &Path, url: &String, seperator: &String) -> Result<()
if &site.url == url {
println!(
"removing {} from names.json",
site_string(&site, PrintOptions::All, seperator)
site_string(&site, PrintOptions::All, separator)
);
}
&site.url != url

View File

@ -6,7 +6,7 @@ pub(super) enum PrintOptions {
All,
}
pub(super) fn site_string(site: &Site, options: PrintOptions, seperator: &String) -> String {
pub(super) fn site_string(site: &Site, options: PrintOptions, separator: &String) -> String {
let mut string = String::new();
if matches!(options, PrintOptions::Url) || matches!(options, PrintOptions::All) {
@ -18,7 +18,7 @@ pub(super) fn site_string(site: &Site, options: PrintOptions, seperator: &String
return string;
}
if !string.is_empty() {
string += &format!("{}{}", seperator, name)
string += &format!("{}{}", separator, name)
} else {
string += name;
}

View File

@ -13,7 +13,7 @@ fn main() -> Result<(), Error> {
let default_path = directories::get_names_path()?;
let args = Arguments::parse();
let seperator = args.seperator.unwrap_or(": ".into());
let separator = args.separator.unwrap_or(": ".into());
let path = match &args.path {
Some(path) => Path::new(path),
@ -25,9 +25,9 @@ fn main() -> Result<(), Error> {
filter,
group,
json,
} => print(path, filter, group, &seperator, *json)?,
Commands::Add { url, name } => add(path, url, name, &seperator)?,
Commands::Remove { url } => remove(path, url, &seperator)?,
} => print(path, filter, group, &separator, *json)?,
Commands::Add { url, name } => add(path, url, name, &separator)?,
Commands::Remove { url } => remove(path, url, &separator)?,
};
Ok(())

View File

@ -1,7 +1,7 @@
use crate::{
assets::ErrorTemplate,
links::{next_url, previous_url},
sites::{ Names},
sites::get_global_names,
};
use rocket::{
@ -28,25 +28,25 @@ pub fn index() -> &'static str {
}
#[get("/previous?<source_url>")]
pub fn previous(source_url: String) -> Result<Redirect, Status> {
match previous_url(&source_url, &Names::global()) {
pub async fn previous(source_url: String) -> Result<Redirect, Status> {
match previous_url(&source_url, &get_global_names().await) {
Some(url) => Ok(Redirect::to(format!("https://{}", url))),
None => Err(Status::NotFound),
}
}
#[get("/next?<source_url>")]
pub fn next(source_url: String) -> Result<Redirect, Status> {
match next_url(&source_url, &Names::global()) {
pub async fn next(source_url: String) -> Result<Redirect, Status> {
match next_url(&source_url, &get_global_names().await) {
Some(url) => Ok(Redirect::to(format!("https://{}", url))),
None => Err(Status::NotFound),
}
}
#[get("/name?<source_url>")]
pub fn name(source_url: String) -> Result<Json<JsonResponse>, Status> {
let previous_site_name = previous_url(&source_url, &Names::global());
let next_site_name = next_url(&source_url, &Names::global());
pub async fn name(source_url: String) -> Result<Json<JsonResponse>, Status> {
let previous_site_name = previous_url(&source_url, &get_global_names().await);
let next_site_name = next_url(&source_url, &get_global_names().await);
if previous_site_name.is_none() && next_site_name.is_none() {
return Err(Status::NotFound);

View File

@ -1,19 +1,17 @@
use rocket::tokio::sync::Mutex;
use shared::{
directories,
errors::{Error, ErrorStatus},
names::{self, Site},
};
use std::sync::{Mutex, OnceLock};
use std::sync::{OnceLock};
pub struct Names {}
pub async fn get_global_names() -> Vec<Site> {
NAMES.get().unwrap().lock().await.clone()
}
impl Names {
pub fn global() -> Vec<Site> {
NAMES.get().unwrap().lock().unwrap().clone()
}
pub fn set() {
*NAMES.get().unwrap().lock().unwrap() = get_names();
}
pub fn set_names() {
*NAMES.get().unwrap().blocking_lock() = get_names();
}
static NAMES: OnceLock<Mutex<Vec<Site>>> = OnceLock::new();

View File

@ -1,7 +1,8 @@
use notify::{Result, Watcher};
use shared::directories;
use crate::sites::Names;
use crate::sites;
pub(crate) fn hot_reloading() {
let (tx, rx) = std::sync::mpsc::channel();
@ -21,7 +22,7 @@ fn watch(res: Result<notify::Event>) {
match res {
Ok(event) => {
if event.kind.is_modify() {
Names::set();
sites::set_names();
}
}
Err(err) => println!("Error: {}", err),