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

ambee/giterated

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

Unified stack `GetValue` implementation

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨325f5af

⁨giterated-models/src/instance/mod.rs⁩ - ⁨1686⁩ 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
50 impl Display for Instance {
51 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52 f.write_str(&self.0.to_string())
53 }
54 }
55
56 impl FromStr for Instance {
57 type Err = InstanceParseError;
58
59 fn from_str(s: &str) -> Result<Self, Self::Err> {
60 let with_protocol = format!("wss://{}", s);
61
62 if Url::parse(&with_protocol).is_ok() {
63 Ok(Self(s.to_string()))
64 } else {
65 Err(InstanceParseError::InvalidFormat)
66 }
67 }
68 }
69
70 #[derive(Debug, Error)]
71 pub enum InstanceParseError {
72 #[error("invalid format")]
73 InvalidFormat,
74 }
75