dizhen 0.1.0

Library to retrieve seismic data
Documentation
/*!

For data sources, see [`Source`] and re-exported `*Source` structs.

Time values are [`chrono::DateTime<Utc>`].

```rust
use dizhen::{CENCSource, USGSSource, Source};

let ceic = CENCSource::new();
let usgs = USGSSource::new();
dbg!(
	ceic.get_latest().unwrap().first(),
	usgs.get_latest().unwrap().first()
);
```

 */

use chrono::{DateTime, Utc, FixedOffset};

pub mod sources;
#[doc(inline)]
pub use sources::{Source, SourceId, CENCSource, USGSSource};

type DateTimeUtc = DateTime<Utc>;
type DateTimeF = DateTime<FixedOffset>;

#[derive(Debug)]
pub enum ReportStatus {
	Automatic,
	Reviewed,
	Deleted,
}

/** A reported seismic event.
 */
#[derive(Debug)]
pub struct Event {
	/// Event ID from its source, forms vary.
	pub id: String,
	/// Published URL from its source.
	pub url: String,
	/// ID of its source.
	pub source: SourceId,
	/// Time event occurred.
	pub time: DateTimeUtc,
	/// Last time the source updated this event.
	pub updated: DateTimeUtc,
	pub status: ReportStatus,

	pub longitude: f64,
	pub latitude: f64,
	pub depth: f64,
	pub magnitude: f64,
	pub place: String,
}

impl PartialEq for Event {
	fn eq(&self, other: &Self) -> bool {
		self.id == other.id && self.source == other.source
	}
}

pub type Result<T> = std::result::Result<T, Error>;

#[derive(thiserror::Error, Debug)]
pub enum Error {
	#[error("failed to request data")]
	Request(#[from] Box<ureq::Error>),
	#[error("invalid response")]
	InvalidResponse {
		source: std::io::Error,
	},
	#[error("failed to parse floating number")]
	FloatParsing(#[from] std::num::ParseFloatError),
	#[error("failed to parse a JSON document")]
	Json(#[from] serde_json::Error),
	#[error("required field missing value")]
	MissingValue,
	#[error("unexpected value")]
	UnexpectedValue,

	#[error("failed to parse a time value")]
	ParseTime(#[from] chrono::ParseError),
	/// chrono sometimes returns Option, but we need a Result
	#[error("failed to parse a time value")]
	TimeNone,
}

// reduce size for Error
impl From<ureq::Error> for Error {
	fn from(value: ureq::Error) -> Self {
		Self::Request(Box::new(value))
	}
}