JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

More progress :)

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨92c3f32

⁨giterated-models/src/instance/mod.rs⁩ - ⁨1732⁩ bytes
Raw
1 use std::{fmt::Display, str::FromStr};
2
3 use serde::{Deserialize, Serialize};
4 use thiserror::Error;
5
6 mod operations;
7 mod values;
8
9 pub use operations::*;
10 use url::Url;
11
12 use crate::object::GiteratedObject;
13
14 pub struct InstanceMeta {
15 pub url: String,
16 pub public_key: String,
17 }
18
19 /// An instance, defined by the URL it can be reached at.
20 ///
21 /// # Textual Format
22 /// An instance's textual format is its URL.
23 ///
24 /// ## Examples
25 /// For the instance `giterated.dev`, the following [`Instance`] initialization
26 /// would be valid:
27 ///
28 /// ```
29 /// let instance = Instance {
30 /// url: String::from("giterated.dev")
31 /// };
32 ///
33 /// // This is correct
34 /// assert_eq!(Instance::from_str("giterated.dev").unwrap(), instance);
35 /// ```
36 #[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
37 pub struct Instance(pub String);
38
39 impl GiteratedObject for Instance {
40 fn object_name() -> &'static str {
41 "instance"
42 }
43
44 fn from_object_str(object_str: &str) -> Result<Self, anyhow::Error> {
45 Ok(Instance::from_str(object_str).unwrap())
46 }
47
48 fn home_uri(&self) -> String {
49 self.0.clone()
50 }
51 }
52
53 impl Display for Instance {
54 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55 f.write_str(&self.0.to_string())
56 }
57 }
58
59 impl FromStr for Instance {
60 type Err = InstanceParseError;
61
62 fn from_str(s: &str) -> Result<Self, Self::Err> {
63 let with_protocol = format!("wss://{}", s);
64
65 if Url::parse(&with_protocol).is_ok() {
66 Ok(Self(s.to_string()))
67 } else {
68 Err(InstanceParseError::InvalidFormat)
69 }
70 }
71 }
72
73 #[derive(Debug, Error)]
74 pub enum InstanceParseError {
75 #[error("invalid format")]
76 InvalidFormat,
77 }
78