omglol 0.0.1

A wraper for api.omg.lol for your Rust masterpieces.
Documentation
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
// SPDX-LICENSE-IDENTIFIER: MPL-2.0

/*
Strucutures - omglol crate for Rust

Copyright © 2023 Gil Poiares-Oliveira <gil@poiares-oliveira.com>.
All rights reserved.

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 If a copy of the MPL was not distributed with this file,
 You can obtain one at https://mozilla.org/MPL/2.0/.
*/

//! Structures corresponding to various API elements

use serde::{Deserialize, Serialize};
use std::{collections::HashMap, error::Error, fmt, fmt::Display};

// fn return_true() -> bool {
//     // This is the world's more useful function /s
//     // #[serde(default)] needs a function to be supplied.
//     true

/// Response to an account GET request
#[derive(Deserialize, Debug)]
pub struct AccountResponse {
    /// Status message returned by the API
    pub message: String,

    /// Email associated with the account
    pub email: String,

    /// Name associated with the account
    pub name: String, // TODO: Check if this can be null

    /// API key
    pub api_key: String,

    /// Timestamp of account creation
    pub created: TimeStrings,

    /// User-defined account settings
    pub settings: AccountSettings,
}

#[derive(Deserialize, Debug)]
pub struct AccountSettings {
    /// Email address of account owner
    pub owner: String,

    /// Opt-in for email communications
    pub communication: Option<String>, // IMPROVEMENT: Remove "Option" if patched upstream https://github.com/neatnik/omg.lol/issues/613
    /// Opt-in for email communications
    pub date_format: Option<String>, // IMPROVEMENT: Remove "Option" if patched upstream https://github.com/neatnik/omg.lol/issues/613

    /// Which web editor the user chose to use
    pub web_editor: String, // TODO: Check if this can be null, it seems so in the official docs
}

/// Response to a request to determine if an account is verified
#[derive(Deserialize, Debug)]
pub struct Verification {
    /// Status message returned by the API
    pub message: String,

    /// True if the account is verified, false otherwise
    pub verified: bool,
}

/// Address expiration time in different string formats
#[derive(Deserialize, Debug)]
pub struct Expiration {
    /// Status message returned by the API
    pub message: String,

    /// True if the domain has expired, false otherwise
    pub expired: bool,

    /// False if the domain has a lifetime registration, true otherwise
    pub will_expire: Option<bool>,

    /// Unix time
    pub unix_epoch_time: Option<String>,

    /// Time in [ISO 7601](https://www.iso.org/iso-8601-date-and-time-format.html) format
    pub iso_8601_time: Option<String>,

    /// Time in [RFC 2822](https://datatracker.ietf.org/doc/html/rfc2822) format
    pub rfc_2822_time: Option<String>,

    /// Relative time in English
    pub relative_time: Option<String>,
}

/// Address registered with omg.lol
#[derive(Deserialize, Debug)]
pub struct Address {
    /// String of address, without ".omg.lol"
    pub address: String,

    /// Status message returned by the API
    pub message: String,

    /// Time of address registration
    pub registration: TimeStrings,

    /// Time of address expiration
    pub expiration: Expiration,

    /// Verification status of address
    pub verification: Verification,

    /// Email of the address' owner, if it exists
    pub owner: Option<String>,
}

/// Successful DNS records request response
#[derive(Deserialize, Debug)]
pub struct DNSrecords {
    /// Status message returned by the API
    pub message: String,

    /// Vector of DNS records
    pub dns: Vec<DNSrecord>,
}

/// DNS record
#[derive(Deserialize, Debug)]
pub struct DNSrecord {
    /// Record ID
    pub id: i32,

    /// Domain relative to omg.lol zone
    pub name: String,

    /// Entry data/target
    pub data: String,

    /// Record priority, if applicable
    pub priority: Option<i32>,

    /// Record TTL (time to live)
    pub ttl: i32,

    /// Creation timestamp in
    /// [ISO 7601](https://www.iso.org/iso-8601-date-and-time-format.html) format, if applicable
    pub created_at: Option<String>,

    /// Last update timestamp in
    /// [ISO 7601](https://www.iso.org/iso-8601-date-and-time-format.html) format, if applicable
    pub updated_at: Option<String>,

    /// DNS record type
    #[serde(rename = "type")]
    pub record_type: DNStype,
}

/// DNS record types supported by omg.lol DNS routing.
#[derive(Deserialize, Debug)]
pub enum DNStype {
    A,
    AAA,
    CAA,
    CNAME,
    MX,
    NS,
    SRV,
    TXT,
}

impl Display for DNStype {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", &self)
    }
}

/// Forwarding addresses for your @omg.lol email address
#[derive(Deserialize, Debug)]
pub struct ForwardingAddresses {
    /// Status message returned by the API
    pub message: String,
    pub destination_string: String,
    pub destination_array: Vec<String>,
    pub address: String,
    pub email_address: String,
}

/// Struct for when the server returns a String message
#[derive(Deserialize, Debug)]
pub struct MessageResponse {
    /// Status message returned by the API
    pub message: String,
}

/// Response to a REST API request.
#[derive(Deserialize, Debug)]
pub struct RequestResponse<T> {
    pub request: RequestStatus,
    pub response: T,
}

/// Status of a request
#[derive(Deserialize, Debug)]
pub struct RequestStatus {
    /// HTTP status code of the request response
    pub status_code: u16,
    /// `true` if request completed successfully, `false` otherwise.
    pub success: bool,
}

/// String representations of a moment in time according to different protocols.
#[derive(Deserialize, Debug)]
pub struct TimeStrings {
    /// String of seconds elapsed since the UNIX epoch (1970-01-01 00:00:00 UTC)
    pub unix_epoch_time: String,
    /// Time in [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) format.
    pub iso_8601_time: String,
    /// Time in [RFC 2822](https://www.ietf.org/rfc/rfc2822.txt) format.
    pub rfc_2822_time: String,
    /// Relative time string, e.g. "20 minutes ago".
    pub relative_time: String,
    /// Message provided by the API server in response to some requests.
    pub message: Option<String>,
}

/// Response to a query to a Now page endpoint
#[derive(Deserialize, Debug)]
pub struct NowResponse {
    pub message: String,
    pub now: Vec<NowPage>,
}

/// Now page
#[derive(Deserialize, Debug)]
pub struct NowPage {
    pub content: String,
    pub updated: u32,
    pub listed: u8,
    pub nudge: u8,
    pub metadata: String,
}

/// Response to a Now Garden endpoint
#[derive(Deserialize, Debug)]
pub struct NowGardenResponse {
    pub message: String,
    pub garden: Vec<NowGarden>,
}

/// A Now Garden
#[derive(Deserialize, Debug)]
pub struct NowGarden {
    pub address: String,
    pub url: String,
    pub updated: TimeStrings,
}

#[derive(Deserialize, Debug)]
pub struct Token {
    pub access_token: String,
    pub token_type: String,
    pub scope: String,
}

/// Response for a Paste request
#[derive(Deserialize, Debug)]
pub struct PasteResponse {
    /// Status message returned by the API
    pub message: String,
    pub pastebin: Paste,
}

/// Response for a Pastebin request
#[derive(Deserialize, Debug)]
pub struct PastebinResponse {
    /// Status message returned by the API
    pub message: String,
    pub success: Vec<Paste>,
}

/// Pastebin paste
#[derive(Deserialize, Serialize, Debug)]
pub struct Paste {
    pub title: String,
    pub content: String,

    #[serde(skip_serializing)]
    pub modified_on: Option<String>,
}

/// A Paste from [paste.lol](https://paste.lol)
impl Paste {
    /// New instance of `Paste`
    pub fn new(title: String, content: String) -> Paste {
        Paste {
            title,
            content,
            modified_on: None,
        }
    }
}

#[derive(Deserialize, Debug)]
pub struct PurlResponse {
    /// Status message returned by the API
    pub message: String,
    pub purl: Purl,
}

#[derive(Deserialize, Debug)]
pub struct PurlsResponse {
    /// Status message returned by the API
    pub message: String,
    pub purls: Vec<Purl>,
}

#[derive(Deserialize, Debug)]
pub struct Purl {
    pub name: String,
    pub url: String,
    pub counter: Option<i32>,
}

#[derive(Deserialize, Debug)]
pub struct ServiceStatus {
    /// Status message returned by the API
    pub message: String,
    pub members: i32,
    pub addresses: i32,
    pub profiles: i32,
}

pub trait ContentAsJSON {
    fn json_content(&self) -> String {
        panic!("Error converting to JSON content.")
    }
}

impl ContentAsJSON for String {
    fn json_content(&self) -> String {
        format!("{{\"content\": \"{}\"}}", &self)
    }
}

#[derive(Deserialize, Debug)]
pub struct StatuslogResponseArray {
    /// Status message returned by the API
    pub message: String,
    pub id: String,
    pub status: Status,
}

#[derive(Deserialize, Debug)]
pub struct StatuslogUpdateResponse {
    /// Status message returned by the API
    pub message: String,
    pub id: String,
    pub url: String,
}

/// Response struct for a successful statuslog query all statuses request
#[derive(Deserialize, Debug)]
pub struct StatuslogAllStatuses {
    /// Status message returned by the API
    pub message: String,
    pub statuses: Vec<Status>,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct Status {
    /// A status log entry.
    pub emoji: String,
    pub content: String,
    pub external_url: Option<String>,

    #[serde(skip_serializing)]
    pub id: String,

    #[serde(skip_serializing)]
    pub address: String,

    #[serde(skip_serializing)]
    pub created: String,

    #[serde(skip_serializing)]
    pub relative_time: String,
}

impl Status {
    /// Create a new instance of `Status`
    ///
    /// Arguments:
    /// * `emoji`, your status' emoji
    /// * `content`, The text content of your status
    /// * `external_url`, An optional external URL for your status
    pub fn new(emoji: String, content: String, external_url: Option<String>) -> Status {
        Status {
            emoji,
            content,
            external_url,
            id: "".to_string(),
            address: "".to_string(),
            created: "".to_string(),
            relative_time: "".to_string(),
        }
    }
}

/// Your statuslog's bio.
#[derive(Deserialize, Serialize, Debug)]
pub struct StatuslogBio {
    pub bio: String,

    #[serde(skip_serializing)]
    pub css: String,

    #[serde(skip_serializing)]
    pub message: String,
}

impl StatuslogBio {
    /// Create a new `StatuslogBio` struct.
    ///
    /// Arguments:
    /// * `bio: String` the text content of your bio
    pub fn new(bio: String) -> StatuslogBio {
        StatuslogBio {
            bio,
            css: "".to_string(),
            message: "".to_string(),
        }
    }
}

impl ContentAsJSON for StatuslogBio {
    fn json_content(&self) -> String {
        format!("{{\"content\": \"{}\"}}", &self.message)
    }
}

/// Response to a POST request to the statuslog endpoint
#[derive(Deserialize, Debug)]
pub struct StatusPostResponse {
    pub message: String,
    pub id: String,
    pub status: String,
    pub url: String,
    pub external_url: String,
}

/// Profile themes
#[derive(Deserialize, Debug)]
pub struct ProfileThemes {
    pub message: String,
    pub themes: HashMap<String, Theme>,
}

/// omg.lol theme
#[derive(Deserialize, Debug)]
pub struct Theme {
    pub id: String,
    pub name: String,
    pub created: String,
    pub updated: String,
    pub author: String,
    pub author_url: String,
    pub version: String,
    pub license: String,
    pub description: String,
    pub preview_css: String,
    pub sample_profile: String,
}

/// omg.lol webpage
#[derive(Serialize, Deserialize, Debug)]
pub struct Web {
    pub content: String,

    /// Status message provided by the API
    #[serde(skip_serializing)]
    pub message: String,

    /// CSS profile code
    #[serde(skip_serializing)]
    pub css: String,

    /// Additional `<head>` elements
    #[serde(skip_serializing)]
    pub head: String,

    /// Weather the profile has been [verified](https://home.omg.lol/info/profile-verification).
    #[serde(skip_serializing)]
    pub verified: Option<i8>,

    /// Profile picture, `Some` with a URL `String` or `None` if not defined.
    #[serde(skip_serializing)]
    pub pfp: Option<String>,

    #[serde(skip_serializing)]
    pub metadata: String,

    #[serde(skip_serializing)]
    pub branding: String,

    #[serde(rename(deserialize = "type"), skip_serializing)]
    pub page_type: String,
    // Publish currently does not do anything
    //#[serde(skip_deserializing, default = "return_true")]
    //pub publish: bool,
}

impl Web {
    /// Create a new `Web` struct.
    ///
    /// Arguments:
    /// * `content: String` with the web page content.
    pub fn new(content: String) -> Web {
        Web {
            //publish,
            content,
            message: " ".to_string(),
            css: "".to_string(),
            head: "".to_string(),
            verified: None,
            pfp: None,
            metadata: "".to_string(),
            branding: "".to_string(),
            page_type: "".to_string(),
        }
    }
}

/// Response to a request for weblog entries
#[derive(Deserialize, Debug)]
pub struct WeblogEntriesResponse {
    /// Status message provided by the API
    pub message: String,

    /// A vector of `WeblogEntry` structs.
    pub entries: Vec<WeblogEntry>,
}

/// API response for a weblog entry GET request.
#[derive(Deserialize, Debug)]
pub struct WeblogEntryResponse {
    /// Status message povided by the API
    pub message: String,

    /// Weblog entry
    #[serde(alias = "post")]
    pub entry: WeblogEntry,
}

/// A weblog entry
#[derive(Deserialize, Debug)]
pub struct WeblogEntry {
    pub location: String,
    pub title: String,
    pub date: u32,
    pub status: String,
    pub body: String,
    pub source: String,
    pub metadata: WeblogMetadata,
    pub output: String,
    pub entry: String,

    #[serde(rename(deserialize = "type"))]
    entry_type: String,
}

/// Metadata for a weblog entry.
#[derive(Deserialize, Debug)]
pub struct WeblogMetadata {
    pub date: String,
    pub slug: String,
}

/// API response for a weblog configuration GET request.
#[derive(Deserialize, Debug)]
pub struct WeblogConfigurationResponse {
    pub message: String,
    pub configuration: WeblogConfigurationFormats,
}

/// Weblog configuration in different formats
#[derive(Deserialize, Debug)]
pub struct WeblogConfigurationFormats {
    pub object: WeblogConfiguration,
    pub json: String,
    pub raw: String,
}

/// A weblog's configuration.
#[derive(Deserialize, Debug)]
pub struct WeblogConfiguration {
    pub weblog_title: String,
    pub weblog_description: String,
    pub author: String,
    pub separator: String,
    pub tag_path: String,
    pub timezone: String,
    pub date_format: String,
    pub default_post: String,
    pub feed_post_count: String,
    pub recents_posts_format: String,
    pub post_list_format: String,
    pub search_satus: String,
    pub search_status_success_message: String,
    pub search_results_failure_message: String,
    pub search_results_format: String,
}

/// API response for a weblog template GET request.
#[derive(Deserialize, Debug)]
pub struct WeblogTemplateResponse {
    pub message: String,
    pub template: String,
}

/// An HTTP request error
#[derive(Debug, Clone)]
pub struct RequestError {
    /// HTTP status code as defined in [RFC 9110](https://httpwg.org/specs/rfc9110.html#overview.of.status.codes).
    pub status_code: u16,
}

impl Error for RequestError {}

impl fmt::Display for RequestError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Request error. HTTP status code: {}.", &self.status_code)
    }
}