feat: resolve daletl titles

This commit is contained in:
Artemy Egorov 2024-08-07 15:33:36 +03:00
parent 055b03838d
commit eb448cc1b7
4 changed files with 22 additions and 11 deletions

4
src-tauri/Cargo.lock generated
View file

@ -609,9 +609,9 @@ dependencies = [
[[package]] [[package]]
name = "dalet" name = "dalet"
version = "1.0.0-pre.12" version = "1.0.0-pre.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcb1baee9a3c3f0f6aed8cb4cf29331717227cb92dd6204e2df963dd315d69c1" checksum = "bf04981045a3b665c3b52a753a72f976489aac293462363375767d91b1ae74fb"
dependencies = [ dependencies = [
"clap", "clap",
"enum-procs", "enum-procs",

View file

@ -26,7 +26,7 @@ tauri = { version = "1", features = [
] } ] }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
dalet = "1.0.0-pre.12" dalet = "1.0.0-pre.14"
tokio = { version = "1.39.2", features = ["full"] } tokio = { version = "1.39.2", features = ["full"] }
tokio-rustls = { version = "0.26.0", default-features = false, features = [ tokio-rustls = { version = "0.26.0", default-features = false, features = [

View file

@ -1,9 +1,16 @@
use bytes::Bytes; use bytes::Bytes;
use dalet::{daletl::DlPage, parsers::gemtext::parse_gemtext, typed::Tag::*}; use dalet::{
daletl::DlPage,
parsers::gemtext::parse_gemtext,
typed::{ResolveTitle, Tag::*},
};
use mime::Mime; use mime::Mime;
use std::str; use std::str;
use crate::types::{VigiError, VigiOutput}; use crate::{
types::{VigiError, VigiOutput},
utils::truncate,
};
pub async fn process_data(mime: Mime, data: Bytes) -> Result<VigiOutput, VigiError> { pub async fn process_data(mime: Mime, data: Bytes) -> Result<VigiOutput, VigiError> {
let result = match (mime.type_().as_str(), mime.subtype().as_str()) { let result = match (mime.type_().as_str(), mime.subtype().as_str()) {
@ -27,13 +34,10 @@ async fn process_text(data: &str) -> VigiOutput {
} }
async fn process_gemini(data: &str) -> Result<VigiOutput, VigiError> { async fn process_gemini(data: &str) -> Result<VigiOutput, VigiError> {
let mut truncated = data.to_owned(); let truncated = truncate(data, 20);
truncated.truncate(50);
let page = parse_gemtext(data).map_err(|_| VigiError::Parse)?; let page = parse_gemtext(data).map_err(|_| VigiError::Parse)?;
let output = DlPage::from(page).data; let title = page.resolve_title().unwrap_or(truncated.to_owned());
let res = VigiOutput::new(truncated, output); Ok(VigiOutput::new(title.to_owned(), DlPage::from(page).data))
Ok(res)
} }

View file

@ -57,3 +57,10 @@ pub fn write_tabs(path: &PathBuf, tabs: &Vec<Tab>) -> Result<(), VigiError> {
) )
.map_err(|_| VigiError::StateUpdate) .map_err(|_| VigiError::StateUpdate)
} }
pub fn truncate(s: &str, max_chars: usize) -> &str {
match s.char_indices().nth(max_chars) {
Some((idx, _)) => &s[..idx],
None => s,
}
}