Skip to main content

superstac_core/
utils.rs

1use chrono::{DateTime, Utc};
2use url::{ParseError, Url};
3
4use crate::errors::{SuperSTACError, ValidationError};
5
6/// Current UTC timestamp. Centralized so tests can mock it later.
7pub fn get_date_time() -> DateTime<Utc> {
8    Utc::now()
9}
10
11/// Parse and validate a URL string.
12pub fn parse_url(url: &str) -> Result<Url, ParseError> {
13    Url::parse(url)
14}
15
16/// Validate a catalog/provider identifier. Allowed: ASCII letters, digits,
17/// hyphen, underscore. Empty / whitespace-only is rejected.
18pub fn validate_identifier(id: &str) -> Result<(), SuperSTACError> {
19    if id.trim().is_empty() {
20        return Err(ValidationError::MissingField("id".into()).into());
21    }
22
23    if !id.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_') {
24        return Err(ValidationError::InvalidIdentifier(
25            "only ASCII letters, digits, hyphen, and underscore are allowed".into(),
26        )
27        .into());
28    }
29
30    Ok(())
31}