diff options
| author | Teddy Wing | 2018-10-22 20:14:55 +0200 |
|---|---|---|
| committer | Teddy Wing | 2018-10-22 20:14:55 +0200 |
| commit | 26156433c51cdcfa175c41de28c11828eb78c2eb (patch) | |
| tree | 4a4084739773865eac1aa10e547c9a31e61cb613 /src | |
| parent | 0736dd67d690c597e93324e19126f6c48b81f7c4 (diff) | |
| download | dome-key-map-26156433c51cdcfa175c41de28c11828eb78c2eb.tar.bz2 | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/errors.rs | 1 | ||||
| -rw-r--r-- | src/trial.rs | 43 |
2 files changed, 40 insertions, 4 deletions
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<Local>, now: DateTime<Local>, days_available: u8, -) -> Result<u8, DurationError> { +) -> result::Result<u8, DurationError> { 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<Local>) -> Result<u8, DurationError> { +fn days_remaining_from_now( + start: DateTime<Local> +) -> result::Result<u8, DurationError> { days_remaining(start, Local::now(), DAYS_REMAINING) } @@ -40,7 +73,9 @@ fn encode_datetime(d: DateTime<Local>) -> String { format!("{}//{}", timestamp, iv) } -fn decode_datetime(s: &str) -> Result<DateTime<FixedOffset>, DateCryptError> { +fn decode_datetime( + s: &str +) -> result::Result<DateTime<FixedOffset>, DateCryptError> { let encrypted: Vec<_> = s.rsplitn(2, "//").collect(); let timestamp = encrypted[0]; let iv = encrypted[1]; |
