nautechsystems/nautilus_trader · error
Failed to parse string '{value}' into UnixNanos: {e}. Use st
Error message
Failed to parse string '{value}' into UnixNanos: {e}. Use str::parse() for non-panicking error handling. What it means
`impl From<&str> for UnixNanos` parses the string as a u64 nanosecond value and panics on any parse failure. The panic message explicitly points to the intended non-panicking alternative: `str::parse::<UnixNanos>()`, which returns a Result. This error is thrown for malformed or out-of-range strings such as non-numeric text, negative numbers, or values exceeding u64::MAX.
Source
Thrown at crates/core/src/nanos.rs:888
value.0
}
}
/// Converts a string slice to [`UnixNanos`].
///
/// # Panics
///
/// This implementation will panic if the string cannot be parsed into a valid [`UnixNanos`].
/// This is intentional fail-fast behavior where invalid timestamps indicate a critical
/// logic error that should halt execution rather than silently propagate incorrect data.
///
/// For error handling without panicking, use [`str::parse::<UnixNanos>()`] which returns
/// a [`Result`].
impl From<&str> for UnixNanos {
fn from(value: &str) -> Self {
value
.parse()
.unwrap_or_else(|e| panic!("Failed to parse string '{value}' into UnixNanos: {e}. Use str::parse() for non-panicking error handling."))
}
}
/// Converts a [`String`] to [`UnixNanos`].
///
/// # Panics
///
/// This implementation will panic if the string cannot be parsed into a valid [`UnixNanos`].
/// This is intentional fail-fast behavior where invalid timestamps indicate a critical
/// logic error that should halt execution rather than silently propagate incorrect data.
///
/// For error handling without panicking, use [`str::parse::<UnixNanos>()`] which returns
/// a [`Result`].
impl From<String> for UnixNanos {
fn from(value: String) -> Self {
value
.parse()
.unwrap_or_else(|e| panic!("Failed to parse string '{value}' into UnixNanos: {e}. Use str::parse() for non-panicking error handling."))View on GitHub (pinned to 18893faf8b)
Solutions
- Use `value.parse::<UnixNanos>()` (or `parse::<u64>()`) and handle the Err instead of the panicking `From` impl.
- Pre-validate the string: trim, ensure it is all ASCII digits and fits in u64 before conversion.
- Handle common formats upstream: strip units, reject/persist floats by converting with explicit precision, check for a leading '-'.
- If the input may legitimately be absent, return Option/Result from your wrapper rather than calling From at all.
Example fix
// before
let ts = UnixNanos::from(raw_field); // panics on bad input
// after
let ts: UnixNanos = raw_field.trim().parse().map_err(|e| {
anyhow!("invalid UnixNanos field '{raw_field}': {e}")
})?; Defensive patterns
Strategy: try-catch
Validate before calling
fn parse_unix_nanos_str(s: &str) -> Result<UnixNanos, String> {
let t = s.trim();
if t.is_empty() || !t.bytes().all(|b| b.is_ascii_digit()) {
return Err(format!("not a non-negative integer: '{s}'"));
}
t.parse::<UnixNanos>().map_err(|e| e.to_string())
} Type guard
fn is_u64_literal(s: &str) -> bool {
!s.is_empty() && s.bytes().all(|b| b.is_ascii_digit())
} Try / catch
// Rust panics are not catchable in normal code; avoid the panic by
// using the Result-returning path instead of From<&str>:
let ts: UnixNanos = s.trim().parse().map_err(|e| anyhow!("bad UnixNanos '{s}': {e}"))?; Prevention
- Never use UnixNanos::from(&str) on untrusted strings; always prefer str::parse::<UnixNanos>().
- Trim and digit-check strings from env/config/CSV before parsing.
- Handle empty and missing fields explicitly rather than letting parse panic.
- Reject float-formatted and unit-suffixed values at the schema level.
When it happens
Trigger: Calling `UnixNanos::from("abc")`, `UnixNanos::from("-1")` (negative is invalid for u64), `UnixNanos::from("99999999999999999999999")` (exceeds u64::MAX), or any string with whitespace/units (e.g. "123ns", "1.5"). Also `"abc".into()` / `"abc".to_string().into()` conversion sites.
Common situations: Reading timestamps from environment variables, CLI args, config files, or CSV/JSON payloads where the field may be empty, contain a units suffix, be a float, or be negative; log-parsing code feeding raw tokens into UnixNanos::from.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- UnixNanos overflow in from_seconds
- UnixNanos overflow in from_millis
- UnixNanos overflow in from_micros
- in-flight mutex poisoned
- wallet balance mutex poisoned
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/9300a560fd5f1db5.
Report an issue: GitHub.