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
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//! Everything related to parsing schemaId

use json_pointer::JsonPointer;
use serde::de::{self, Deserialize, Deserializer, Visitor};
use serde::ser::{Serialize, Serializer};
use url::Url;

use std::fmt;
use std::str::FromStr;

use crate::error::{InvalidFragment, InvalidPath};

/// Either a `Url` or a `JsonPointer`
#[derive(Debug)]
pub enum SchemaId {
    Url(Url),
    Pointer(JsonPointer<String, Vec<String>>),
    Fragment(Fragment),
    Path(Path),
}

#[derive(Debug)]
pub struct Fragment(String);

impl FromStr for Fragment {
    type Err = InvalidFragment;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.chars().nth(0) {
            Some('#') => Ok(Fragment(s[1..].to_owned())),
            _ => Err(InvalidFragment),
        }
    }
}

impl ToString for Fragment {
    fn to_string(&self) -> String {
        format!("#{}", self.0)
    }
}

#[derive(Debug)]
pub struct Path(String);

impl FromStr for Path {
    type Err = InvalidPath;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.chars().any(char::is_whitespace) {
            Err(InvalidPath)
        } else {
            Ok(Path(s.to_string()))
        }
    }
}

impl ToString for Path {
    fn to_string(&self) -> String {
        self.0.clone()
    }
}

impl<'de> Deserialize<'de> for SchemaId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_str(SchemaIdVisitor)
    }
}

struct SchemaIdVisitor;

impl<'de> Visitor<'de> for SchemaIdVisitor {
    type Value = SchemaId;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(
            formatter,
            "a string that is either a Url, JsonPointer or Path"
        )
    }

    fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Fragment::from_str(s)
            .map(SchemaId::Fragment)
            .or_else(|_| Url::parse(s).map(SchemaId::Url))
            .or_else(|_| JsonPointer::from_str(s).map(SchemaId::Pointer))
            .or_else(|_| Path::from_str(s).map(SchemaId::Path))
            .map_err(|_| de::Error::invalid_value(de::Unexpected::Str(s), &self))
    }
}

impl ToString for SchemaId {
    fn to_string(&self) -> String {
        match self {
            Self::Pointer(p) => p.to_string(),
            Self::Url(u) => u.to_string(),
            Self::Fragment(f) => f.to_string(),
            Self::Path(p) => p.to_string(),
        }
    }
}

impl Serialize for SchemaId {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}