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

ambee/giterated

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

Fixed imports!

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨ef0e853

⁨giterated-models/src/instance/mod.rs⁩ - ⁨1499⁩ 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 pub use values::*;
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 {
38 pub url: String,
39 }
40
41 impl GiteratedObject for Instance {
42 fn object_name() -> &'static str {
43 "instance"
44 }
45
46 fn from_object_str(object_str: &str) -> Result<Self, anyhow::Error> {
47 Ok(Instance::from_str(object_str).unwrap())
48 }
49 }
50
51 impl Display for Instance {
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53 f.write_str(&self.url)
54 }
55 }
56
57 impl FromStr for Instance {
58 type Err = InstanceParseError;
59
60 fn from_str(s: &str) -> Result<Self, Self::Err> {
61 Ok(Self { url: s.to_string() })
62 }
63 }
64
65 #[derive(Debug, Error)]
66 pub enum InstanceParseError {
67 #[error("invalid format")]
68 InvalidFormat,
69 }
70