1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2025-02-05 00:54:42 +00:00
ibis/src/utils.rs

16 lines
621 B
Rust
Raw Normal View History

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).
2023-11-15 00:32:46 +00:00
pub fn generate_object_id(domain: &Url) -> Result<Url, ParseError> {
let port = domain.port().unwrap();
2023-11-15 00:32:46 +00:00
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))
}