hl7probe 0.5.0

Inspect and validate HL7 v2 messages from the command line
Documentation
//! Read and check HL7 v2 messages.
//!
//! A message is decomposed into named fields and checked against the HL7
//! dictionary: required fields, data types, code tables and cross-field
//! consistency. Parsing borrows the text it was given rather than copying it,
//! so a message costs a few hundred bytes on top of the string it came from.
//!
//! ```
//! let text = "MSH|^~\\&|HIS|MERCY|LIS|LAB|20240115143200||ADT^A01|MSG1|P|2.5.1\r\
//!             PID|1||123456^^^MERCY^MR||Smith^John||19850312|M\r";
//!
//! let message = hl7probe::parse(text)?;
//! assert_eq!(message.version(), "2.5.1");
//! assert_eq!(message.type_label(), "ADT^A01");
//!
//! // PID-5.1 is the patient's family name.
//! let pid = message.first("PID").expect("the message has a PID");
//! assert_eq!(pid.comp(5, 1), "Smith");
//!
//! let report = hl7probe::validate(&message);
//! println!("{} errors, {} warnings", report.errors(), report.warnings());
//! # Ok::<(), hl7probe::ParseError>(())
//! ```
//!
//! Messages borrow the text they were read from, so keep it alive for as long
//! as the [`Message`]. Use [`parser::split_messages`] and
//! [`parser::parse_message`] directly to walk a file holding several messages.

pub mod datetime;
pub mod parser;
pub mod spec;
pub mod validate;

// The command line's own presentation, not part of the library API.
mod render;
mod text;
mod tui;
mod view;

#[doc(hidden)]
pub mod cli;

pub use parser::{Component, Field, Message, ParseError, Repetition, Segment, Separators};
pub use validate::{Category, Finding, Report, Severity};

/// Parses the first message in `text`.
///
/// Leading batch wrappers and MLLP framing bytes are skipped, and CR, LF or
/// CRLF line endings are all accepted. For a file holding several messages,
/// use [`parser::split_messages`].
///
/// # Errors
///
/// Returns [`ParseError`] when `text` holds no MSH segment, or when the first
/// message's MSH does not declare a usable set of delimiters.
pub fn parse(text: &str) -> Result<Message<'_>, ParseError> {
    let (raws, _notes) = parser::split_messages(text);
    let Some(first) = raws.first() else {
        return Err(ParseError::no_message());
    };
    parser::parse_message(first)
}

/// Checks a parsed message against the HL7 dictionary.
#[must_use]
pub fn validate(message: &Message<'_>) -> Report {
    validate::validate(message)
}