replace GString with DateTime object

This commit is contained in:
yggverse 2024-10-13 01:22:00 +03:00
parent 4829d02b5c
commit 1a410860e8
3 changed files with 31 additions and 22 deletions

View File

@ -14,6 +14,7 @@ GTK 4 / Libadwaita client written in Rust
Make sure your system support: Make sure your system support:
* Glib 2.56+
* GTK 4.8+ * GTK 4.8+
* Libadwaita 1.4+ * Libadwaita 1.4+

View File

@ -7,15 +7,13 @@ use widget::Widget;
use gtk::{ use gtk::{
gio::SimpleAction, gio::SimpleAction,
glib::{GString, Uri}, glib::{GString, TimeZone, Uri},
prelude::{TextBufferExt, TextBufferExtManual}, prelude::{TextBufferExt, TextBufferExtManual},
TextBuffer, TextTag, TextTagTable, TextView, WrapMode, TextBuffer, TextTag, TextTagTable, TextView, WrapMode,
}; };
use std::sync::Arc; use std::sync::Arc;
const NEW_LINE: &str = "\n";
pub struct Reader { pub struct Reader {
title: Option<GString>, title: Option<GString>,
// css: CssProvider, // css: CssProvider,
@ -68,7 +66,6 @@ impl Reader {
tags.add(&link); tags.add(&link);
// Parse lines // Parse lines
let buffer = TextBuffer::new(Some(&tags)); let buffer = TextBuffer::new(Some(&tags));
for line in gemtext.lines() { for line in gemtext.lines() {
@ -88,7 +85,7 @@ impl Reader {
&[tag], &[tag],
); );
buffer.insert(&mut buffer.end_iter(), NEW_LINE); buffer.insert(&mut buffer.end_iter(), "\n");
// Set title if empty, on first document header match // Set title if empty, on first document header match
// this feature wanted to update parent elements like tab title // this feature wanted to update parent elements like tab title
@ -100,7 +97,7 @@ impl Reader {
} }
// Is link // Is link
if let Some(link) = Link::from(line, Some(base)) { if let Some(link) = Link::from(line, Some(base), Some(&TimeZone::local())) {
// Build link alt from optional values // Build link alt from optional values
let mut alt = Vec::new(); let mut alt = Vec::new();
@ -112,8 +109,11 @@ impl Reader {
} }
// Append date on exist // Append date on exist
if let Some(date) = link.date { if let Some(timestamp) = link.timestamp {
alt.push(date.to_string()); // https://docs.gtk.org/glib/method.DateTime.format.html
if let Ok(value) = timestamp.format("%Y-%m-%d") {
alt.push(value.to_string())
}
} }
// Append alt on exist or use URL // Append alt on exist or use URL
@ -123,14 +123,14 @@ impl Reader {
}); });
buffer.insert_with_tags_by_name(&mut buffer.end_iter(), &alt.join(" "), &["link"]); buffer.insert_with_tags_by_name(&mut buffer.end_iter(), &alt.join(" "), &["link"]);
buffer.insert(&mut buffer.end_iter(), NEW_LINE); buffer.insert(&mut buffer.end_iter(), "\n");
continue; continue;
} }
// Nothing match, use plain text @TODO // Nothing match, use plain text @TODO
buffer.insert(&mut buffer.end_iter(), line); buffer.insert(&mut buffer.end_iter(), line);
buffer.insert(&mut buffer.end_iter(), NEW_LINE); buffer.insert(&mut buffer.end_iter(), "\n");
} }
// Init widget // Init widget

View File

@ -1,17 +1,19 @@
use gtk::glib::{GString, Regex, RegexCompileFlags, RegexMatchFlags, Uri, UriFlags}; use gtk::glib::{
DateTime, GString, Regex, RegexCompileFlags, RegexMatchFlags, TimeZone, Uri, UriFlags,
};
pub struct Link { pub struct Link {
pub alt: Option<GString>, pub alt: Option<GString>, // [optional] alternative link description
pub date: Option<GString>, // @TODO https://docs.gtk.org/glib/struct.Date.html pub is_external: Option<bool>, // [optional] external link indication, on base option provided
pub is_external: Option<bool>, // on to_base option provided pub timestamp: Option<DateTime>, // [optional] valid link DateTime object
pub uri: Uri, pub uri: Uri, // [required] valid link URI object
} }
impl Link { impl Link {
pub fn from(line: &str, to_base: Option<&Uri>) -> Option<Self> { pub fn from(line: &str, base: Option<&Uri>, timezone: Option<&TimeZone>) -> Option<Self> {
// Define initial values // Define initial values
let mut alt = None; let mut alt = None;
let mut date = None; let mut timestamp = None;
let mut is_external = None; let mut is_external = None;
// Begin line parse // Begin line parse
@ -26,7 +28,7 @@ impl Link {
let unresolved_address = regex.get(1)?; let unresolved_address = regex.get(1)?;
// Convert address to the valid URI // Convert address to the valid URI
let uri = match to_base { let uri = match base {
// Base conversion requested // Base conversion requested
Some(base_uri) => { Some(base_uri) => {
// Convert relative address to absolute // Convert relative address to absolute
@ -64,9 +66,15 @@ impl Link {
} }
}; };
// Date // Timestamp
if let Some(value) = regex.get(2) { if let Some(date) = regex.get(2) {
date = Some(GString::from(value.as_str())) // @TODO even possible, but simpler to work with `DateTime` API
// await for new features in `Date` as better in Gemini context
// https://docs.gtk.org/glib/struct.Date.html
timestamp = match DateTime::from_iso8601(&format!("{date}T00:00:00"), timezone) {
Ok(value) => Some(value),
Err(_) => None,
}
} }
// Alt // Alt
@ -76,8 +84,8 @@ impl Link {
Some(Self { Some(Self {
alt, alt,
date,
is_external, is_external,
timestamp,
uri, uri,
}) })
} }