Skip to content

Commit cf06a74

Browse files
committed
Make Id generation part of its implementation
1 parent d0aeb01 commit cf06a74

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

src/handlers/insert/api.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use crate::db::{Database, write};
2-
use crate::errors::{Error, JsonErrorResponse};
2+
use crate::errors::JsonErrorResponse;
33
use crate::id::Id;
44
use axum::Json;
55
use axum::extract::State;
6-
use rand::Rng;
76
use serde::{Deserialize, Serialize};
87
use std::num::NonZeroU32;
98

@@ -40,11 +39,7 @@ pub async fn post(
4039
State(db): State<Database>,
4140
Json(entry): Json<Entry>,
4241
) -> Result<Json<RedirectResponse>, JsonErrorResponse> {
43-
let id: Id = tokio::task::spawn_blocking(|| rand::rng().random::<u32>())
44-
.await
45-
.map_err(Error::from)?
46-
.into();
47-
42+
let id = Id::new();
4843
let entry: write::Entry = entry.into();
4944
let path = format!("/{}", id.to_url_path(&entry));
5045
db.insert(id, entry).await?;

src/handlers/insert/form.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
use crate::Page;
12
use crate::db::{Database, write};
23
use crate::handlers::extract::{Theme, Uid};
34
use crate::handlers::html::make_error;
45
use crate::id::Id;
5-
use crate::{Error, Page};
66
use axum::extract::{Form, State};
77
use axum::http::HeaderMap;
88
use axum::response::{IntoResponse, Redirect};
99
use axum_extra::extract::cookie::{Cookie, SameSite, SignedCookieJar};
10-
use rand::Rng;
1110
use serde::{Deserialize, Serialize};
1211
use std::num::NonZeroU32;
1312

@@ -66,11 +65,6 @@ pub async fn post(
6665
.unwrap_or(false);
6766

6867
async {
69-
let id: Id = tokio::task::spawn_blocking(|| rand::rng().random::<u32>())
70-
.await
71-
.map_err(Error::from)?
72-
.into();
73-
7468
// Use cookie uid or generate a new one.
7569
let uid = if let Some(Uid(uid)) = uid {
7670
uid
@@ -81,6 +75,7 @@ pub async fn post(
8175
let mut entry: write::Entry = entry.into();
8276
entry.uid = Some(uid);
8377

78+
let id = Id::new();
8479
let mut url = id.to_url_path(&entry);
8580

8681
if entry.burn_after_reading.unwrap_or(false) {

src/id.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::db::write::Entry;
22
use crate::errors::Error;
3+
use rand::Rng;
34
use std::fmt;
45
use std::str::FromStr;
56

@@ -17,6 +18,13 @@ pub(crate) struct Id {
1718
}
1819

1920
impl Id {
21+
/// Generate a new random [`Id`]. According to the [`rand::rng()`] documentation this should be
22+
/// fast and not require additional an `spawn_blocking()` call.
23+
pub fn new() -> Self {
24+
let n = rand::rng().random::<u32>();
25+
Self { n }
26+
}
27+
2028
/// Return i64 representation for database storage purposes.
2129
pub fn to_i64(self) -> i64 {
2230
self.n.into()

0 commit comments

Comments
 (0)