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

ambee/giterated

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

The long awaited, exhalted huge networking stack change.

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨21b6a72

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