diff options
| author | Teddy Wing | 2018-10-22 19:33:14 +0200 |
|---|---|---|
| committer | Teddy Wing | 2018-10-22 19:33:14 +0200 |
| commit | 0736dd67d690c597e93324e19126f6c48b81f7c4 (patch) | |
| tree | 57e0343f29a5f97d47353214267e67fa61615fee /src/trial.rs | |
| parent | a709acca8ba41f21e0222debdbe39186c5106beb (diff) | |
| download | dome-key-map-0736dd67d690c597e93324e19126f6c48b81f7c4.tar.bz2 | |
decode_datetime(): Return a `Result`
Make a custom error type because it's trickier to integrate with
'error-chain' given that 'magic-crypt''s `Error` type doesn't implement
the `std::error::Error` trait.
For the same reason, we can't use that as the `cause` for
`DateCryptError::Decrypt`.
This allows us to capture the errors without panicking.
Include some additional cleanups as a result of removing the `use
std::result` and `use errors::*` imports.
Diffstat (limited to 'src/trial.rs')
| -rw-r--r-- | src/trial.rs | 19 |
1 files changed, 7 insertions, 12 deletions
diff --git a/src/trial.rs b/src/trial.rs index e716559..b536e9b 100644 --- a/src/trial.rs +++ b/src/trial.rs @@ -1,9 +1,7 @@ -use std::result; - use chrono::{DateTime, FixedOffset, Local, TimeZone}; use magic_crypt::{self, MagicCrypt}; -use errors::*; +use errors::{DateCryptError, DurationError}; // Start timestamp on October 1 at 23h // Trial should be valid until November 1 00h @@ -16,7 +14,7 @@ fn days_remaining( start: DateTime<Local>, now: DateTime<Local>, days_available: u8, -) -> result::Result<u8, DurationError> { +) -> Result<u8, DurationError> { let duration = (now.date() - start.date()).num_days() as u8; if duration > days_available { @@ -28,9 +26,7 @@ fn days_remaining( } } -fn days_remaining_from_now( - start: DateTime<Local> -) -> result::Result<u8, DurationError> { +fn days_remaining_from_now(start: DateTime<Local>) -> Result<u8, DurationError> { days_remaining(start, Local::now(), DAYS_REMAINING) } @@ -44,18 +40,17 @@ fn encode_datetime(d: DateTime<Local>) -> String { format!("{}//{}", timestamp, iv) } -fn decode_datetime(s: &str) -> DateTime<FixedOffset> { +fn decode_datetime(s: &str) -> Result<DateTime<FixedOffset>, DateCryptError> { let encrypted: Vec<_> = s.rsplitn(2, "//").collect(); let timestamp = encrypted[0]; let iv = encrypted[1]; let mut mc = MagicCrypt::new(KEY, magic_crypt::SecureBit::Bit64, Some(&iv)); - let timestamp = mc.decrypt_base64_to_string(×tamp) - .expect("unable to read trial key"); + let timestamp = mc.decrypt_base64_to_string(×tamp)?; + let timestamp = DateTime::parse_from_rfc3339(×tamp)?; - DateTime::parse_from_rfc3339(×tamp) - .expect("unable to parse timestamp") + Ok(timestamp) } fn initialization_vector() -> String { |
