Compare commits
11 Commits
dd2d19ee13
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 59b4ad5f2c | |||
| bc0f71956b | |||
| 0276bfe515 | |||
| 143e177b8c | |||
| 0ba4e21ae3 | |||
| 221f75e976 | |||
| 498cb43390 | |||
| a168c00ee7 | |||
| 2e942a9b31 | |||
| 2e51ae8064 | |||
| 8c11630801 |
112
Cargo.lock
generated
112
Cargo.lock
generated
@@ -150,13 +150,15 @@ dependencies = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "brittling"
|
name = "brittle"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ammonia",
|
"ammonia",
|
||||||
|
"brittle_macros",
|
||||||
"clap",
|
"clap",
|
||||||
"crossterm",
|
"crossterm",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
|
"futures",
|
||||||
"html-escape",
|
"html-escape",
|
||||||
"log",
|
"log",
|
||||||
"open",
|
"open",
|
||||||
@@ -170,6 +172,14 @@ dependencies = [
|
|||||||
"unicode-general-category",
|
"unicode-general-category",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "brittle_macros"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.112",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bumpalo"
|
name = "bumpalo"
|
||||||
version = "3.19.1"
|
version = "3.19.1"
|
||||||
@@ -250,7 +260,7 @@ dependencies = [
|
|||||||
"heck",
|
"heck",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -380,7 +390,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "13b588ba4ac1a99f7f2964d24b3d896ddc6bf847ee3855dbd4366f058cfcd331"
|
checksum = "13b588ba4ac1a99f7f2964d24b3d896ddc6bf847ee3855dbd4366f058cfcd331"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -404,7 +414,7 @@ dependencies = [
|
|||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"strsim",
|
"strsim",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -415,7 +425,7 @@ checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"darling_core",
|
"darling_core",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -452,7 +462,7 @@ dependencies = [
|
|||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"rustc_version",
|
"rustc_version",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -473,7 +483,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -654,6 +664,21 @@ dependencies = [
|
|||||||
"new_debug_unreachable",
|
"new_debug_unreachable",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures"
|
||||||
|
version = "0.3.31"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
|
||||||
|
dependencies = [
|
||||||
|
"futures-channel",
|
||||||
|
"futures-core",
|
||||||
|
"futures-executor",
|
||||||
|
"futures-io",
|
||||||
|
"futures-sink",
|
||||||
|
"futures-task",
|
||||||
|
"futures-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-channel"
|
name = "futures-channel"
|
||||||
version = "0.3.31"
|
version = "0.3.31"
|
||||||
@@ -661,6 +686,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"
|
checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures-core",
|
"futures-core",
|
||||||
|
"futures-sink",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -669,6 +695,34 @@ version = "0.3.31"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
|
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-executor"
|
||||||
|
version = "0.3.31"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f"
|
||||||
|
dependencies = [
|
||||||
|
"futures-core",
|
||||||
|
"futures-task",
|
||||||
|
"futures-util",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-io"
|
||||||
|
version = "0.3.31"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-macro"
|
||||||
|
version = "0.3.31"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.112",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-sink"
|
name = "futures-sink"
|
||||||
version = "0.3.31"
|
version = "0.3.31"
|
||||||
@@ -687,10 +741,16 @@ version = "0.3.31"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
|
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"futures-channel",
|
||||||
"futures-core",
|
"futures-core",
|
||||||
|
"futures-io",
|
||||||
|
"futures-macro",
|
||||||
|
"futures-sink",
|
||||||
"futures-task",
|
"futures-task",
|
||||||
|
"memchr",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"pin-utils",
|
"pin-utils",
|
||||||
|
"slab",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1044,7 +1104,7 @@ dependencies = [
|
|||||||
"indoc",
|
"indoc",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1133,7 +1193,7 @@ checksum = "b787bebb543f8969132630c51fd0afab173a86c6abae56ff3b9e5e3e3f9f6e58"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1267,7 +1327,7 @@ checksum = "ac84fd3f360fcc43dc5f5d186f02a94192761a080e8bc58621ad4d12296a58cf"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1375,7 +1435,7 @@ checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1442,7 +1502,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1537,7 +1597,7 @@ dependencies = [
|
|||||||
"pest_meta",
|
"pest_meta",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1590,7 +1650,7 @@ dependencies = [
|
|||||||
"phf_shared",
|
"phf_shared",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2010,7 +2070,7 @@ checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2190,7 +2250,7 @@ dependencies = [
|
|||||||
"heck",
|
"heck",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2212,9 +2272,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.111"
|
version = "2.0.112"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87"
|
checksum = "21f182278bf2d2bcb3c88b1b08a37df029d71ce3d3ae26168e3c653b213b99d4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@@ -2238,7 +2298,7 @@ checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2386,7 +2446,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2397,7 +2457,7 @@ checksum = "3ff15c8ecd7de3849db632e14d18d2571fa09dfc5ed93479bc4485c7a517c913"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2456,7 +2516,7 @@ checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2759,7 +2819,7 @@ dependencies = [
|
|||||||
"bumpalo",
|
"bumpalo",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
"wasm-bindgen-shared",
|
"wasm-bindgen-shared",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -3110,7 +3170,7 @@ checksum = "b659052874eb698efe5b9e8cf382204678a0086ebf46982b79d6ca3182927e5d"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
"synstructure",
|
"synstructure",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -3131,7 +3191,7 @@ checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
"synstructure",
|
"synstructure",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -3171,7 +3231,7 @@ checksum = "eadce39539ca5cb3985590102671f2567e659fca9666581ad3411d59207951f3"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.111",
|
"syn 2.0.112",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
|
[workspace]
|
||||||
|
members = [".", "macros"]
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "brittling"
|
name = "brittle"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
@@ -19,3 +22,5 @@ static_cell = "2.1.1"
|
|||||||
textwrap = "0.16.2"
|
textwrap = "0.16.2"
|
||||||
tokio = { version = "1.48.0", features = ["full"] }
|
tokio = { version = "1.48.0", features = ["full"] }
|
||||||
unicode-general-category = "1.1.0"
|
unicode-general-category = "1.1.0"
|
||||||
|
brittle_macros = { path = "macros" }
|
||||||
|
futures = "0.3.31"
|
||||||
|
|||||||
12
macros/Cargo.toml
Normal file
12
macros/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[package]
|
||||||
|
name = "brittle_macros"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
proc-macro = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
quote = "1.0.42"
|
||||||
|
syn = "2.0.112"
|
||||||
|
|
||||||
140
macros/src/lib.rs
Normal file
140
macros/src/lib.rs
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
// TODO: Clean this up
|
||||||
|
|
||||||
|
use proc_macro::TokenStream;
|
||||||
|
use quote::{format_ident, quote};
|
||||||
|
use syn::{FnArg, ImplItem, ItemImpl, ReturnType, Type, parse_macro_input};
|
||||||
|
|
||||||
|
#[proc_macro_attribute]
|
||||||
|
pub fn component(attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
|
let mut input = parse_macro_input!(item as ItemImpl);
|
||||||
|
let struct_name = &input.self_ty;
|
||||||
|
|
||||||
|
// Use the attribute argument as the Enum name,
|
||||||
|
// or fallback to the [Struct]Action logic if empty.
|
||||||
|
let enum_name = if !attr.is_empty() {
|
||||||
|
format_ident!("{}", attr.to_string().trim())
|
||||||
|
} else {
|
||||||
|
let struct_name_str = quote!(#struct_name).to_string().replace(" ", "");
|
||||||
|
format_ident!("{}Action", struct_name_str)
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut enum_variants = Vec::new();
|
||||||
|
let mut match_arms = Vec::new();
|
||||||
|
|
||||||
|
for item in &mut input.items {
|
||||||
|
if let ImplItem::Fn(method) = item {
|
||||||
|
let has_action = method
|
||||||
|
.attrs
|
||||||
|
.iter()
|
||||||
|
.any(|attr| attr.path().is_ident("action"));
|
||||||
|
|
||||||
|
if has_action {
|
||||||
|
method.attrs.retain(|attr| !attr.path().is_ident("action"));
|
||||||
|
|
||||||
|
let method_name = &method.sig.ident;
|
||||||
|
let variant_name = format_ident!(
|
||||||
|
"{}",
|
||||||
|
snake_to_pascal(&method_name.to_string())
|
||||||
|
);
|
||||||
|
|
||||||
|
let returns_result = match &method.sig.output {
|
||||||
|
ReturnType::Default => false,
|
||||||
|
ReturnType::Type(_, ty) => {
|
||||||
|
quote!(#ty).to_string().contains("Result")
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut variant_fields = Vec::new();
|
||||||
|
let mut call_args = Vec::new();
|
||||||
|
let mut variant_arg_names = Vec::new();
|
||||||
|
|
||||||
|
// Inspect arguments to decide what goes into the Enum and what is injected
|
||||||
|
for arg in method.sig.inputs.iter().skip(1) {
|
||||||
|
// Skip 'self'
|
||||||
|
if let FnArg::Typed(pat_type) = arg {
|
||||||
|
if is_sender_type(&pat_type.ty) {
|
||||||
|
// Inject the 'tx' from handle_action, don't add to Enum
|
||||||
|
call_args.push(quote!(tx));
|
||||||
|
} else {
|
||||||
|
// This is a data argument, add to Enum
|
||||||
|
let ty = &pat_type.ty;
|
||||||
|
variant_fields.push(quote!(#ty));
|
||||||
|
|
||||||
|
let arg_id = format_ident!(
|
||||||
|
"arg_{}",
|
||||||
|
variant_arg_names.len()
|
||||||
|
);
|
||||||
|
variant_arg_names.push(arg_id.clone());
|
||||||
|
call_args.push(quote!(#arg_id));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate Enum Variant
|
||||||
|
if variant_fields.is_empty() {
|
||||||
|
enum_variants.push(quote!(#variant_name));
|
||||||
|
} else {
|
||||||
|
enum_variants
|
||||||
|
.push(quote!(#variant_name(#(#variant_fields),*)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate Match Arm
|
||||||
|
let pattern = if variant_arg_names.is_empty() {
|
||||||
|
quote!(#enum_name::#variant_name)
|
||||||
|
} else {
|
||||||
|
quote!(#enum_name::#variant_name(#(#variant_arg_names),*))
|
||||||
|
};
|
||||||
|
|
||||||
|
let call = if returns_result {
|
||||||
|
quote!(self.#method_name(#(#call_args),*))
|
||||||
|
} else {
|
||||||
|
quote!({ self.#method_name(#(#call_args),*); ::core::result::Result::Ok(()) })
|
||||||
|
};
|
||||||
|
|
||||||
|
match_arms.push(quote!(#pattern => #call));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let expanded = quote! {
|
||||||
|
#input
|
||||||
|
|
||||||
|
#[derive(::core::clone::Clone, ::core::fmt::Debug)]
|
||||||
|
pub enum #enum_name {
|
||||||
|
#(#enum_variants),*
|
||||||
|
}
|
||||||
|
|
||||||
|
impl crate::app::common::Component<#enum_name> for #struct_name {
|
||||||
|
fn handle_action(
|
||||||
|
&mut self,
|
||||||
|
action: #enum_name,
|
||||||
|
tx: &'static ::tokio::sync::mpsc::UnboundedSender<crate::app::Action>,
|
||||||
|
) -> ::core::result::Result<(), ::tokio::sync::mpsc::error::SendError<crate::app::Action>> {
|
||||||
|
match action {
|
||||||
|
#(#match_arms),*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TokenStream::from(expanded)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_sender_type(ty: &Type) -> bool {
|
||||||
|
let s = quote!(#ty).to_string();
|
||||||
|
s.contains("UnboundedSender")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn snake_to_pascal(s: &str) -> String {
|
||||||
|
s.split('_')
|
||||||
|
.map(|word| {
|
||||||
|
let mut chars = word.chars();
|
||||||
|
match chars.next() {
|
||||||
|
None => String::new(),
|
||||||
|
Some(f) => {
|
||||||
|
f.to_uppercase().collect::<String>() + chars.as_str()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
299
src/app.rs
299
src/app.rs
@@ -1,22 +1,27 @@
|
|||||||
pub mod common;
|
pub mod common;
|
||||||
|
pub mod run;
|
||||||
pub mod seeding;
|
pub mod seeding;
|
||||||
pub mod snowballing;
|
pub mod snowballing;
|
||||||
|
|
||||||
|
use futures::StreamExt;
|
||||||
use log::{error, info, warn};
|
use log::{error, info, warn};
|
||||||
use ratatui::crossterm::event::KeyCode;
|
use ratatui::crossterm::event::KeyCode;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::{
|
use tokio::{
|
||||||
sync::mpsc::{self, error::SendError},
|
sync::mpsc::{UnboundedSender, error::SendError},
|
||||||
time::sleep,
|
time::sleep,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
app::common::Component,
|
app::{run::Action, snowballing::ActivePane},
|
||||||
literature::{Publication, SnowballingHistory, get_publication_by_id},
|
literature::{
|
||||||
|
Publication, SnowballingHistory, get_citing_works_stream,
|
||||||
|
get_publication_by_id, get_references_stream,
|
||||||
|
},
|
||||||
status_error, status_info,
|
status_error, status_info,
|
||||||
};
|
};
|
||||||
|
use brittle_macros::component;
|
||||||
use seeding::{SeedingAction, SeedingComponent};
|
use seeding::{SeedingAction, SeedingComponent};
|
||||||
use snowballing::{SnowballingAction, SnowballingComponent};
|
use snowballing::{SnowballingAction, SnowballingComponent};
|
||||||
|
|
||||||
@@ -40,41 +45,6 @@ pub enum Tab {
|
|||||||
Snowballing,
|
Snowballing,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub enum GlobalAction {
|
|
||||||
ShowStatusMessage(StatusMessage),
|
|
||||||
ClearStatusMessage,
|
|
||||||
AddIncludedPublication(Publication),
|
|
||||||
SubmitSeedLink,
|
|
||||||
NextTab,
|
|
||||||
Quit,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub enum Action {
|
|
||||||
Snowballing(SnowballingAction),
|
|
||||||
Seeding(SeedingAction),
|
|
||||||
Global(GlobalAction),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<GlobalAction> for Action {
|
|
||||||
fn from(action: GlobalAction) -> Self {
|
|
||||||
Action::Global(action)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<SnowballingAction> for Action {
|
|
||||||
fn from(action: SnowballingAction) -> Self {
|
|
||||||
Action::Snowballing(action)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<SeedingAction> for Action {
|
|
||||||
fn from(action: SeedingAction) -> Self {
|
|
||||||
Action::Seeding(action)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Default)]
|
#[derive(Serialize, Deserialize, Default)]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
// UI state
|
// UI state
|
||||||
@@ -100,7 +70,6 @@ impl AppState {
|
|||||||
pub struct App {
|
pub struct App {
|
||||||
pub state: AppState,
|
pub state: AppState,
|
||||||
pub should_quit: bool,
|
pub should_quit: bool,
|
||||||
pub action_tx: &'static mpsc::UnboundedSender<Action>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Implement moving through steps and iterations (populating pending papers)
|
// TODO: Implement moving through steps and iterations (populating pending papers)
|
||||||
@@ -108,22 +77,28 @@ pub struct App {
|
|||||||
// TODO: Implement possibility of pushing excluded papers back into pending
|
// TODO: Implement possibility of pushing excluded papers back into pending
|
||||||
// TODO: Implement export of included papers as csv for keywording with a spreadsheet
|
// TODO: Implement export of included papers as csv for keywording with a spreadsheet
|
||||||
// TODO: Implement export of included papers into zotero (Use RIS format somehow)
|
// TODO: Implement export of included papers into zotero (Use RIS format somehow)
|
||||||
|
#[component(GlobalAction)]
|
||||||
impl App {
|
impl App {
|
||||||
// TODO: Have status messages always last the same amount of time
|
#[action]
|
||||||
fn handle_global_action(
|
fn quit(&mut self) {
|
||||||
&mut self,
|
self.should_quit = true;
|
||||||
action: GlobalAction,
|
}
|
||||||
action_tx: &'static mpsc::UnboundedSender<Action>,
|
|
||||||
) -> Result<(), SendError<Action>> {
|
#[action]
|
||||||
match action {
|
fn next_tab(&mut self) {
|
||||||
GlobalAction::NextTab => {
|
|
||||||
self.state.current_tab = match self.state.current_tab {
|
self.state.current_tab = match self.state.current_tab {
|
||||||
Tab::Seeding => Tab::Snowballing,
|
Tab::Seeding => Tab::Snowballing,
|
||||||
Tab::Snowballing => Tab::Seeding,
|
Tab::Snowballing => Tab::Seeding,
|
||||||
};
|
};
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
GlobalAction::ShowStatusMessage(msg) => {
|
|
||||||
|
// TODO: Have status messages always last the same amount of time
|
||||||
|
#[action]
|
||||||
|
fn show_stat_msg(
|
||||||
|
&mut self,
|
||||||
|
msg: StatusMessage,
|
||||||
|
action_tx: &'static UnboundedSender<Action>,
|
||||||
|
) {
|
||||||
match &msg {
|
match &msg {
|
||||||
StatusMessage::Error(_) => {
|
StatusMessage::Error(_) => {
|
||||||
error!("Status message: {:?}", msg)
|
error!("Status message: {:?}", msg)
|
||||||
@@ -139,22 +114,36 @@ impl App {
|
|||||||
self.state.status_message = msg;
|
self.state.status_message = msg;
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
sleep(Duration::from_millis(1000)).await;
|
sleep(Duration::from_millis(4000)).await;
|
||||||
|
|
||||||
if let Err(err) =
|
if let Err(err) = action_tx.send(GlobalAction::ClearStatMsg.into())
|
||||||
action_tx.send(GlobalAction::ClearStatusMessage.into())
|
|
||||||
{
|
{
|
||||||
error!("{}", err);
|
error!("{}", err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
#[action]
|
||||||
}
|
fn clear_stat_msg(&mut self) {
|
||||||
GlobalAction::ClearStatusMessage => {
|
|
||||||
self.state.status_message = StatusMessage::Info("".to_string());
|
self.state.status_message = StatusMessage::Info("".to_string());
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
GlobalAction::SubmitSeedLink => {
|
|
||||||
|
#[action]
|
||||||
|
fn add_included_pub(&mut self, publ: Publication) {
|
||||||
|
self.state.history.add_included_publication(publ);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[action]
|
||||||
|
fn add_pending_pub(&mut self, publ: Publication) {
|
||||||
|
self.state.history.add_pending_publication(publ);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[action]
|
||||||
|
fn fetch_and_include_seed(
|
||||||
|
&self,
|
||||||
|
link: String,
|
||||||
|
action_tx: &'static UnboundedSender<Action>,
|
||||||
|
) -> Result<(), SendError<Action>> {
|
||||||
if !self
|
if !self
|
||||||
.state
|
.state
|
||||||
.seeding
|
.seeding
|
||||||
@@ -162,30 +151,28 @@ impl App {
|
|||||||
.starts_with("https://openalex.org/")
|
.starts_with("https://openalex.org/")
|
||||||
{
|
{
|
||||||
status_error!(
|
status_error!(
|
||||||
self.action_tx,
|
action_tx,
|
||||||
"Seed link must start with 'https://openalex.org/'"
|
"Seed link must start with 'https://openalex.org/'"
|
||||||
)?;
|
)?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
status_info!(
|
status_info!(
|
||||||
self.action_tx,
|
action_tx,
|
||||||
"Submitting seed link: {}",
|
"Submitting seed link: {}",
|
||||||
&self.state.seeding.input
|
&self.state.seeding.input
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let api_link = format!(
|
let api_link = format!(
|
||||||
"https://api.openalex.org/{}",
|
"https://api.openalex.org/{}",
|
||||||
self.state
|
link.trim_start_matches("https://openalex.org/")
|
||||||
.seeding
|
|
||||||
.input
|
|
||||||
.trim_start_matches("https://openalex.org/")
|
|
||||||
);
|
);
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let publ = get_publication_by_id(
|
let publ = get_publication_by_id(
|
||||||
&api_link,
|
api_link.into(),
|
||||||
"an.tsouchlos@gmail.com",
|
"an.tsouchlos@gmail.com".to_string().into(),
|
||||||
|
None,
|
||||||
);
|
);
|
||||||
|
|
||||||
match publ.await {
|
match publ.await {
|
||||||
@@ -193,82 +180,168 @@ impl App {
|
|||||||
let _ = status_info!(
|
let _ = status_info!(
|
||||||
action_tx,
|
action_tx,
|
||||||
"Seed paper obtained successfully: {}",
|
"Seed paper obtained successfully: {}",
|
||||||
publ.get_title().unwrap_or(
|
publ.get_title()
|
||||||
"[title unavailable]".to_string()
|
.unwrap_or("[title unavailable]".to_string())
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let _ = action_tx.send(
|
let _ = action_tx
|
||||||
GlobalAction::AddIncludedPublication(publ)
|
.send(GlobalAction::AddIncludedPub(publ).into());
|
||||||
.into(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let _ =
|
let _ = status_error!(action_tx, "{}", e.to_string());
|
||||||
status_error!(action_tx, "{}", e.to_string());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
self.action_tx.send(SeedingAction::ClearInput.into())
|
action_tx.send(SeedingAction::ClearInput.into())
|
||||||
}
|
}
|
||||||
GlobalAction::Quit => {
|
|
||||||
self.should_quit = true;
|
#[action]
|
||||||
|
fn fetch_citing_works(
|
||||||
|
&self,
|
||||||
|
action_tx: &'static UnboundedSender<Action>,
|
||||||
|
) -> Result<(), SendError<Action>> {
|
||||||
|
status_info!(action_tx, "Fetching citing works...")?;
|
||||||
|
|
||||||
|
let included = self.state.history.get_all_included();
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let stream = get_citing_works_stream(
|
||||||
|
included,
|
||||||
|
"an.tsouchlos@gmail.com".to_string(),
|
||||||
|
);
|
||||||
|
|
||||||
|
tokio::pin!(stream);
|
||||||
|
|
||||||
|
while let Some(result) = stream.next().await {
|
||||||
|
match result {
|
||||||
|
Ok(vals) => {
|
||||||
|
for val in vals {
|
||||||
|
let _ = action_tx
|
||||||
|
.send(GlobalAction::AddPendingPub(val).into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
let _ = status_error!(
|
||||||
|
action_tx,
|
||||||
|
"Error loading citing works: {}",
|
||||||
|
err
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let _ = status_info!(action_tx, "Done fetching citing works");
|
||||||
|
});
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
GlobalAction::AddIncludedPublication(publ) => {
|
|
||||||
self.state
|
#[action]
|
||||||
.history
|
fn fetch_references(
|
||||||
.current_iteration
|
&self,
|
||||||
.included_publications
|
action_tx: &'static UnboundedSender<Action>,
|
||||||
.push(publ.clone());
|
) -> Result<(), SendError<Action>> {
|
||||||
|
status_info!(action_tx, "Fetching references...")?;
|
||||||
|
|
||||||
|
let included = self.state.history.get_all_included();
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let stream = get_references_stream(
|
||||||
|
included,
|
||||||
|
"an.tsouchlos@gmail.com".to_string(),
|
||||||
|
);
|
||||||
|
|
||||||
|
tokio::pin!(stream);
|
||||||
|
|
||||||
|
while let Some(result) = stream.next().await {
|
||||||
|
match result {
|
||||||
|
Ok(vals) => {
|
||||||
|
for val in vals {
|
||||||
|
let _ = action_tx
|
||||||
|
.send(GlobalAction::AddPendingPub(val).into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
let _ = status_error!(
|
||||||
|
action_tx,
|
||||||
|
"Error loading reference: {}",
|
||||||
|
err
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let _ = status_info!(action_tx, "Done fetching references");
|
||||||
|
});
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[action]
|
||||||
|
// TODO: Implement
|
||||||
|
fn remove_selected_pending(&mut self) {
|
||||||
|
if self.state.snowballing.active_pane != ActivePane::PendingPublications
|
||||||
|
{
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_action(
|
#[action]
|
||||||
&mut self,
|
// TODO: Implement
|
||||||
action: Action,
|
fn include_selected_pending(&mut self) {
|
||||||
) -> Result<(), SendError<Action>> {
|
if self.state.snowballing.active_pane != ActivePane::PendingPublications
|
||||||
match action {
|
{
|
||||||
Action::Seeding(seeding_action) => self
|
return;
|
||||||
.state
|
|
||||||
.seeding
|
|
||||||
.handle_action(seeding_action, self.action_tx),
|
|
||||||
Action::Snowballing(snowballing_action) => self
|
|
||||||
.state
|
|
||||||
.snowballing
|
|
||||||
.handle_action(snowballing_action, self.action_tx),
|
|
||||||
Action::Global(global_action) => {
|
|
||||||
self.handle_global_action(global_action, self.action_tx)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_key(
|
pub fn handle_key(
|
||||||
&mut self,
|
&mut self,
|
||||||
key: KeyCode,
|
key: KeyCode,
|
||||||
|
action_tx: &'static UnboundedSender<Action>,
|
||||||
) -> Result<(), SendError<Action>> {
|
) -> Result<(), SendError<Action>> {
|
||||||
match (self.state.current_tab, key) {
|
match (self.state.current_tab, key) {
|
||||||
(_, KeyCode::Esc) => {
|
(_, KeyCode::Esc) => action_tx.send(GlobalAction::Quit.into()),
|
||||||
self.action_tx.send(GlobalAction::Quit.into())?;
|
(_, KeyCode::Tab) => action_tx.send(GlobalAction::NextTab.into()),
|
||||||
}
|
|
||||||
(_, KeyCode::Tab) => {
|
|
||||||
self.action_tx.send(GlobalAction::NextTab.into())?;
|
|
||||||
}
|
|
||||||
(Tab::Seeding, KeyCode::Char(c)) => {
|
(Tab::Seeding, KeyCode::Char(c)) => {
|
||||||
self.action_tx.send(SeedingAction::EnterChar(c).into())?;
|
action_tx.send(SeedingAction::EnterChar(c).into())
|
||||||
}
|
}
|
||||||
(Tab::Seeding, KeyCode::Backspace) => {
|
(Tab::Seeding, KeyCode::Backspace) => {
|
||||||
self.action_tx.send(SeedingAction::EnterBackspace.into())?;
|
action_tx.send(SeedingAction::EnterBackspace.into())
|
||||||
}
|
}
|
||||||
(Tab::Seeding, KeyCode::Enter) => {
|
(Tab::Seeding, KeyCode::Enter) => action_tx.send(
|
||||||
self.action_tx.send(GlobalAction::SubmitSeedLink.into())?;
|
GlobalAction::FetchAndIncludeSeed(
|
||||||
|
self.state.seeding.input.clone(),
|
||||||
|
)
|
||||||
|
.into(),
|
||||||
|
),
|
||||||
|
(Tab::Snowballing, KeyCode::Enter) => {
|
||||||
|
action_tx.send(SnowballingAction::Search.into())
|
||||||
}
|
}
|
||||||
_ => {}
|
(Tab::Snowballing, KeyCode::Char('h')) => {
|
||||||
|
action_tx.send(SnowballingAction::SelectLeftPane.into())
|
||||||
|
}
|
||||||
|
(Tab::Snowballing, KeyCode::Char('l')) => {
|
||||||
|
action_tx.send(SnowballingAction::SelectRightPane.into())
|
||||||
|
}
|
||||||
|
(Tab::Snowballing, KeyCode::Char('j')) => {
|
||||||
|
action_tx.send(SnowballingAction::NextItem.into())
|
||||||
|
}
|
||||||
|
(Tab::Snowballing, KeyCode::Char('k')) => {
|
||||||
|
action_tx.send(SnowballingAction::PrevItem.into())
|
||||||
|
}
|
||||||
|
(Tab::Snowballing, KeyCode::Char(' ')) => {
|
||||||
|
action_tx.send(GlobalAction::FetchReferences.into())
|
||||||
|
}
|
||||||
|
(Tab::Snowballing, KeyCode::Char('c')) => {
|
||||||
|
action_tx.send(GlobalAction::FetchCitingWorks.into())
|
||||||
|
}
|
||||||
|
(Tab::Snowballing, KeyCode::Char('X')) => {
|
||||||
|
action_tx.send(GlobalAction::RemoveSelectedPending.into())
|
||||||
|
}
|
||||||
|
(Tab::Snowballing, KeyCode::Char('<')) => {
|
||||||
|
action_tx.send(GlobalAction::IncludeSelectedPending.into())
|
||||||
|
}
|
||||||
|
_ => Ok(()),
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,22 @@
|
|||||||
|
use crate::app::Action;
|
||||||
use tokio::sync::mpsc::{self, error::SendError};
|
use tokio::sync::mpsc::{self, error::SendError};
|
||||||
|
|
||||||
use crate::app::Action;
|
// TODO: Put this somewhere closer to the procedural macro definitions
|
||||||
|
pub trait Component<T> {
|
||||||
|
fn handle_action(
|
||||||
|
&mut self,
|
||||||
|
action: T,
|
||||||
|
tx: &'static mpsc::UnboundedSender<Action>,
|
||||||
|
) -> Result<(), SendError<Action>>;
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(unused_macros)]
|
#[allow(unused_macros)]
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! status_info {
|
macro_rules! status_info {
|
||||||
($action_tx:expr, $text:expr, $($args:expr)*) => {
|
($action_tx:expr, $text:expr $(, $args:expr)*) => {
|
||||||
$action_tx.send(
|
$action_tx.send(
|
||||||
GlobalAction::ShowStatusMessage(StatusMessage::Info(format!($text, $($args)*)))
|
crate::app::GlobalAction::ShowStatMsg(
|
||||||
|
crate::app::StatusMessage::Info(format!($text, $($args)*)))
|
||||||
.into(),
|
.into(),
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
@@ -16,9 +25,10 @@ macro_rules! status_info {
|
|||||||
#[allow(unused_macros)]
|
#[allow(unused_macros)]
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! status_warn {
|
macro_rules! status_warn {
|
||||||
($action_tx:expr, $text:expr, $($args:expr)*) => {
|
($action_tx:expr, $text:expr $(, $args:expr)*) => {
|
||||||
$action_tx.send(
|
$action_tx.send(
|
||||||
GlobalAction::ShowStatusMessage(StatusMessage::Warning(format!($text, $($args)*)))
|
crate::app::GlobalAction::ShowStatMsg(
|
||||||
|
crate::app::StatusMessage::Warn(format!($text, $($args)*)))
|
||||||
.into(),
|
.into(),
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
@@ -29,16 +39,9 @@ macro_rules! status_warn {
|
|||||||
macro_rules! status_error {
|
macro_rules! status_error {
|
||||||
($action_tx:expr, $text:expr $(, $args:expr)*) => {
|
($action_tx:expr, $text:expr $(, $args:expr)*) => {
|
||||||
$action_tx.send(
|
$action_tx.send(
|
||||||
GlobalAction::ShowStatusMessage(StatusMessage::Error(format!($text, $($args)*)))
|
crate::app::GlobalAction::ShowStatMsg(
|
||||||
|
crate::app::StatusMessage::Error(format!($text $(, $args)*)))
|
||||||
.into(),
|
.into(),
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Component<T> {
|
|
||||||
fn handle_action(
|
|
||||||
&mut self,
|
|
||||||
action: T,
|
|
||||||
tx: &mpsc::UnboundedSender<Action>,
|
|
||||||
) -> Result<(), SendError<Action>>;
|
|
||||||
}
|
|
||||||
|
|||||||
97
src/app/run.rs
Normal file
97
src/app/run.rs
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
use std::{error::Error, time::Duration};
|
||||||
|
|
||||||
|
use crossterm::event::{self, Event};
|
||||||
|
use ratatui::{Terminal, prelude::Backend};
|
||||||
|
use static_cell::StaticCell;
|
||||||
|
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
app::{
|
||||||
|
App, AppState, GlobalAction, common::Component, seeding::SeedingAction,
|
||||||
|
snowballing::SnowballingAction,
|
||||||
|
},
|
||||||
|
ui,
|
||||||
|
};
|
||||||
|
|
||||||
|
static ACTION_QUEUE_TX: StaticCell<UnboundedSender<Action>> = StaticCell::new();
|
||||||
|
static ACTION_QUEUE_RX: StaticCell<UnboundedReceiver<Action>> =
|
||||||
|
StaticCell::new();
|
||||||
|
|
||||||
|
// TODO: Move this somewhere sensible
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum Action {
|
||||||
|
Snowballing(SnowballingAction),
|
||||||
|
Seeding(SeedingAction),
|
||||||
|
Global(GlobalAction),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<GlobalAction> for Action {
|
||||||
|
fn from(action: GlobalAction) -> Self {
|
||||||
|
Action::Global(action)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<SnowballingAction> for Action {
|
||||||
|
fn from(action: SnowballingAction) -> Self {
|
||||||
|
Action::Snowballing(action)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<SeedingAction> for Action {
|
||||||
|
fn from(action: SeedingAction) -> Self {
|
||||||
|
Action::Seeding(action)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Is there a way to completely decouple this from crossterm?
|
||||||
|
pub async fn run_app<B: Backend>(
|
||||||
|
terminal: &mut Terminal<B>,
|
||||||
|
app_state: AppState,
|
||||||
|
) -> Result<AppState, Box<dyn Error>>
|
||||||
|
where
|
||||||
|
<B as Backend>::Error: 'static,
|
||||||
|
{
|
||||||
|
let (action_tx, action_rx): (
|
||||||
|
UnboundedSender<Action>,
|
||||||
|
UnboundedReceiver<Action>,
|
||||||
|
) = mpsc::unbounded_channel();
|
||||||
|
|
||||||
|
let action_tx_ref = ACTION_QUEUE_TX.init(action_tx);
|
||||||
|
let action_rx_ref = ACTION_QUEUE_RX.init(action_rx);
|
||||||
|
|
||||||
|
let mut app = App {
|
||||||
|
state: app_state,
|
||||||
|
should_quit: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
loop {
|
||||||
|
app.state.refresh_component_states(); // TODO: Is it a problem to call this every frame?
|
||||||
|
terminal.draw(|frame| ui::draw(frame, &mut app.state))?;
|
||||||
|
|
||||||
|
if event::poll(Duration::from_millis(100))? {
|
||||||
|
if let Event::Key(key) = event::read()? {
|
||||||
|
app.handle_key(key.code, action_tx_ref)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while let Ok(action) = action_rx_ref.try_recv() {
|
||||||
|
match action {
|
||||||
|
Action::Seeding(seeding_action) => app
|
||||||
|
.state
|
||||||
|
.seeding
|
||||||
|
.handle_action(seeding_action, action_tx_ref),
|
||||||
|
Action::Snowballing(snowballing_action) => app
|
||||||
|
.state
|
||||||
|
.snowballing
|
||||||
|
.handle_action(snowballing_action, action_tx_ref),
|
||||||
|
Action::Global(global_action) => {
|
||||||
|
app.handle_action(global_action, action_tx_ref)
|
||||||
|
}
|
||||||
|
}?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if app.should_quit {
|
||||||
|
return Ok(app.state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +1,11 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tokio::sync::mpsc::{self, error::SendError};
|
use tokio::sync::mpsc::{UnboundedSender, error::SendError};
|
||||||
|
|
||||||
use crate::{app::Action, app::common::Component, literature::Publication};
|
use crate::{
|
||||||
|
app::{GlobalAction, run::Action},
|
||||||
#[derive(Clone, Debug)]
|
literature::Publication,
|
||||||
pub enum SeedingAction {
|
};
|
||||||
EnterChar(char),
|
use brittle_macros::component;
|
||||||
EnterBackspace,
|
|
||||||
ClearInput,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Default)]
|
#[derive(Serialize, Deserialize, Default)]
|
||||||
pub struct SeedingComponent {
|
pub struct SeedingComponent {
|
||||||
@@ -17,28 +14,31 @@ pub struct SeedingComponent {
|
|||||||
pub included_publications: Vec<Publication>,
|
pub included_publications: Vec<Publication>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Component<SeedingAction> for SeedingComponent {
|
#[component(SeedingAction)]
|
||||||
fn handle_action(
|
impl SeedingComponent {
|
||||||
|
#[action]
|
||||||
|
pub fn submit(
|
||||||
&mut self,
|
&mut self,
|
||||||
action: SeedingAction,
|
action_tx: &UnboundedSender<Action>,
|
||||||
_: &mpsc::UnboundedSender<Action>,
|
|
||||||
) -> Result<(), SendError<Action>> {
|
) -> Result<(), SendError<Action>> {
|
||||||
match action {
|
action_tx
|
||||||
SeedingAction::ClearInput => {
|
.send(GlobalAction::FetchAndIncludeSeed(self.input.clone()).into())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[action]
|
||||||
|
pub fn clear_input(&mut self) {
|
||||||
self.input.clear();
|
self.input.clear();
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
SeedingAction::EnterChar(c) => {
|
|
||||||
self.input.push(c);
|
#[action]
|
||||||
Ok(())
|
pub fn enter_char(&mut self, c: char) {
|
||||||
|
self.input.push(c)
|
||||||
}
|
}
|
||||||
SeedingAction::EnterBackspace => {
|
|
||||||
|
#[action]
|
||||||
|
pub fn enter_backspace(&mut self) {
|
||||||
if self.input.len() > 0 {
|
if self.input.len() > 0 {
|
||||||
self.input.truncate(self.input.len() - 1);
|
self.input.truncate(self.input.len() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
|
use brittle_macros::component;
|
||||||
use ratatui::widgets::ListState;
|
use ratatui::widgets::ListState;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tokio::sync::mpsc;
|
|
||||||
use tokio::sync::mpsc::error::SendError;
|
|
||||||
|
|
||||||
use crate::app::{Action, common::Component};
|
|
||||||
use crate::literature::Publication;
|
use crate::literature::Publication;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Default, PartialEq)]
|
#[derive(Serialize, Deserialize, Default, PartialEq)]
|
||||||
@@ -13,17 +11,6 @@ pub enum ActivePane {
|
|||||||
PendingPublications,
|
PendingPublications,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub enum SnowballingAction {
|
|
||||||
SelectLeftPane,
|
|
||||||
SelectRightPane,
|
|
||||||
SearchForSelected,
|
|
||||||
NextItem,
|
|
||||||
PrevItem,
|
|
||||||
ShowIncludedPublication(Publication),
|
|
||||||
ShowPendingPublication(Publication),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct SerializableListState {
|
struct SerializableListState {
|
||||||
pub offset: usize,
|
pub offset: usize,
|
||||||
@@ -75,8 +62,9 @@ pub struct SnowballingComponent {
|
|||||||
pub pending_publications: Vec<Publication>,
|
pub pending_publications: Vec<Publication>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[component(SnowballingAction)]
|
||||||
impl SnowballingComponent {
|
impl SnowballingComponent {
|
||||||
fn next_list_item(
|
fn select_next_item_impl(
|
||||||
list_state: &mut ListState,
|
list_state: &mut ListState,
|
||||||
publications: &Vec<Publication>,
|
publications: &Vec<Publication>,
|
||||||
) {
|
) {
|
||||||
@@ -93,7 +81,7 @@ impl SnowballingComponent {
|
|||||||
list_state.select(Some(i));
|
list_state.select(Some(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prev_list_item(
|
fn select_prev_item_impl(
|
||||||
list_state: &mut ListState,
|
list_state: &mut ListState,
|
||||||
publications: &Vec<Publication>,
|
publications: &Vec<Publication>,
|
||||||
) {
|
) {
|
||||||
@@ -109,81 +97,74 @@ impl SnowballingComponent {
|
|||||||
};
|
};
|
||||||
list_state.select(Some(i));
|
list_state.select(Some(i));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl Component<SnowballingAction> for SnowballingComponent {
|
#[action]
|
||||||
fn handle_action(
|
fn select_left_pane(&mut self) {
|
||||||
&mut self,
|
|
||||||
action: SnowballingAction,
|
|
||||||
_: &mpsc::UnboundedSender<Action>,
|
|
||||||
) -> Result<(), SendError<Action>> {
|
|
||||||
match action {
|
|
||||||
SnowballingAction::SelectLeftPane => {
|
|
||||||
self.active_pane = ActivePane::IncludedPublications;
|
self.active_pane = ActivePane::IncludedPublications;
|
||||||
|
|
||||||
if let None = self.included_list_state.selected() {
|
if let None = self.included_list_state.selected() {
|
||||||
self.included_list_state.select(Some(0));
|
self.included_list_state.select(Some(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
SnowballingAction::SelectRightPane => {
|
|
||||||
self.active_pane = ActivePane::IncludedPublications;
|
|
||||||
|
|
||||||
if let None = self.included_list_state.selected() {
|
|
||||||
self.included_list_state.select(Some(0));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
#[action]
|
||||||
|
fn select_right_pane(&mut self) {
|
||||||
|
self.active_pane = ActivePane::PendingPublications;
|
||||||
|
|
||||||
|
if let None = self.pending_list_state.selected() {
|
||||||
|
self.pending_list_state.select(Some(0));
|
||||||
}
|
}
|
||||||
SnowballingAction::SearchForSelected => match self.active_pane {
|
}
|
||||||
|
|
||||||
|
#[action]
|
||||||
|
fn search(&self) {
|
||||||
|
match self.active_pane {
|
||||||
ActivePane::IncludedPublications => {
|
ActivePane::IncludedPublications => {
|
||||||
if let Some(idx) = self.included_list_state.selected() {
|
if let Some(idx) = self.included_list_state.selected() {
|
||||||
open::that(&self.included_publications[idx].id)
|
open::that(&self.included_publications[idx].id).unwrap();
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
ActivePane::PendingPublications => {
|
ActivePane::PendingPublications => {
|
||||||
if let Some(idx) = self.pending_list_state.selected() {
|
if let Some(idx) = self.pending_list_state.selected() {
|
||||||
open::that(&self.pending_publications[idx].id).unwrap();
|
open::that(&self.pending_publications[idx].id).unwrap();
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
SnowballingAction::NextItem => match self.active_pane {
|
}
|
||||||
|
|
||||||
|
#[action]
|
||||||
|
fn next_item(&mut self) {
|
||||||
|
match self.active_pane {
|
||||||
ActivePane::IncludedPublications => {
|
ActivePane::IncludedPublications => {
|
||||||
Self::next_list_item(
|
Self::select_next_item_impl(
|
||||||
&mut self.included_list_state,
|
&mut self.included_list_state,
|
||||||
&self.included_publications,
|
&self.included_publications,
|
||||||
);
|
);
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
ActivePane::PendingPublications => {
|
ActivePane::PendingPublications => {
|
||||||
Self::next_list_item(
|
Self::select_next_item_impl(
|
||||||
&mut self.pending_list_state,
|
&mut self.pending_list_state,
|
||||||
&self.pending_publications,
|
&self.pending_publications,
|
||||||
);
|
);
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
SnowballingAction::PrevItem => match self.active_pane {
|
}
|
||||||
|
|
||||||
|
#[action]
|
||||||
|
fn prev_item(&mut self) {
|
||||||
|
match self.active_pane {
|
||||||
ActivePane::IncludedPublications => {
|
ActivePane::IncludedPublications => {
|
||||||
Self::prev_list_item(
|
Self::select_prev_item_impl(
|
||||||
&mut self.included_list_state,
|
&mut self.included_list_state,
|
||||||
&self.included_publications,
|
&self.included_publications,
|
||||||
);
|
);
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
ActivePane::PendingPublications => {
|
ActivePane::PendingPublications => {
|
||||||
Self::prev_list_item(
|
Self::select_prev_item_impl(
|
||||||
&mut self.pending_list_state,
|
&mut self.pending_list_state,
|
||||||
&self.pending_publications,
|
&self.pending_publications,
|
||||||
);
|
);
|
||||||
Ok(())
|
}
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => Ok(()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
use std::{error::Error, io, time::Duration};
|
use std::{error::Error, io};
|
||||||
|
|
||||||
use log::error;
|
use log::error;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
Terminal,
|
Terminal,
|
||||||
backend::{Backend, CrosstermBackend},
|
backend::CrosstermBackend,
|
||||||
crossterm::{
|
crossterm::{
|
||||||
event::{self, DisableMouseCapture, EnableMouseCapture, Event},
|
event::{DisableMouseCapture, EnableMouseCapture},
|
||||||
execute,
|
execute,
|
||||||
terminal::{
|
terminal::{
|
||||||
EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode,
|
EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode,
|
||||||
@@ -13,12 +13,8 @@ use ratatui::{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::app::{AppState, run::run_app};
|
||||||
app::{Action, App, AppState},
|
|
||||||
ui,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub async fn run(app_state: AppState) -> Result<AppState, Box<dyn Error>> {
|
pub async fn run(app_state: AppState) -> Result<AppState, Box<dyn Error>> {
|
||||||
// setup terminal
|
// setup terminal
|
||||||
@@ -47,50 +43,3 @@ pub async fn run(app_state: AppState) -> Result<AppState, Box<dyn Error>> {
|
|||||||
|
|
||||||
Ok(app_result?)
|
Ok(app_result?)
|
||||||
}
|
}
|
||||||
|
|
||||||
use static_cell::StaticCell;
|
|
||||||
|
|
||||||
static ACTION_QUEUE_TX: StaticCell<UnboundedSender<Action>> = StaticCell::new();
|
|
||||||
static ACTION_QUEUE_RX: StaticCell<UnboundedReceiver<Action>> =
|
|
||||||
StaticCell::new();
|
|
||||||
|
|
||||||
async fn run_app<B: Backend>(
|
|
||||||
terminal: &mut Terminal<B>,
|
|
||||||
app_state: AppState,
|
|
||||||
) -> Result<AppState, Box<dyn Error>>
|
|
||||||
where
|
|
||||||
<B as Backend>::Error: 'static,
|
|
||||||
{
|
|
||||||
let (action_tx, action_rx): (
|
|
||||||
UnboundedSender<Action>,
|
|
||||||
UnboundedReceiver<Action>,
|
|
||||||
) = mpsc::unbounded_channel();
|
|
||||||
|
|
||||||
let action_tx_ref = ACTION_QUEUE_TX.init(action_tx);
|
|
||||||
let action_rx_ref = ACTION_QUEUE_RX.init(action_rx);
|
|
||||||
|
|
||||||
let mut app = App {
|
|
||||||
state: app_state,
|
|
||||||
action_tx: action_tx_ref,
|
|
||||||
should_quit: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
loop {
|
|
||||||
app.state.refresh_component_states(); // TODO: Is it a problem to call this every frame?
|
|
||||||
terminal.draw(|frame| ui::draw(frame, &mut app.state))?;
|
|
||||||
|
|
||||||
if event::poll(Duration::from_millis(100))? {
|
|
||||||
if let Event::Key(key) = event::read()? {
|
|
||||||
app.handle_key(key.code)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
while let Ok(action) = action_rx_ref.try_recv() {
|
|
||||||
app.handle_action(action)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
if app.should_quit {
|
|
||||||
return Ok(app.state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
use ammonia::Builder;
|
use ammonia::Builder;
|
||||||
|
use futures::{StreamExt, future::BoxFuture, stream};
|
||||||
use html_escape::decode_html_entities;
|
use html_escape::decode_html_entities;
|
||||||
use reqwest::Error;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::HashMap;
|
use std::{collections::HashMap, fmt::Display, sync::Arc};
|
||||||
use unicode_general_category::{GeneralCategory, get_general_category};
|
use unicode_general_category::{GeneralCategory, get_general_category};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
@@ -50,16 +50,17 @@ pub struct SnowballingIteration {
|
|||||||
pub struct SnowballingHistory {
|
pub struct SnowballingHistory {
|
||||||
pub seed: Vec<Publication>,
|
pub seed: Vec<Publication>,
|
||||||
pub current_iteration: SnowballingIteration,
|
pub current_iteration: SnowballingIteration,
|
||||||
pub previoius_iterations: Vec<SnowballingIteration>,
|
pub previous_iterations: Vec<SnowballingIteration>,
|
||||||
pub pending_publications: Vec<Publication>,
|
pub pending_publications: Vec<Publication>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SnowballingHistory {
|
impl SnowballingHistory {
|
||||||
|
// TODO: Make this return references if possible
|
||||||
pub fn get_all_included(&self) -> Vec<Publication> {
|
pub fn get_all_included(&self) -> Vec<Publication> {
|
||||||
vec![self.current_iteration.included_publications.clone()]
|
vec![self.current_iteration.included_publications.clone()]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.chain(
|
.chain(
|
||||||
self.previoius_iterations
|
self.previous_iterations
|
||||||
.iter()
|
.iter()
|
||||||
.map(|iter| iter.included_publications.clone()),
|
.map(|iter| iter.included_publications.clone()),
|
||||||
)
|
)
|
||||||
@@ -67,14 +68,73 @@ impl SnowballingHistory {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Make this return references if possible
|
||||||
pub fn get_all_pending(&self) -> Vec<Publication> {
|
pub fn get_all_pending(&self) -> Vec<Publication> {
|
||||||
self.pending_publications.clone()
|
self.pending_publications.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn publication_exists(&self, publ: &Publication) -> bool {
|
||||||
|
self.pending_publications
|
||||||
|
.iter()
|
||||||
|
.chain(self.current_iteration.included_publications.iter())
|
||||||
|
.chain(self.current_iteration.excluded_publications.iter())
|
||||||
|
.chain(
|
||||||
|
self.previous_iterations
|
||||||
|
.iter()
|
||||||
|
.flat_map(|p| p.included_publications.iter()),
|
||||||
|
)
|
||||||
|
.chain(
|
||||||
|
self.previous_iterations
|
||||||
|
.iter()
|
||||||
|
.flat_map(|p| p.excluded_publications.iter()),
|
||||||
|
)
|
||||||
|
.any(|p| p.id == publ.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Implement deduplication
|
||||||
|
pub fn add_pending_publication(&mut self, publ: Publication) {
|
||||||
|
if !self.publication_exists(&publ) {
|
||||||
|
self.pending_publications.push(publ);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Implement deduplication
|
||||||
|
pub fn add_included_publication(&mut self, publ: Publication) {
|
||||||
|
if !self.publication_exists(&publ) {
|
||||||
|
self.current_iteration.included_publications.push(publ);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sanitize_text(raw_text: &str) -> String {
|
||||||
|
let cleaner = Builder::empty().clean(&raw_text).to_string();
|
||||||
|
let decoded = decode_html_entities(&cleaner);
|
||||||
|
|
||||||
|
let cleaned: String = decoded
|
||||||
|
.chars()
|
||||||
|
.filter(|&c| {
|
||||||
|
let cat = get_general_category(c);
|
||||||
|
!matches!(
|
||||||
|
cat,
|
||||||
|
GeneralCategory::Control
|
||||||
|
| GeneralCategory::Format
|
||||||
|
| GeneralCategory::Surrogate
|
||||||
|
| GeneralCategory::PrivateUse
|
||||||
|
| GeneralCategory::Unassigned
|
||||||
|
) || c.is_whitespace()
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
cleaned
|
||||||
|
.split_whitespace()
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(" ")
|
||||||
|
.replace("\\n", " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Publication {
|
impl Publication {
|
||||||
pub fn get_title(&self) -> Option<String> {
|
pub fn get_title(&self) -> Option<String> {
|
||||||
self.display_name.clone()
|
self.display_name.clone().map(|s| sanitize_text(&s))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_year(&self) -> Option<u32> {
|
pub fn get_year(&self) -> Option<u32> {
|
||||||
@@ -113,70 +173,215 @@ impl Publication {
|
|||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(" ");
|
.join(" ");
|
||||||
|
|
||||||
let cleaner = Builder::empty().clean(&raw_text).to_string();
|
sanitize_text(&raw_text)
|
||||||
let decoded = decode_html_entities(&cleaner);
|
|
||||||
|
|
||||||
let cleaned: String = decoded
|
|
||||||
.chars()
|
|
||||||
.filter(|&c| {
|
|
||||||
let cat = get_general_category(c);
|
|
||||||
!matches!(
|
|
||||||
cat,
|
|
||||||
GeneralCategory::Control
|
|
||||||
| GeneralCategory::Format
|
|
||||||
| GeneralCategory::Surrogate
|
|
||||||
| GeneralCategory::PrivateUse
|
|
||||||
| GeneralCategory::Unassigned
|
|
||||||
) || c.is_whitespace()
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
cleaned.split_whitespace().collect::<Vec<_>>().join(" ")
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[derive(Serialize, Deserialize, Debug)]
|
#[derive(Deserialize)]
|
||||||
// pub struct OpenAlexResponse {
|
struct OpenAlexResponse {
|
||||||
// pub results: Vec<Publication>,
|
results: Vec<Publication>,
|
||||||
// }
|
meta: Meta,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct Meta {
|
||||||
|
next_cursor: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
ApiError(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for Error {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Error::ApiError(s) => write!(
|
||||||
|
f,
|
||||||
|
"An error occurred while attempting to access the OpenAlex \
|
||||||
|
API: url={}",
|
||||||
|
s
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn get_publication_by_id(
|
pub async fn get_publication_by_id(
|
||||||
api_link: &str,
|
api_link: Arc<String>,
|
||||||
email: &str,
|
email: Arc<String>,
|
||||||
|
client: Option<reqwest::Client>,
|
||||||
) -> Result<Publication, Error> {
|
) -> Result<Publication, Error> {
|
||||||
let url = format!("{}&mailto={}", api_link, email);
|
let url = format!("{}&mailto={}", api_link, email);
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
let client = client.or(Some(reqwest::Client::new())).unwrap();
|
||||||
let response = client
|
|
||||||
.get(url)
|
client
|
||||||
|
.get(url.clone())
|
||||||
.header("User-Agent", "Rust-OpenAlex-Client/1.0")
|
.header("User-Agent", "Rust-OpenAlex-Client/1.0")
|
||||||
.send()
|
.send()
|
||||||
.await?
|
.await
|
||||||
|
.map_err(|_| Error::ApiError(url.clone()))?
|
||||||
.json::<Publication>()
|
.json::<Publication>()
|
||||||
.await?;
|
.await
|
||||||
|
.map_err(|_| Error::ApiError(url))
|
||||||
Ok(response)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// // TODO: Get all papers, not just the first page
|
// TODO: Rename this
|
||||||
// pub async fn get_citing_papers(
|
fn get_cited_works(
|
||||||
// target_id: &str,
|
id: String,
|
||||||
// email: &str,
|
email: Arc<String>,
|
||||||
// ) -> Result<Vec<Publication>, Error> {
|
client: reqwest::Client,
|
||||||
// let url = format!(
|
) -> impl futures::Stream<Item = Result<Vec<Publication>, Error>> {
|
||||||
// "https://api.openalex.org/works?filter=cites:{}&mailto={}",
|
let initial_state = Some("*".to_string());
|
||||||
// target_id, email
|
|
||||||
// );
|
stream::unfold(initial_state, move |cursor| {
|
||||||
//
|
let email = email.clone();
|
||||||
// let client = reqwest::Client::new();
|
let client = client.clone();
|
||||||
// let response = client
|
let id = id.clone();
|
||||||
// .get(url)
|
|
||||||
// .header("User-Agent", "Rust-OpenAlex-Client/1.0")
|
async move {
|
||||||
// .send()
|
let current_cursor = cursor?;
|
||||||
// .await?
|
|
||||||
// .json::<OpenAlexResponse>()
|
let url = format!(
|
||||||
// .await?;
|
"https://api.openalex.org/works?filter=cited_by:{}&mailto={}&cursor={}",
|
||||||
//
|
id, email, current_cursor
|
||||||
// Ok(response.results)
|
);
|
||||||
// }
|
|
||||||
|
let response = client
|
||||||
|
.get(&url)
|
||||||
|
.header("User-Agent", "Rust-OpenAlex-Client/1.0")
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|_| Error::ApiError(url.clone()))
|
||||||
|
.ok()?
|
||||||
|
.json::<OpenAlexResponse>()
|
||||||
|
.await
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
|
let next_cursor = if response.results.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
response.meta.next_cursor
|
||||||
|
};
|
||||||
|
|
||||||
|
Some((Ok(response.results), next_cursor))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_references_stream(
|
||||||
|
publications: Vec<Publication>,
|
||||||
|
email: String,
|
||||||
|
) -> impl futures::Stream<Item = Result<Vec<Publication>, Error>> {
|
||||||
|
let email = Arc::new(email);
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
|
let mut publication_ids = Vec::<String>::new();
|
||||||
|
let mut referenced_work_urls = Vec::<String>::new();
|
||||||
|
|
||||||
|
for p in &publications {
|
||||||
|
publication_ids
|
||||||
|
.push(p.id.trim_start_matches("https://openalex.org/").to_string());
|
||||||
|
|
||||||
|
referenced_work_urls.append(
|
||||||
|
&mut p
|
||||||
|
.referenced_works
|
||||||
|
.iter()
|
||||||
|
.map(|r| {
|
||||||
|
format!(
|
||||||
|
"https://api.openalex.org/{}",
|
||||||
|
r.trim_start_matches("https://openalex.org/")
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect::<Vec<String>>(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get references using the referenced_works field
|
||||||
|
|
||||||
|
let stream1 = stream::iter(referenced_work_urls)
|
||||||
|
.map({
|
||||||
|
let (email, client) = (email.clone(), client.clone());
|
||||||
|
move |url| {
|
||||||
|
let (email, client) = (email.clone(), client.clone());
|
||||||
|
let fut: BoxFuture<'static, Result<Vec<Publication>, Error>> =
|
||||||
|
Box::pin(async move {
|
||||||
|
get_publication_by_id(url.into(), email, Some(client))
|
||||||
|
.await
|
||||||
|
.map(|val| vec![val])
|
||||||
|
});
|
||||||
|
fut
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.buffer_unordered(10);
|
||||||
|
|
||||||
|
// Search for references using API calls
|
||||||
|
|
||||||
|
let stream2 = stream::iter(publication_ids)
|
||||||
|
.map(move |id| get_cited_works(id, email.clone(), client.clone()))
|
||||||
|
.flatten();
|
||||||
|
|
||||||
|
// Combine the two streams
|
||||||
|
stream::select(stream1, stream2)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Rename this
|
||||||
|
fn get_citing_works(
|
||||||
|
id: String,
|
||||||
|
email: Arc<String>,
|
||||||
|
client: reqwest::Client,
|
||||||
|
) -> impl futures::Stream<Item = Result<Vec<Publication>, Error>> {
|
||||||
|
let initial_state = Some("*".to_string());
|
||||||
|
|
||||||
|
stream::unfold(initial_state, move |cursor| {
|
||||||
|
let email = email.clone();
|
||||||
|
let client = client.clone();
|
||||||
|
let id = id.clone();
|
||||||
|
|
||||||
|
async move {
|
||||||
|
let current_cursor = cursor?;
|
||||||
|
|
||||||
|
let url = format!(
|
||||||
|
"https://api.openalex.org/works?filter=cites:{}&mailto={}&cursor={}",
|
||||||
|
id, email, current_cursor
|
||||||
|
);
|
||||||
|
|
||||||
|
let response = client
|
||||||
|
.get(&url)
|
||||||
|
.header("User-Agent", "Rust-OpenAlex-Client/1.0")
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|_| Error::ApiError(url.clone()))
|
||||||
|
.ok()?
|
||||||
|
.json::<OpenAlexResponse>()
|
||||||
|
.await
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
|
let next_cursor = if response.results.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
response.meta.next_cursor
|
||||||
|
};
|
||||||
|
|
||||||
|
Some((Ok(response.results), next_cursor))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_citing_works_stream(
|
||||||
|
publications: Vec<Publication>,
|
||||||
|
email: String,
|
||||||
|
) -> impl futures::Stream<Item = Result<Vec<Publication>, Error>> {
|
||||||
|
let email = Arc::new(email);
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
|
let ids: Vec<String> = publications
|
||||||
|
.iter()
|
||||||
|
.map(|p| p.id.trim_start_matches("https://openalex.org/").to_string())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
stream::iter(ids)
|
||||||
|
.map(move |id| get_citing_works(id, email.clone(), client.clone()))
|
||||||
|
.flatten()
|
||||||
|
}
|
||||||
|
|||||||
77
src/main.rs
77
src/main.rs
@@ -1,66 +1,45 @@
|
|||||||
mod app;
|
mod app;
|
||||||
|
mod crossterm;
|
||||||
mod literature;
|
mod literature;
|
||||||
mod ui;
|
mod ui;
|
||||||
|
|
||||||
|
use clap::Parser;
|
||||||
use log::error;
|
use log::error;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
|
use std::{env, error::Error, fs::OpenOptions};
|
||||||
|
|
||||||
use crate::app::AppState;
|
use crate::app::AppState;
|
||||||
|
|
||||||
// use crate::snowballing::get_citing_papers;
|
fn deserialize_savefile(filename: &String) -> Result<AppState, Box<dyn Error>> {
|
||||||
// #[tokio::main]
|
if !std::fs::exists(filename)? {
|
||||||
// async fn main() -> Result<(), reqwest::Error> {
|
return Ok(AppState::default());
|
||||||
// let publications = get_citing_papers("w2963127785", "an.tsouchlos@gmail.com").await?;
|
}
|
||||||
//
|
|
||||||
// let app = App {
|
|
||||||
// pending_publications: publications,
|
|
||||||
// ..Default::default()
|
|
||||||
// };
|
|
||||||
//
|
|
||||||
// if let Ok(serialized) = serde_json::to_string_pretty(&app) {
|
|
||||||
// std::fs::write("temp.json", serialized)
|
|
||||||
// .expect("We can't really deal with io errors ourselves");
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// Ok(())
|
|
||||||
// }
|
|
||||||
|
|
||||||
fn deserialize_savefile(
|
let app_state: AppState =
|
||||||
filename: &String,
|
serde_json::from_str::<AppState>(&std::fs::read_to_string(filename)?)?;
|
||||||
) -> Result<AppState, serde_json::Error> {
|
|
||||||
match std::fs::read_to_string(filename) {
|
Ok(app_state)
|
||||||
Ok(content) => {
|
|
||||||
let app: AppState = serde_json::from_str(&content)?;
|
|
||||||
Ok(app)
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
let app = AppState::default();
|
|
||||||
if let Ok(serialized) = serde_json::to_string_pretty(&app) {
|
|
||||||
let _ = std::fs::write(filename, serialized);
|
|
||||||
}
|
|
||||||
Ok(app)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_savefile(
|
fn serialize_savefile(
|
||||||
app: &AppState,
|
app: &AppState,
|
||||||
filename: &String,
|
filename: &String,
|
||||||
) -> Result<(), serde_json::Error> {
|
) -> Result<(), Box<dyn Error>> {
|
||||||
if let Ok(serialized) = serde_json::to_string_pretty(&app) {
|
let serialized = serde_json::to_string_pretty(&app)?;
|
||||||
std::fs::write(filename, serialized)
|
Ok(std::fs::write(filename, serialized)?)
|
||||||
.expect("We can't really deal with io errors ourselves");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn run(args: &Args) -> Result<(), Box<dyn Error>> {
|
||||||
|
let starting_app_state = deserialize_savefile(&args.savefile)?;
|
||||||
|
let final_app_state = crate::crossterm::run(starting_app_state).await?;
|
||||||
|
|
||||||
|
serialize_savefile(&final_app_state, &args.savefile)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
use clap::Parser;
|
|
||||||
mod crossterm;
|
|
||||||
use std::{env, error::Error, fs::OpenOptions};
|
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(name = "Brittling")]
|
#[command(name = "Brittle")]
|
||||||
#[command(about = "A tool to perform snowballing for literature studies", long_about = None)]
|
#[command(about = "A tool to perform snowballing for literature studies", long_about = None)]
|
||||||
struct Args {
|
struct Args {
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
@@ -89,21 +68,9 @@ async fn main() {
|
|||||||
)))
|
)))
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
match run(&args).await {
|
if let Err(e) = run(&args).await {
|
||||||
Err(e) => {
|
|
||||||
error!("Application error: {}", e);
|
error!("Application error: {}", e);
|
||||||
print!("{e:?}");
|
print!("{e:?}");
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn run(args: &Args) -> Result<(), Box<dyn Error>> {
|
|
||||||
let starting_app_state = deserialize_savefile(&args.savefile)?;
|
|
||||||
let final_app_state = crate::crossterm::run(starting_app_state).await?;
|
|
||||||
|
|
||||||
serialize_savefile(&final_app_state, &args.savefile)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -298,7 +298,7 @@ fn draw_left_pane(frame: &mut Frame, app: &mut AppState, area: Rect) {
|
|||||||
Line::from(vec![
|
Line::from(vec![
|
||||||
Span::raw("Step: "),
|
Span::raw("Step: "),
|
||||||
Span::styled(
|
Span::styled(
|
||||||
app.history.previoius_iterations.len().to_string(),
|
app.history.previous_iterations.len().to_string(),
|
||||||
Style::default().fg(Color::Cyan),
|
Style::default().fg(Color::Cyan),
|
||||||
),
|
),
|
||||||
]),
|
]),
|
||||||
|
|||||||
Reference in New Issue
Block a user