mod ldtk;
pub use ldtk::*;
pub use serde_json;
pub type Ldtk = LdtkJson;
use std::{convert::TryFrom, path::Path};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
SerdeJson(#[from] serde_json::Error),
}
impl Ldtk {
pub fn from_path(path: impl AsRef<Path>) -> Result<Self, Error> {
Ok(serde_json::from_str(
&std::fs::read_to_string(path)
.map_err(|e| Error::Io(e))
?,
)?)
}
pub fn from_str(s: impl AsRef<str>) -> Result<Self, serde_json::Error> {
Ok(serde_json::from_str(s.as_ref())?)
}
}
impl TryFrom<&Path> for Ldtk {
type Error = Error;
fn try_from(path: &Path) -> Result<Self, Self::Error> {
Ldtk::from_path(path)
}
}
impl TryFrom<&str> for Ldtk {
type Error = serde_json::Error;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Ldtk::from_str(s)
}
}