1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-11-22 17:31:10 +00:00
ibis/src/utils.rs

15 lines
621 B
Rust

use rand::{distributions::Alphanumeric, thread_rng, Rng};
use url::{ParseError, Url};
/// Just generate random url as object id. In a real project, you probably want to use
/// an url which contains the database id for easy retrieval (or store the random id in db).
pub fn generate_object_id(domain: &Url) -> Result<Url, ParseError> {
let port = domain.port().unwrap();
let domain = domain.domain().unwrap();
let id: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(7)
.map(char::from)
.collect();
Url::parse(&format!("http://{}:{}/objects/{}", domain,port, id))
}