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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use crate::{
error::{Error, ErrorKind},
lockfile::encoding::EncodableDependency,
Checksum, Dependency, Map,
};
use serde::{de, ser, Deserialize, Serialize};
use std::{
convert::{TryFrom, TryInto},
fmt,
str::FromStr,
};
const CHECKSUM_PREFIX: &str = "checksum ";
pub type Metadata = Map<Key, Value>;
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Key(String);
impl Key {
pub fn for_checksum(dep: &Dependency) -> Self {
Key(format!("{}{}", CHECKSUM_PREFIX, dep))
}
pub fn is_checksum(&self) -> bool {
self.0.starts_with(CHECKSUM_PREFIX)
}
pub fn checksum_dependency(&self) -> Result<Dependency, Error> {
self.try_into()
}
}
impl AsRef<str> for Key {
fn as_ref(&self) -> &str {
&self.0
}
}
impl fmt::Display for Key {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl FromStr for Key {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
Ok(Key(s.to_owned()))
}
}
impl TryFrom<&Key> for Dependency {
type Error = Error;
fn try_from(key: &Key) -> Result<Dependency, Error> {
if !key.is_checksum() {
fail!(
ErrorKind::Parse,
"can only parse dependencies from `checksum` metadata"
);
}
let dep = EncodableDependency::from_str(&key.as_ref()[CHECKSUM_PREFIX.len()..])?;
(&dep).try_into()
}
}
impl<'de> Deserialize<'de> for Key {
fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
use de::Error;
let s = String::deserialize(deserializer)?;
s.parse().map_err(D::Error::custom)
}
}
impl Serialize for Key {
fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.to_string().serialize(serializer)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Value(String);
impl Value {
pub fn checksum(&self) -> Result<Checksum, Error> {
self.try_into()
}
}
impl AsRef<str> for Value {
fn as_ref(&self) -> &str {
&self.0
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl FromStr for Value {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
Ok(Value(s.to_owned()))
}
}
impl TryFrom<&Value> for Checksum {
type Error = Error;
fn try_from(value: &Value) -> Result<Checksum, Error> {
value.as_ref().parse()
}
}
impl<'de> Deserialize<'de> for Value {
fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
use de::Error;
let s = String::deserialize(deserializer)?;
s.parse().map_err(D::Error::custom)
}
}
impl Serialize for Value {
fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.to_string().serialize(serializer)
}
}