use crate::errors::AtomicResult;
use url::Url;
pub fn server_url(url: &str) -> AtomicResult<String> {
let mut parsed: Url = Url::parse(url)?;
match parsed.path_segments_mut() {
Ok(mut path) => {
path.clear();
}
Err(_) => return Err(format!("Url {} is not valid.", url).into()),
}
parsed.set_query(None);
Ok(parsed.to_string())
}
pub fn check_valid_url(url: &str) -> AtomicResult<()> {
if !url.starts_with("http") && !url.starts_with("local:") {
return Err(format!("Url does not start with http: {}", url).into());
}
Ok(())
}
pub fn now() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("You're a time traveler")
.as_millis() as i64
}
pub fn random_string(n: usize) -> String {
use rand::Rng;
let random_string: String = rand::thread_rng()
.sample_iter(&rand::distributions::Alphanumeric)
.take(n)
.map(char::from)
.collect();
random_string.to_lowercase()
}
pub fn check_timestamp_in_past(timestamp: i64, difference: i64) -> AtomicResult<()> {
let now = crate::utils::now();
if timestamp > now + difference {
return Err(format!(
"Commit CreatedAt timestamp must lie in the past. Check your clock. Timestamp now: {} CreatedAt is: {}",
now, timestamp
)
.into());
}
return Ok(());
}