📦 Add Serde and Serde JSON dependencies for cheat sheet metadata handling

This commit is contained in:
2026-04-13 00:26:08 +08:00
parent 6ac1e61b40
commit 85b319d239
5 changed files with 88 additions and 7 deletions

21
md-rs/Cargo.lock generated
View File

@@ -877,6 +877,8 @@ version = "0.1.0"
dependencies = [
"reqwest",
"rust_util",
"serde",
"serde_json",
"termimad",
]
@@ -1394,6 +1396,19 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.149"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86"
dependencies = [
"itoa",
"memchr",
"serde",
"serde_core",
"zmij",
]
[[package]]
name = "shlex"
version = "1.3.0"
@@ -2294,3 +2309,9 @@ dependencies = [
"quote",
"syn",
]
[[package]]
name = "zmij"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"

View File

@@ -6,4 +6,6 @@ edition = "2024"
[dependencies]
reqwest = { version = "0.13.2", features = ["blocking"] }
rust_util = "0.6.51"
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.149"
termimad = "0.34.1"

View File

@@ -4,6 +4,8 @@
//! [dependencies]
//! reqwest = { version = "0.13.2", features = ["blocking"] }
//! rust_util = "0.6.51"
//! serde = { version = "1.0.228", features = ["derive"] }
//! serde_json = "1.0.149"
//! termimad = "0.34.1"
//! ```
@@ -15,7 +17,9 @@
// println!("\nand now {}\n", skin.inline("a little *too much* **style!** (and `some(code)` too)"));
use rust_util::{opt_result, warning, XResult};
use serde::Deserialize;
use std::io::Read;
use std::process::exit;
use std::{fs, io};
// https://github.com/Canop/termimad
@@ -44,6 +48,18 @@ fn read_to_string(path_opt: Option<&String>) -> XResult<String> {
Ok(buffer)
}
Some(path) => {
let path = if let Some(cheat_sheet_name) = try_cheat_sheet(path) {
if cheat_sheet_name.is_empty() {
print_cheat_sheet_meta();
}
format!(
"https://git.hatter.ink/hatter/cheatsheets/raw/branch/main/{cheat_sheet_name}/CHEATSHEET.md"
)
} else {
path.to_string()
};
let path = &path;
let is_http_path = path.starts_with("http://");
let is_https_path = path.starts_with("https://");
if is_http_path || is_https_path {
@@ -64,5 +80,47 @@ fn read_to_string(path_opt: Option<&String>) -> XResult<String> {
}
}
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260412T235541+08:00.MEUCID7zWIZ1zyjpISPbbROS
// X+wrb0hfpeWaan8mYIaG9hoaAiEAw1jj2TvjDx0Y6egXTfpIQD5XzW91sutNjSz62Nc9taI=
#[derive(Deserialize)]
struct CheatSheetMeta {
name: String,
// aliases: Option<String>,
// repo: String,
description: String,
}
fn print_cheat_sheet_meta() -> ! {
let cheat_sheet_meta_url =
"https://git.hatter.ink/hatter/cheatsheets/raw/branch/main/meta.json";
let cheat_sheet_meta_json = reqwest::blocking::get(cheat_sheet_meta_url)
.expect("Get cheat sheet meta failed")
.text()
.expect("Read cheat sheet meta failed");
let cheat_sheet_meta: Vec<CheatSheetMeta> =
serde_json::from_str(&cheat_sheet_meta_json).expect("Parse cheat sheet meta failed");
let mut cheat_sheet_md = String::new();
cheat_sheet_md.push_str("# Cheat Sheets\n\n");
cheat_sheet_md.push_str("| Name | Description |\n");
cheat_sheet_md.push_str("| ----- | ------ |\n");
for cheat_sheet in &cheat_sheet_meta {
cheat_sheet_md.push_str(&format!(
"| {} | {} |\n",
cheat_sheet.name, cheat_sheet.description
));
}
termimad::print_text(&cheat_sheet_md);
exit(0);
}
fn try_cheat_sheet(path: &str) -> Option<String> {
let cheat_prefixes = ["cheat:", "cheatsheet:", "cheat_sheet:"];
for cheat_prefix in cheat_prefixes {
if path.starts_with(cheat_prefix) {
return Some(path.chars().skip(cheat_prefix.len()).collect::<String>());
}
}
None
}
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260413T002526+08:00.MEUCIQDtbn9VsvXD5i0CDDpX
// vaa/uR8bjm5CuvKZFs9gIVFfxwIgYXGJkKom0g+uG6QB6jqi31d7Vtyg80SWmyRAi5aZCvk=

View File

@@ -82,11 +82,11 @@
},
"md.rs": {
"script_name": "md.rs",
"script_length": 2081,
"script_sha256": "d4723b7f59bd9e805a4a482e3107f2f30ec70d24d8efb3717b7bc7008a1cc67b",
"script_length": 4052,
"script_sha256": "eaf9e006406250ae6841b62ea75d751826e42562e6117b505b77fc9f594cdff5",
"script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/md-rs/src/main.rs",
"publish_time": 1775921621093,
"update_time": 1776009548379
"update_time": 1776011164981
},
"myip.rs": {
"script_name": "myip.rs",

View File

@@ -46,8 +46,8 @@
},
"md-rs": {
"script_name": "md-rs",
"script_length": 2081,
"script_sha256": "d4723b7f59bd9e805a4a482e3107f2f30ec70d24d8efb3717b7bc7008a1cc67b"
"script_length": 4052,
"script_sha256": "eaf9e006406250ae6841b62ea75d751826e42562e6117b505b77fc9f594cdff5"
},
"myip-rs": {
"script_name": "myip-rs",