mod types;
#[cfg(feature = "import")]
mod adapter;
#[cfg(feature = "import")]
mod decode;
#[cfg(feature = "import")]
mod listforge;
#[cfg(feature = "import")]
mod newrecruit_json;
#[cfg(feature = "import")]
mod newrecruit_simple;
#[cfg(feature = "import")]
mod newrecruit_text;
#[cfg(feature = "import")]
mod newrecruit_wtc;
#[cfg(feature = "import")]
mod resolve;
pub use types::{
BattleSize, Candidate, Diagnostics, GameVersionRef, ParsedRoster, ParsedUnit, ParsedWargear,
ResolvedRef, Roster, RosterFormat, RosterLeaderAttachment, RosterPoints, RosterSource,
RosterUnit, RosterWargear, Warning, WarningCode,
};
#[cfg(feature = "import")]
pub use adapter::{format_id, select_adapter, FormatAdapter, ParseError};
#[cfg(feature = "import")]
pub use decode::{decode_listforge, DecodeError};
#[cfg(feature = "import")]
pub use listforge::ListForgeAdapter;
#[cfg(feature = "import")]
pub use newrecruit_json::NewRecruitJsonAdapter;
#[cfg(feature = "import")]
pub use newrecruit_simple::NewRecruitSimpleAdapter;
#[cfg(feature = "import")]
pub use newrecruit_wtc::{NewRecruitWtcCompactAdapter, NewRecruitWtcFullAdapter};
#[cfg(feature = "import")]
pub use resolve::resolve;
#[cfg(feature = "import")]
use crate::data::Dataset;
#[cfg(feature = "import")]
#[derive(Debug)]
pub enum ImportError {
Decode(DecodeError),
Parse(ParseError),
}
#[cfg(feature = "import")]
impl std::fmt::Display for ImportError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ImportError::Decode(e) => write!(f, "{e}"),
ImportError::Parse(e) => write!(f, "{e}"),
}
}
}
#[cfg(feature = "import")]
impl std::error::Error for ImportError {}
#[cfg(feature = "import")]
impl From<DecodeError> for ImportError {
fn from(e: DecodeError) -> Self {
ImportError::Decode(e)
}
}
#[cfg(feature = "import")]
impl From<ParseError> for ImportError {
fn from(e: ParseError) -> Self {
ImportError::Parse(e)
}
}
#[cfg(feature = "import")]
fn adapters() -> Vec<Box<dyn FormatAdapter>> {
vec![
Box::new(NewRecruitJsonAdapter),
Box::new(NewRecruitWtcFullAdapter),
Box::new(NewRecruitWtcCompactAdapter),
Box::new(NewRecruitSimpleAdapter),
Box::new(ListForgeAdapter),
]
}
#[cfg(feature = "import")]
pub fn import_listforge(input: &str, ds: &Dataset) -> Result<Roster, ImportError> {
let decoded = decode_listforge(input)?;
import_roster(&decoded, ds)
}
#[cfg(feature = "import")]
pub fn import_roster(decoded: &serde_json::Value, ds: &Dataset) -> Result<Roster, ImportError> {
let registry = adapters();
let adapter = select_adapter(decoded, ®istry)?;
let parsed = adapter.parse(decoded)?;
Ok(resolve(&parsed, ds, adapter.format()))
}
#[cfg(feature = "import")]
pub fn import_roster_text(input: &str, ds: &Dataset) -> Result<Roster, ImportError> {
let wrapped = serde_json::Value::String(input.to_string());
import_roster(&wrapped, ds)
}