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
50
51
52
53
54
use std::str::FromStr;

use bones_utils::LabeledId;
use semver::VersionReq;
use serde::Deserialize;

use crate::prelude::*;

impl FromStr for AssetPackReq {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Some((id, version)) = s.split_once('@') {
            let id = id.parse::<LabeledId>().map_err(|e| e.to_string())?;
            let version = version.parse::<VersionReq>().map_err(|e| e.to_string())?;
            Ok(Self { id, version })
        } else {
            let id = s.parse::<LabeledId>().map_err(|e| e.to_string())?;
            Ok(Self {
                id,
                version: VersionReq::STAR,
            })
        }
    }
}

impl FromStr for SchemaPath {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Some((pack_id, schema)) = s.split_once('/') {
            let pack = AssetPackReq::from_str(pack_id)?;

            Ok(SchemaPath {
                pack: Some(pack),
                name: schema.into(),
            })
        } else {
            Ok(SchemaPath {
                pack: None,
                name: s.into(),
            })
        }
    }
}

impl<'de> Deserialize<'de> for SchemaPath {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde::de::Error;
        let s = String::deserialize(deserializer)?;
        s.parse::<SchemaPath>().map_err(D::Error::custom)
    }
}