### Placeholder resolution example (text)
Source: https://docs.rs/evtx/latest/src/evtx/binxml/ir.rs.html?search=
Conceptual example in the doc comment for resolve_placeholder_into. Shows how a placeholder resolves to an array value and how the containing element is later repeated.
```text
Template: %{0}
Placeholder(0) resolves to Value(StringArrayType(["a","b"]))
=> out += [ Value(StringArrayType(["a","b"])) ]
=> Data element is then repeated into two nodes.
```
--------------------------------
### and_then example function
Source: https://docs.rs/evtx/latest/evtx/err/type.Result.html?search=
Example function for and_then, showing control flow based on Result values.
```rust
fn sq_then_to_string(x: u32) -> Result {
x.checked_mul(x).map(|sq| sq.to_string()).ok_or("overflowed")
}
```
--------------------------------
### Timestamp::as_millisecond() example
Source: https://docs.rs/evtx/latest/evtx/struct.Timestamp.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E
Use to verify the whole milliseconds since the Unix epoch. The fractional milliseconds are truncated. The example covers positive and negative timestamps.
```rust
use jiff::Timestamp;
let ts = Timestamp::new(5, 123_456_789)?;
assert_eq!(ts.as_millisecond(), 5_123);
let ts = Timestamp::new(5, 999_999_999)?;
assert_eq!(ts.as_millisecond(), 5_999);
let ts = Timestamp::new(-5, -123_456_789)?;
assert_eq!(ts.as_millisecond(), -5_123);
let ts = Timestamp::new(-5, -999_999_999)?;
assert_eq!(ts.as_millisecond(), -5_999);
```
--------------------------------
### Timestamp::as_microsecond() example
Source: https://docs.rs/evtx/latest/evtx/struct.Timestamp.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E
Use to verify the whole microseconds since the Unix epoch. The fractional microseconds are truncated. The example covers positive and negative timestamps.
```rust
use jiff::Timestamp;
let ts = Timestamp::new(5, 123_456_789)?;
assert_eq!(ts.as_microsecond(), 5_123_456);
let ts = Timestamp::new(5, 999_999_999)?;
assert_eq!(ts.as_microsecond(), 5_999_999);
let ts = Timestamp::new(-5, -123_456_789)?;
assert_eq!(ts.as_microsecond(), -5_123_456);
let ts = Timestamp::new(-5, -999_999_999)?;
assert_eq!(ts.as_microsecond(), -5_999_999);
```
--------------------------------
### Template value conversion example
Source: https://docs.rs/evtx/latest/src/evtx/binxml/ir.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E
Illustrates the conversion of raw template substitution values into TemplateValue variants, showing how BinXmlType fragments become BinXmlElement references. This is a conceptual example from the documentation.
```text
values_raw = [ StringType("x"), BinXmlType(y) ]
values = [ Value(StringType("x")), BinXmlElement(id_of_C) ]
```
--------------------------------
### Timestamp::new example
Source: https://docs.rs/evtx/latest/evtx/struct.Timestamp.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E
Constructs a Timestamp from seconds and nanoseconds. The example shows the instant 123,456,789 seconds after the Unix epoch. Requires the `?` operator in a function returning `Result`.
```rust
use jiff::Timestamp;
assert_eq!(
Timestamp::new(123_456_789, 0)?.to_string(),
"1973-11-29T21:33:09Z",
);
```
--------------------------------
### FromIterator success example
Source: https://docs.rs/evtx/latest/evtx/err/type.Result.html?search=
Example of collecting a vector of Results into a Result containing a vector, checking for overflow.
```rust
let v = vec![1, 2];
let res: Result, &'static str> = v.iter().map(|x: &u32|
x.checked_add(1).ok_or("Overflow!")
).collect();
assert_eq!(res, Ok(vec![2, 3]));
```
--------------------------------
### map example
Source: https://docs.rs/evtx/latest/evtx/err/type.Result.html?search=
Shows mapping the Ok value of a Result using map, leaving Err untouched. The example parses lines and prints doubled numbers.
```rust
let line = "1\n2\n3\n4\n";
for num in line.lines() {
match num.parse::().map(|i| i * 2) {
Ok(n) => println!("{n}"),
Err(..) => {}
}
}
```
--------------------------------
### as_ref example
Source: https://docs.rs/evtx/latest/evtx/err/type.Result.html?search=
Shows converting a Result to a Result with references, leaving the original in place.
```rust
let x: Result = Ok(2);
assert_eq!(x.as_ref(), Ok(&2));
let x: Result = Err("Error");
assert_eq!(x.as_ref(), Err(&"Error"));
```
--------------------------------
### Render JSON text (plain) - start
Source: https://docs.rs/evtx/latest/src/evtx/binxml/compiled.rs.html?search=std%3A%3Avec
Starts the plain JSON text rendering for a `Text` value, returning early if the text is empty. The match on `Text::Utf16` is incomplete in the source.
```rust
fn json_text_plain(&mut self, text: &Text<'_>) -> Result<()> {
if text.is_empty() {
return Ok(());
}
match text {
Text::Utf16(value) => {
```
--------------------------------
### ok example
Source: https://docs.rs/evtx/latest/evtx/err/type.Result.html?search=
Demonstrates converting a Result to an Option, returning Some(value) for Ok and None for Err.
```rust
let x: Result = Ok(2);
assert_eq!(x.ok(), Some(2));
let x: Result = Err("Nothing here");
assert_eq!(x.ok(), None);
```
--------------------------------
### read_open_start_element_cursor: Parse open start element
Source: https://docs.rs/evtx/latest/src/evtx/binxml/tokens.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E
Reads an open start element from a cursor, handling optional dependency identifier and attribute list data size. Returns a BinXMLOpenStartElement with data size and name.
```rust
pub(crate) fn read_open_start_element_cursor(
cursor: &mut ByteCursor<'_>,
has_attributes: bool,
has_dependency_identifier: bool,
name_encoding: BinXmlNameEncoding,
) -> Result {
trace!(
"Offset `0x{:08x}` - OpenStartElement",
cursor.position(),
has_attributes,
has_dependency_identifier
);
// Element start headers come in (at least) two variants:
// - Template definitions: include a dependency identifier (u16)
// - Direct record elements / nested BinXML (substitution value type 0x21): omit it
if has_dependency_identifier {
let dependency_identifier = cursor.u16_named("open_start_element_dependency_identifier")?;
trace!(
"\t Dependency Identifier - `0x{:04x} ({})`",
dependency_identifier, dependency_identifier
);
}
let data_size = cursor.u32_named("open_start_element_data_size")?;
trace!("\t Data Size - {}", data_size);
let name = BinXmlNameRef::from_cursor_with_encoding(cursor, name_encoding)?;
trace!("\t Name - {:?}", name);
let _attribute_list_data_size = if has_attributes {
cursor.u32_named("open_start_element_attribute_list_data_size")?
} else {
0
};
Ok(BinXMLOpenStartElement { data_size, name })
}
```
--------------------------------
### Timestamp::from_duration example
Source: https://docs.rs/evtx/latest/evtx/struct.Timestamp.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E
Shows how to construct a Timestamp from a SignedDuration obtained from a SystemTime. The example computes the duration from the Unix epoch to the current time and asserts the resulting timestamp is after the Unix epoch.
```rust
use std::time::SystemTime;
use jiff::{SignedDuration, Timestamp};
let unix_epoch = SystemTime::UNIX_EPOCH;
let now = SystemTime::now();
let duration = SignedDuration::system_until(unix_epoch, now)?;
let ts = Timestamp::from_duration(duration)?;
assert!(ts > Timestamp::UNIX_EPOCH);
```
--------------------------------
### process_open_start_element method
Source: https://docs.rs/evtx/latest/src/evtx/binxml/ir.rs.html?search=
Handles an open start element token, creating a new ElementBuilder and setting it as the current element.
```rust
fn process_open_start_element(&mut self, elem: BinXMLOpenStartElement) -> Result<()> {
if self.current_element.is_some() {
return Err(EvtxError::FailedToCreateRecordModel(
"open start - Bad parser state",
));
}
let name = Name::new(self.expand_name_ref(&elem.name)?);
self.current_element = Some(ElementBuilder::new(name, self.bump));
Ok(())
}
```
--------------------------------
### as_mut example
Source: https://docs.rs/evtx/latest/evtx/err/type.Result.html?search=
Demonstrates mutating the contained values of a Result using as_mut and a helper function.
```rust
fn mutate(r: &mut Result) {
match r.as_mut() {
Ok(v) => *v = 42,
Err(e) => *e = 0,
}
}
let mut x: Result = Ok(2);
mutate(&mut x);
assert_eq!(x.unwrap(), 42);
let mut x: Result = Err(13);
mutate(&mut x);
assert_eq!(x.unwrap_err(), 0);
```
--------------------------------
### Timestamp::as_duration() example
Source: https://docs.rs/evtx/latest/evtx/struct.Timestamp.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E
Use to verify the SignedDuration since the Unix epoch. The example covers the UNIX_EPOCH, a positive timestamp, and a negative timestamp.
```rust
use jiff::{SignedDuration, Timestamp};
assert_eq!(
Timestamp::UNIX_EPOCH.as_duration(),
SignedDuration::ZERO,
);
assert_eq!(
Timestamp::new(5, 123_456_789)?.as_duration(),
SignedDuration::new(5, 123_456_789),
);
assert_eq!(
Timestamp::new(-5, -123_456_789)?.as_duration(),
SignedDuration::new(-5, -123_456_789),
);
```
--------------------------------
### Timestamp::from_nanosecond example
Source: https://docs.rs/evtx/latest/evtx/struct.Timestamp.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E
Shows how to construct a Timestamp from a nanosecond value relative to the Unix epoch. The example asserts that 1 nanosecond after the epoch formats as "1970-01-01T00:00:00.000000001Z" and 1 nanosecond before formats as "1969-12-31T23:59:59.999999999Z".
```rust
use jiff::Timestamp;
assert_eq!(
Timestamp::from_nanosecond(1)?.to_string(),
"1970-01-01T00:00:00.000000001Z",
);
assert_eq!(
Timestamp::from_nanosecond(-1)?.to_string(),
"1969-12-31T23:59:59.999999999Z",
);
```
--------------------------------
### Timestamp::duration_until example
Source: https://docs.rs/evtx/latest/evtx/struct.Timestamp.html
Shows how to compute the signed duration between two timestamps with `Timestamp::duration_until`. The first example demonstrates the basic usage and negative results when the order is flipped. The second example shows conversion between `SignedDuration` and `Span`, which is guaranteed to succeed because the duration is always expressed in uniform units (hours or smaller).
```rust
use jiff::{Timestamp, SignedDuration};
let earlier: Timestamp = "2006-08-24T22:30:00Z".parse()?;
let later: Timestamp = "2019-01-31 21:00:00Z".parse()?;
assert_eq!(
earlier.duration_until(later),
SignedDuration::from_secs(392509800),
);
// Flipping the timestamps is fine, but you'll get a negative span.
assert_eq!(
later.duration_until(earlier),
SignedDuration::from_secs(-392509800),
);
```
```rust
use jiff::{SignedDuration, Span, Timestamp};
let ts1: Timestamp = "2024-02-28T00:00:00Z".parse()?;
let ts2: Timestamp = "2024-03-01T00:00:00Z".parse()?;
let dur = ts1.duration_until(ts2);
// Guaranteed to never fail because the duration
// between two civil times never exceeds the limits
// of a `Span`.
let span = Span::try_from(dur).unwrap();
assert_eq!(format!("{span:#}"), "172800s");
// Guaranteed to succeed and always return the original
// duration because the units are always hours or smaller,
// and thus uniform. This means a relative datetime is
// never required to do this conversion.
let dur = SignedDuration::try_from(span).unwrap();
assert_eq!(dur, SignedDuration::from_secs(172_800));
```
--------------------------------
### Timestamp::as_nanosecond() example
Source: https://docs.rs/evtx/latest/evtx/struct.Timestamp.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E
Use to verify the total nanoseconds since the Unix epoch. The result is lossless and can be used with Timestamp::from_nanosecond to recreate the timestamp. The example covers positive and negative timestamps.
```rust
use jiff::Timestamp;
let ts = Timestamp::new(5, 123_456_789)?;
assert_eq!(ts.as_nanosecond(), 5_123_456_789);
let ts = Timestamp::new(5, 999_999_999)?;
assert_eq!(ts.as_nanosecond(), 5_999_999_999);
let ts = Timestamp::new(-5, -123_456_789)?;
assert_eq!(ts.as_nanosecond(), -5_123_456_789);
let ts = Timestamp::new(-5, -999_999_999)?;
assert_eq!(ts.as_nanosecond(), -5_999_999_999);
```
--------------------------------
### Timestamp::subsec_millisecond() example
Source: https://docs.rs/evtx/latest/evtx/struct.Timestamp.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E
Use to verify the fractional second component in milliseconds. The value is guaranteed to be within -1 to 1 second. The example covers positive and negative timestamps.
```rust
use jiff::Timestamp;
let ts = Timestamp::new(5, 123_456_789)?;
assert_eq!(ts.subsec_millisecond(), 123);
let ts = Timestamp::new(5, 999_999_999)?;
assert_eq!(ts.subsec_millisecond(), 999);
let ts = Timestamp::new(-5, -123_456_789)?;
assert_eq!(ts.subsec_millisecond(), -123);
let ts = Timestamp::new(-5, -999_999_999)?;
assert_eq!(ts.subsec_millisecond(), -999);
```
--------------------------------
### err example
Source: https://docs.rs/evtx/latest/evtx/err/type.Result.html?search=
Demonstrates converting a Result to an Option, returning Some(error) for Err and None for Ok.
```rust
let x: Result = Ok(2);
assert_eq!(x.err(), None);
let x: Result = Err("Nothing here");
assert_eq!(x.err(), Some("Nothing here"));
```
--------------------------------
### is_ok method example
Source: https://docs.rs/evtx/latest/evtx/err/type.Result.html?search=
Checks if a result is `Ok`.
```rust
let x: Result = Ok(-3);
assert_eq!(x.is_ok(), true);
let x: Result = Err("Some error message");
assert_eq!(x.is_ok(), false);
```
--------------------------------
### Timestamp::subsec_microsecond() example
Source: https://docs.rs/evtx/latest/evtx/struct.Timestamp.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E
Use to verify the fractional second component in microseconds. The value is guaranteed to be within -1 to 1 second. The example covers positive and negative timestamps.
```rust
use jiff::Timestamp;
let ts = Timestamp::new(5, 123_456_789)?;
assert_eq!(ts.subsec_microsecond(), 123_456);
let ts = Timestamp::new(5, 999_999_999)?;
assert_eq!(ts.subsec_microsecond(), 999_999);
let ts = Timestamp::new(-5, -123_456_789)?;
assert_eq!(ts.subsec_microsecond(), -123_456);
let ts = Timestamp::new(-5, -999_999_999)?;
assert_eq!(ts.subsec_microsecond(), -999_999);
```
--------------------------------
### is_ok_and method example
Source: https://docs.rs/evtx/latest/evtx/err/type.Result.html?search=
Returns true if the result is `Ok` and the value matches a predicate.
```rust
let x: Result = Ok(2);
assert_eq!(x.is_ok_and(|x| x > 1), true);
let x: Result = Ok(0);
assert_eq!(x.is_ok_and(|x| x > 1), false);
let x: Result = Err("hey");
assert_eq!(x.is_ok_and(|x| x > 1), false);
let x: Result = Ok("ownership".to_string());
assert_eq!(x.as_ref().is_ok_and(|x| x.len() > 1), true);
println!("still alive {:?}", x);
```
--------------------------------
### Timestamp::from_second example
Source: https://docs.rs/evtx/latest/evtx/struct.Timestamp.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E
Constructs a `Timestamp` from seconds since the Unix epoch. The example shows 1 second after and before the epoch. Returns an error if the value is outside `Timestamp::MIN` and `Timestamp::MAX`.
```rust
use jiff::Timestamp;
assert_eq!(
Timestamp::from_second(1)?.to_string(),
"1970-01-01T00:00:01Z",
);
assert_eq!(
Timestamp::from_second(-1)?.to_string(),
"1969-12-31T23:59:59Z",
);
```
--------------------------------
### is_zero() example
Source: https://docs.rs/evtx/latest/evtx/struct.Timestamp.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E
Checks that the Unix epoch instant is recognized as zero.
```rust
use jiff::Timestamp;
assert!(Timestamp::UNIX_EPOCH.is_zero());
```
--------------------------------
### copied method example
Source: https://docs.rs/evtx/latest/evtx/err/type.Result.html?search=
Demonstrates `Result<&T, E>::copied` copying the `Ok` value from a shared reference.
```rust
let val = 12;
let x: Result<&i32, i32> = Ok(&val);
assert_eq!(x, Ok(&12));
let copied = x.copied();
assert_eq!(copied, Ok(12));
```
--------------------------------
### transpose method example
Source: https://docs.rs/evtx/latest/evtx/err/type.Result.html?search=
Transposes a `Result