-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlib.rs
161 lines (139 loc) · 3.58 KB
/
lib.rs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::fmt::Display;
use chrono::{DateTime, NaiveDateTime};
use indexmap::IndexMap;
use locale::Locale;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::fm_types::PageType;
pub mod error;
pub mod fm_types;
pub mod globals;
pub mod locale;
pub mod settings;
#[derive(Clone, Debug, Error)]
pub enum ArgError {
#[error("must be a string")]
MustBeString,
#[error("must be an integer")]
MustBeInt,
#[error("must be a boolean")]
MustBeBool,
#[error("must be provided")]
MustBeProvided,
}
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
pub enum Quotes {
Double,
Single,
Back,
}
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
pub enum Arg {
String(String, Quotes),
Int(i64),
Float(f64),
Bool(bool),
}
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
pub struct AnyArg {
pub value: Arg,
}
impl AnyArg {
pub fn as_int(&self) -> i64 {
match &self.value {
Arg::String(s, _) => s
.trim_matches(|c: char| !c.is_numeric())
.parse()
.unwrap_or(0),
Arg::Int(n) => *n,
Arg::Float(f) => *f as i64,
Arg::Bool(true) => 1,
Arg::Bool(false) => 0,
}
}
pub fn as_bool(&self) -> bool {
match &self.value {
Arg::String(s, _) => !s.is_empty(),
Arg::Int(n) => *n != 0,
Arg::Float(f) => *f != 0f64 && !f.is_nan(),
Arg::Bool(b) => *b,
}
}
}
impl Display for AnyArg {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.value {
Arg::String(s, _) => f.write_str(s),
Arg::Int(n) => f.write_fmt(format_args!("{}", n)),
Arg::Float(n) => f.write_fmt(format_args!("{}", n)),
Arg::Bool(b) => f.write_fmt(format_args!("{}", b)),
}
}
}
impl TryFrom<Arg> for AnyArg {
type Error = ArgError;
fn try_from(value: Arg) -> Result<Self, Self::Error> {
Ok(AnyArg { value })
}
}
impl TryInto<String> for Arg {
type Error = ArgError;
fn try_into(self) -> Result<String, Self::Error> {
if let Arg::String(s, _) = self {
Ok(s)
} else {
Err(ArgError::MustBeString)
}
}
}
impl TryInto<i64> for Arg {
type Error = ArgError;
fn try_into(self) -> Result<i64, Self::Error> {
if let Arg::Int(i) = self {
Ok(i)
} else {
Err(ArgError::MustBeInt)
}
}
}
impl TryInto<bool> for Arg {
type Error = ArgError;
fn try_into(self) -> Result<bool, Self::Error> {
if let Arg::Bool(b) = self {
Ok(b)
} else {
Err(ArgError::MustBeBool)
}
}
}
#[derive(Clone, Debug, Default)]
pub struct RariEnv<'a> {
pub url: &'a str,
pub locale: Locale,
pub title: &'a str,
pub tags: &'a [String],
pub browser_compat: &'a [String],
pub spec_urls: &'a [String],
pub page_type: PageType,
pub slug: &'a str,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct HistoryEntry {
pub modified: NaiveDateTime,
pub hash: String,
}
impl HistoryEntry {
pub fn new(date: &str, hash: &str) -> Self {
Self {
modified: DateTime::parse_from_rfc3339(date)
.unwrap_or_default()
.naive_utc(),
hash: hash.to_string(),
}
}
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Popularities {
pub popularities: IndexMap<String, f64>,
pub date: NaiveDateTime,
}