1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! A thin crate for people who just want to use ldtk files freely.
//!
//!
//! # Usage
//!
//! ```rust
//! use std::error::Error;
//!
//! fn main() -> Result<(), Box<dyn Error>> {
//!   use ldtk2::Ldtk;
//!
//!   let map = Ldtk::from_file("tests/example.ldtk")?;
//!   // or
//!   let map = Ldtk::from_str(include_str!("../tests/example.ldtk"))?;
//!
//!   Ok(())
//! }
//! ```
//!
//!
//! # Why did I create this nonsense?
//!
//! - [LDtk-rs](https://github.com/katharostech/LDtk-rs) uses code generation, it does not get autocomplete support from rust-analyzer. Also, there are special license restrictions on using that crate.
//! - [ldtk_rust](https://github.com/estivate/ldtk_rust) uses `.except()` inside the crate, you can't handle the error.

mod ldtk;
pub use ldtk::*;
use std::{error::Error, path::Path};
pub type Ldtk = Coordinate;

// #[test]
// fn test() {
//   use ldtk2::Ldtk;
//   let map = Ldtk::from_file("example.ldtk")?;
//   // or
//   let map = Ldtk::from_str(include_str!("example.ldtk"))?;
// }

impl Ldtk {
  pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn Error>> {
    let value: Self = serde_json::from_str(&std::fs::read_to_string(path)?)?;
    Ok(value)
  }

  pub fn from_str<S: AsRef<str>>(s: S) -> Result<Self, Box<dyn Error>> {
    let value: Self = serde_json::from_str(s.as_ref())?;
    Ok(value)
  }
}