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

ambee/giterated

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

Progress on refactor

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨c9f076f

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