mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-18 07:24:07 +01:00
Template function return Result
This commit is contained in:
@@ -70,6 +70,7 @@ mod render;
|
||||
mod tauri_plugin_mac_window;
|
||||
mod updates;
|
||||
mod window_menu;
|
||||
mod template_fns;
|
||||
|
||||
const DEFAULT_WINDOW_WIDTH: f64 = 1100.0;
|
||||
const DEFAULT_WINDOW_HEIGHT: f64 = 600.0;
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
use std::collections::HashMap;
|
||||
use std::time::SystemTime;
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
use sqlx::types::{Json, JsonValue};
|
||||
|
||||
use templates::parse_and_render;
|
||||
|
||||
use crate::models::{
|
||||
Environment, EnvironmentVariable, HttpRequest, HttpRequestHeader, HttpUrlParameter, Workspace,
|
||||
};
|
||||
use templates::parse_and_render;
|
||||
use crate::template_fns::timestamp;
|
||||
|
||||
pub fn render_request(r: &HttpRequest, w: &Workspace, e: Option<&Environment>) -> HttpRequest {
|
||||
let r = r.clone();
|
||||
@@ -107,16 +108,10 @@ pub fn render(template: &str, vars: &HashMap<String, String>) -> String {
|
||||
parse_and_render(template, vars, Some(template_callback))
|
||||
}
|
||||
|
||||
fn template_callback(name: &str, _args: HashMap<String, String>) -> String {
|
||||
fn template_callback(name: &str, args: HashMap<String, String>) -> Result<String, String> {
|
||||
match name {
|
||||
"timestamp" => {
|
||||
let now = SystemTime::now();
|
||||
let now: DateTime<Utc> = now.into();
|
||||
now.to_rfc3339()
|
||||
},
|
||||
_ => {
|
||||
"".to_string()
|
||||
}
|
||||
"timestamp" => timestamp(args),
|
||||
_ => Err(format!("Unknown template function {name}")),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
64
src-tauri/src/template_fns.rs
Normal file
64
src-tauri/src/template_fns.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn timestamp(args: HashMap<String, String>) -> Result<String, String> {
|
||||
let from = args.get("from").map(|v| v.as_str()).unwrap_or("now");
|
||||
let format = args.get("format").map(|v| v.as_str()).unwrap_or("rfc3339");
|
||||
|
||||
let dt = match from {
|
||||
"now" => {
|
||||
let now = Utc::now();
|
||||
now
|
||||
}
|
||||
_ => {
|
||||
let json_from = serde_json::to_string(from).unwrap_or_default();
|
||||
let now: DateTime<Utc> = serde_json::from_str(json_from.as_str()).unwrap();
|
||||
now
|
||||
}
|
||||
};
|
||||
|
||||
let result = match format {
|
||||
"rfc3339" => dt.to_rfc3339(),
|
||||
"unix" => dt.timestamp().to_string(),
|
||||
"unix_millis" => dt.timestamp_millis().to_string(),
|
||||
_ => "".to_string(),
|
||||
};
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
// Test it
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::template_fns::timestamp;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[test]
|
||||
fn timestamp_empty() {
|
||||
let args = HashMap::new();
|
||||
assert_ne!(timestamp(args), Ok("".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timestamp_from() {
|
||||
let mut args = HashMap::new();
|
||||
args.insert("from".to_string(), "2024-07-31T14:16:41.983Z".to_string());
|
||||
assert_eq!(timestamp(args), Ok("2024-07-31T14:16:41.983+00:00".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timestamp_format_unix() {
|
||||
let mut args = HashMap::new();
|
||||
args.insert("from".to_string(), "2024-07-31T14:16:41.983Z".to_string());
|
||||
args.insert("format".to_string(), "unix".to_string());
|
||||
assert_eq!(timestamp(args), Ok("1722435401".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timestamp_format_unix_millis() {
|
||||
let mut args = HashMap::new();
|
||||
args.insert("from".to_string(), "2024-07-31T14:16:41.983Z".to_string());
|
||||
args.insert("format".to_string(), "unix_millis".to_string());
|
||||
assert_eq!(timestamp(args), Ok("1722435401983".to_string()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user