From 26156433c51cdcfa175c41de28c11828eb78c2eb Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 22 Oct 2018 20:14:55 +0200 Subject: trial: Add a function to initialise a trial timestamp in a file Save an encoded timestamp to a file. This function should be run once if no trial file is found. The encoded timestamp can then be read on subsequent launches to determine whether the trial period is still in effect. Also add a few function stubs for other actions that seem relevant. --- src/errors.rs | 1 + src/trial.rs | 43 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/errors.rs b/src/errors.rs index c2048e9..17508fe 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -5,6 +5,7 @@ use xdg; error_chain! { foreign_links { Xdg(xdg::BaseDirectoriesError); + Io(::std::io::Error); } } diff --git a/src/trial.rs b/src/trial.rs index b536e9b..238ac13 100644 --- a/src/trial.rs +++ b/src/trial.rs @@ -1,7 +1,12 @@ +use std::fs::File; +use std::io::Write; +use std::result; + use chrono::{DateTime, FixedOffset, Local, TimeZone}; use magic_crypt::{self, MagicCrypt}; +use xdg; -use errors::{DateCryptError, DurationError}; +use errors::*; // Start timestamp on October 1 at 23h // Trial should be valid until November 1 00h @@ -10,11 +15,37 @@ use errors::{DateCryptError, DurationError}; const DAYS_REMAINING: u8 = 30; const KEY: &'static str = "TODO SECRET"; +/// Entry point to the trial handler. +fn do_trial() { +} + +fn initialize_trial_start() -> Result<()> { + let encoded_time = encode_datetime(Local::now()); + + let xdg_dirs = xdg::BaseDirectories::with_prefix("dome-key") + .chain_err(|| "failed to get XDG base directories")?; + let trial_path = xdg_dirs.place_data_file("trial") + .chain_err(|| "failed to get trial file path")?; + let mut trial_file = File::create(trial_path) + .chain_err(|| "failed to create trial file")?; + + write!(&mut trial_file, "{}", encoded_time) + .chain_err(|| "failed to write to trial file")?; + + Ok(()) +} + +fn get_trial_start() { +} + +fn print_trial_days(days: u8) { +} + fn days_remaining( start: DateTime, now: DateTime, days_available: u8, -) -> Result { +) -> result::Result { let duration = (now.date() - start.date()).num_days() as u8; if duration > days_available { @@ -26,7 +57,9 @@ fn days_remaining( } } -fn days_remaining_from_now(start: DateTime) -> Result { +fn days_remaining_from_now( + start: DateTime +) -> result::Result { days_remaining(start, Local::now(), DAYS_REMAINING) } @@ -40,7 +73,9 @@ fn encode_datetime(d: DateTime) -> String { format!("{}//{}", timestamp, iv) } -fn decode_datetime(s: &str) -> Result, DateCryptError> { +fn decode_datetime( + s: &str +) -> result::Result, DateCryptError> { let encrypted: Vec<_> = s.rsplitn(2, "//").collect(); let timestamp = encrypted[0]; let iv = encrypted[1]; -- cgit v1.2.3