shrupl/src/sharry/alias.rs

37 lines
819 B
Rust
Raw Normal View History

2025-05-24 00:22:24 +00:00
use std::fmt::{Debug, Display};
2025-05-23 22:13:17 +00:00
use log::debug;
2025-06-02 23:57:17 +00:00
use serde::{Deserialize, Serialize};
2025-05-24 00:22:24 +00:00
use ureq::RequestBuilder;
2025-05-23 22:13:17 +00:00
2025-05-26 23:56:31 +00:00
use super::api::Uri;
2025-05-23 22:13:17 +00:00
2025-06-02 23:57:17 +00:00
#[derive(Serialize, Deserialize, Debug, Hash)]
2025-05-23 22:13:17 +00:00
pub struct Alias {
2025-06-04 22:01:17 +00:00
pub(super) uri: Uri,
2025-05-23 22:13:17 +00:00
pub(super) id: String,
}
2025-05-24 00:22:24 +00:00
pub(super) trait SharryAlias {
fn sharry_header(self, alias: &Alias) -> Self;
}
2025-05-23 22:13:17 +00:00
2025-05-24 00:22:24 +00:00
impl<B> SharryAlias for RequestBuilder<B> {
fn sharry_header(self, alias: &Alias) -> Self {
self.header("Sharry-Alias", &alias.id)
2025-05-23 22:13:17 +00:00
}
2025-05-24 00:22:24 +00:00
}
2025-05-23 22:13:17 +00:00
2025-05-24 00:22:24 +00:00
impl Alias {
2025-06-04 22:01:17 +00:00
pub fn new(uri: Uri, id: impl Into<String>) -> Self {
Self { uri, id: id.into() }
2025-05-24 00:22:24 +00:00
}
pub(super) fn get_endpoint(&self, endpoint: impl Display + Debug) -> String {
2025-06-04 22:01:17 +00:00
let uri = format!("{}/{}", self.uri.to_string(), endpoint);
2025-05-26 23:56:31 +00:00
debug!("endpoint uri: {uri:?}");
2025-05-23 22:13:17 +00:00
2025-05-24 00:22:24 +00:00
uri
2025-05-23 22:13:17 +00:00
}
}