logins/
util.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use crate::error::*;
6use rusqlite::Row;
7use std::time;
8use url::Url;
9
10pub fn url_host_port(url_str: &str) -> Option<String> {
11    let url = Url::parse(url_str).ok()?;
12    let host = url.host_str()?;
13    Some(if let Some(p) = url.port() {
14        format!("{}:{}", host, p)
15    } else {
16        host.to_string()
17    })
18}
19
20pub fn system_time_millis_from_row(row: &Row<'_>, col_name: &str) -> Result<time::SystemTime> {
21    let time_ms = row.get::<_, Option<i64>>(col_name)?.unwrap_or_default() as u64;
22    Ok(time::UNIX_EPOCH + time::Duration::from_millis(time_ms))
23}
24
25pub fn duration_ms_i64(d: time::Duration) -> i64 {
26    (d.as_secs() as i64) * 1000 + (i64::from(d.subsec_nanos()) / 1_000_000)
27}
28
29pub fn system_time_ms_i64(t: time::SystemTime) -> i64 {
30    duration_ms_i64(t.duration_since(time::UNIX_EPOCH).unwrap_or_default())
31}
32
33#[cfg(test)]
34pub(crate) fn init_test_logging() {
35    use std::sync::Once;
36    static INIT_LOGGING: Once = Once::new();
37    INIT_LOGGING.call_once(|| {
38        error_support::init_for_tests_with_level(error_support::Level::Trace);
39    });
40}