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:
* Glib 2.56+
* GTK 4.8+
* Libadwaita 1.4+

View File

@ -7,15 +7,13 @@ use widget::Widget;
use gtk::{
gio::SimpleAction,
glib::{GString, Uri},
glib::{GString, TimeZone, Uri},
prelude::{TextBufferExt, TextBufferExtManual},
TextBuffer, TextTag, TextTagTable, TextView, WrapMode,
};
use std::sync::Arc;
const NEW_LINE: &str = "\n";
pub struct Reader {
title: Option<GString>,
// css: CssProvider,
@ -68,7 +66,6 @@ impl Reader {
tags.add(&link);
// Parse lines
let buffer = TextBuffer::new(Some(&tags));
for line in gemtext.lines() {
@ -88,7 +85,7 @@ impl Reader {
&[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
// this feature wanted to update parent elements like tab title
@ -100,7 +97,7 @@ impl Reader {
}
// 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
let mut alt = Vec::new();
@ -112,8 +109,11 @@ impl Reader {
}
// Append date on exist
if let Some(date) = link.date {
alt.push(date.to_string());
if let Some(timestamp) = link.timestamp {
// 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
@ -123,14 +123,14 @@ impl Reader {
});
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;
}
// Nothing match, use plain text @TODO
buffer.insert(&mut buffer.end_iter(), line);
buffer.insert(&mut buffer.end_iter(), NEW_LINE);
buffer.insert(&mut buffer.end_iter(), "\n");
}
// 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 alt: Option<GString>,
pub date: Option<GString>, // @TODO https://docs.gtk.org/glib/struct.Date.html
pub is_external: Option<bool>, // on to_base option provided
pub uri: Uri,
pub alt: Option<GString>, // [optional] alternative link description
pub is_external: Option<bool>, // [optional] external link indication, on base option provided
pub timestamp: Option<DateTime>, // [optional] valid link DateTime object
pub uri: Uri, // [required] valid link URI object
}
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
let mut alt = None;
let mut date = None;
let mut timestamp = None;
let mut is_external = None;
// Begin line parse
@ -26,7 +28,7 @@ impl Link {
let unresolved_address = regex.get(1)?;
// Convert address to the valid URI
let uri = match to_base {
let uri = match base {
// Base conversion requested
Some(base_uri) => {
// Convert relative address to absolute
@ -64,9 +66,15 @@ impl Link {
}
};
// Date
if let Some(value) = regex.get(2) {
date = Some(GString::from(value.as_str()))
// Timestamp
if let Some(date) = regex.get(2) {
// @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
@ -76,8 +84,8 @@ impl Link {
Some(Self {
alt,
date,
is_external,
timestamp,
uri,
})
}