From 9fd9873bc19eea7cc51bf818d4914fd1beb31a5a Mon Sep 17 00:00:00 2001 From: Arthur Roberts Date: Mon, 15 Jul 2024 22:07:33 +0100 Subject: [PATCH] Added "From Blockchain Time" / "To Obsolete Time" I hardly believe it myself, but this "just werkded" (TM) the first time --- src/lib.rs | 14 ++++++++++++++ src/main.rs | 19 +++++++++++++++++-- templates/index.html.tera | 15 +++++++++++++-- templates/obsolete_time.html.tera | 7 +++++++ 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 templates/obsolete_time.html.tera diff --git a/src/lib.rs b/src/lib.rs index c82590a..f928bc0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ use rocket::serde::Serialize; use std::convert::Into; use std::sync::RwLock; use serde_with::{serde_as, DisplayFromStr}; +use std::str::FromStr; pub enum BlockchainSupported { BTC, @@ -12,6 +13,19 @@ pub enum BlockchainSupported { ETH, } +impl FromStr for BlockchainSupported { + type Err = String; + fn from_str(val: &str) -> Result { + match val { + "BTC" => Ok(BlockchainSupported::BTC), + "BSV" => Ok(BlockchainSupported::BSV), + "XMR" => Ok(BlockchainSupported::XMR), + "ETH" => Ok(BlockchainSupported::ETH), + _ => Err("You picked an unsupported blockchain type! Create a patch and I'll probably accept it!".to_string()) + } + } +} + type BlockHeight = i64; #[serde_as] diff --git a/src/main.rs b/src/main.rs index e065b38..1ff6e1e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ #[macro_use] extern crate rocket; use rocket_dyn_templates::{context, Template}; -use blockchain_time::{BlockchainInfo, BlockchainSupported, update_all_blockheights, time_to_blockheight_specific_chain}; +use blockchain_time::{BlockchainInfo, BlockchainSupported, update_all_blockheights, time_to_blockheight_specific_chain, blockheight_specific_chain_to_time}; use chrono::prelude::*; #[get("/")] @@ -48,8 +48,23 @@ fn to_blockchain_time(input_datetime: &str) -> Template { Template::render("blockchain_time_results", context) } +#[get("/to_obsolete_time?&")] +fn to_obsolete_time(blockheight: i64, blockchain: &str) -> Template { + let blockchain: Result = blockchain.parse(); + let blockchain = match blockchain { + Ok(bl) => bl, + Err(err) => { + return Template::render("error", context!{ error: err }); + } + }; + let context = context! { + time: blockheight_specific_chain_to_time(blockheight, blockchain) + }; + Template::render("obsolete_time", context) +} + #[launch] fn rocket() -> _ { - rocket::build().mount("/", routes![index, update_blockchains, to_blockchain_time]) + rocket::build().mount("/", routes![index, update_blockchains, to_blockchain_time, to_obsolete_time]) .attach(Template::fairing()) } diff --git a/templates/index.html.tera b/templates/index.html.tera index 9eb692f..c413d70 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -3,7 +3,7 @@

Welcome to Blockchain Time

A.K.A. Big Tim

Convert to Blockchain Time

-
+ @@ -12,7 +12,18 @@

Convert from Blockchain Time

Booo why are you going this way??? Don't you know your blocktimes??

-

UNIMPLEMENTED

+ + + + + + +

Latest Block Heights and Times Cached

diff --git a/templates/obsolete_time.html.tera b/templates/obsolete_time.html.tera new file mode 100644 index 0000000..830c62a --- /dev/null +++ b/templates/obsolete_time.html.tera @@ -0,0 +1,7 @@ +{% extends "base" %} +{% block content %} +

BOOOOOOOOOOOOOOOOOOO

+
here's your obsolete time, in UTC, because timezones are boring to get correct, and no longer needed in the new blocktime order
+

{{time}}

+

Why don't you go back home and try another thing

+{% endblock content %}