use std::{fmt::Display, str::FromStr}; use serde::{Deserialize, Serialize}; use thiserror::Error; use crate::operation::GiteratedObject; pub struct InstanceMeta { pub url: String, pub public_key: String, } /// An instance, defined by the URL it can be reached at. /// /// # Textual Format /// An instance's textual format is its URL. /// /// ## Examples /// For the instance `giterated.dev`, the following [`Instance`] initialization /// would be valid: /// /// ``` /// let instance = Instance { /// url: String::from("giterated.dev") /// }; /// /// // This is correct /// assert_eq!(Instance::from_str("giterated.dev").unwrap(), instance); /// ``` #[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)] pub struct Instance { pub url: String, } impl GiteratedObject for Instance { fn object_name() -> &'static str { "instance" } fn from_object_str(object_str: &str) -> Result { Ok(Instance::from_str(object_str).unwrap()) } } impl Display for Instance { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(&self.url) } } impl FromStr for Instance { type Err = InstanceParseError; fn from_str(s: &str) -> Result { Ok(Self { url: s.to_string() }) } } #[derive(Debug, Error)] pub enum InstanceParseError { #[error("invalid format")] InvalidFormat, }