stationxml_rs/lib.rs
1//! Pure Rust FDSN StationXML and SeisComP SC3ML reader/writer.
2//!
3//! `stationxml-rs` provides a format-agnostic inventory model with pluggable
4//! backends for different XML formats used in seismology.
5//!
6//! # Supported Formats
7//!
8//! | Format | Read | Write |
9//! |--------|------|-------|
10//! | FDSN StationXML 1.2 | Yes | Yes |
11//! | SeisComP SC3ML 0.6--0.13 | Yes | Yes |
12//!
13//! # Quick Start
14//!
15//! ```no_run
16//! use stationxml_rs::{read_from_file, write_to_string, Sc3ml};
17//!
18//! // Read any format (auto-detects FDSN or SC3ML)
19//! let inv = read_from_file("station.xml").unwrap();
20//! println!("{} networks", inv.networks.len());
21//!
22//! // Write as SC3ML
23//! let xml = write_to_string::<Sc3ml>(&inv).unwrap();
24//! ```
25//!
26//! # Cross-Format Conversion
27//!
28//! ```no_run
29//! use stationxml_rs::{Fdsn, Sc3ml, StationXmlFormat, write_to_string};
30//!
31//! // Read FDSN StationXML
32//! let inv = Fdsn::read_from_str("<FDSNStationXML>...</FDSNStationXML>").unwrap();
33//!
34//! // Convert to SC3ML
35//! let sc3ml = write_to_string::<Sc3ml>(&inv).unwrap();
36//! ```
37
38pub mod builder;
39pub mod conversion;
40pub(crate) mod datetime;
41pub mod error;
42pub mod fdsn;
43pub mod format;
44pub mod inventory;
45pub mod sc3ml;
46pub mod sensor;
47
48pub use builder::InventoryBuilder;
49pub use conversion::AdcConversion;
50pub use error::{Result, StationXmlError};
51pub use fdsn::Fdsn;
52pub use format::{Format, StationXmlFormat, detect_format};
53pub use inventory::*;
54pub use sc3ml::Sc3ml;
55pub use sensor::{SensorEntry, find_sensor, load_sensor_library};
56
57use std::path::Path;
58
59/// Read from file with auto-format detection.
60pub fn read_from_file(path: impl AsRef<Path>) -> Result<Inventory> {
61 let content = std::fs::read_to_string(path)?;
62 read_from_str(&content)
63}
64
65/// Read from string with auto-format detection.
66pub fn read_from_str(xml: &str) -> Result<Inventory> {
67 match detect_format(xml) {
68 Some(Format::Fdsn) => Fdsn::read_from_str(xml),
69 Some(Format::Sc3ml) => Sc3ml::read_from_str(xml),
70 None => Err(StationXmlError::UnknownFormat),
71 }
72}
73
74/// Read from file with explicit format.
75pub fn read_from_file_as<F: StationXmlFormat>(path: impl AsRef<Path>) -> Result<Inventory> {
76 let content = std::fs::read_to_string(path)?;
77 F::read_from_str(&content)
78}
79
80/// Write to file with explicit format.
81pub fn write_to_file<F: StationXmlFormat>(
82 path: impl AsRef<Path>,
83 inventory: &Inventory,
84) -> Result<()> {
85 let xml = F::write_to_string(inventory)?;
86 std::fs::write(path, xml)?;
87 Ok(())
88}
89
90/// Write to string with explicit format.
91pub fn write_to_string<F: StationXmlFormat>(inventory: &Inventory) -> Result<String> {
92 F::write_to_string(inventory)
93}