nostralink 0.2.1

Linked data library for nostr
Documentation
//! Util

use nostr::{Event, JsonUtil};
use oxigraph::model::*;
use serde_json::Value;
use std::fs;
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};

pub fn get_epoch_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs()
}

pub fn now_literal() -> Literal {
    Literal::from(get_epoch_ms())
}

pub fn pretty_event(
    event: &Event,
) -> Result<String, Box<dyn std::error::Error>> {
    let object: Value = serde_json::from_str(event.as_json().as_str())?;

    Ok(serde_json::to_string_pretty(&object)?)
}

/// Returns the number of seconds elapsed since a file was modified
pub fn file_modified_time<P: AsRef<Path>>(
    path: P,
) -> Result<u64, Box<dyn std::error::Error>> {
    match fs::metadata(path)?.modified() {
        Ok(mt) => Ok(SystemTime::now().duration_since(mt)?.as_secs()),
        Err(_e) => Err(Box::from("File Metadata error")),
    }
}