diff options
Diffstat (limited to 'src/trial.rs')
| -rw-r--r-- | src/trial.rs | 43 |
1 files changed, 39 insertions, 4 deletions
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]; |
