new: Proper reply when invalid data is sent

This commit is contained in:
Sayantan Santra 2024-03-31 21:23:22 -05:00
parent a9168e3459
commit 38b817fdf8
Signed by: SinTan1729
GPG Key ID: EB3E68BFBA25C85F
2 changed files with 10 additions and 6 deletions

2
.gitignore vendored
View File

@ -8,4 +8,4 @@ urls.sqlite
.vscode/
**/.directory
.env
cookie*

View File

@ -1,13 +1,13 @@
use std::env;
use crate::database;
use nanoid::nanoid;
use rand::seq::SliceRandom;
use regex::Regex;
use rusqlite::Connection;
use serde::Deserialize;
use std::env;
#[derive(Deserialize)]
use crate::database;
#[derive(Deserialize, Default, PartialEq)]
struct URLPair {
shortlink: String,
longlink: String,
@ -32,7 +32,11 @@ pub fn getall(db: &Connection) -> String {
}
pub fn add_link(req: String, db: &Connection) -> (bool, String) {
let mut chunks: URLPair = serde_json::from_str(&req).unwrap();
let mut chunks: URLPair = serde_json::from_str(&req).unwrap_or_default();
if chunks == URLPair::default() {
return (false, String::from("Invalid request!"));
}
let style = env::var("slug_style").unwrap_or(String::from("Pair"));
let len_str = env::var("slug_length").unwrap_or(String::from("8"));