aboutsummaryrefslogtreecommitdiff
path: root/common/src/impl.rs
diff options
context:
space:
mode:
Diffstat (limited to 'common/src/impl.rs')
-rw-r--r--common/src/impl.rs74
1 files changed, 73 insertions, 1 deletions
diff --git a/common/src/impl.rs b/common/src/impl.rs
index 3814b1d..db702d3 100644
--- a/common/src/impl.rs
+++ b/common/src/impl.rs
@@ -4,8 +4,10 @@
Copyright (C) 2025 metamuffin <metamuffin.org>
*/
use crate::{
- Node, NodeID, ObjectIds, PeopleGroup, SourceTrack, SourceTrackKind, TmdbKind, TraktKind,
+ Node, NodeID, NodeIDOrSlug, ObjectIds, PeopleGroup, SourceTrack, SourceTrackKind, TmdbKind,
+ TraktKind,
};
+use hex::FromHexError;
use std::{fmt::Display, str::FromStr};
impl SourceTrackKind {
@@ -170,3 +172,73 @@ impl NodeID {
pub const MIN: NodeID = NodeID([0; 32]);
pub const MAX: NodeID = NodeID([255; 32]);
}
+
+#[cfg(feature = "rocket")]
+impl<'a> rocket::request::FromParam<'a> for NodeID {
+ type Error = FromHexError;
+ fn from_param(param: &'a str) -> Result<Self, Self::Error> {
+ if let Some(id) = param.strip_prefix("+") {
+ let mut k = [0; 32];
+ hex::decode_to_slice(id, &mut k)?;
+ Ok(NodeID(k))
+ } else {
+ Ok(NodeID::from_slug(&param))
+ }
+ }
+}
+#[cfg(feature = "rocket")]
+impl rocket::http::uri::fmt::FromUriParam<rocket::http::uri::fmt::Path, NodeID> for NodeID {
+ type Target = NodeID;
+ fn from_uri_param(param: NodeID) -> Self::Target {
+ param
+ }
+}
+#[cfg(feature = "rocket")]
+impl<'a> rocket::http::uri::fmt::FromUriParam<rocket::http::uri::fmt::Path, &'a String> for NodeID {
+ type Target = &'a str;
+ fn from_uri_param(param: &'a String) -> Self::Target {
+ param.as_str()
+ }
+}
+#[cfg(feature = "rocket")]
+impl<'a> rocket::http::uri::fmt::FromUriParam<rocket::http::uri::fmt::Path, &'a str> for NodeID {
+ type Target = &'a str;
+ fn from_uri_param(param: &'a str) -> Self::Target {
+ param
+ }
+}
+#[cfg(feature = "rocket")]
+impl rocket::http::uri::fmt::UriDisplay<rocket::http::uri::fmt::Path> for NodeID {
+ fn fmt(
+ &self,
+ f: &mut rocket::http::uri::fmt::Formatter<'_, rocket::http::uri::fmt::Path>,
+ ) -> std::fmt::Result {
+ f.write_value(format!("+{}", hex::encode(self.0)))?;
+ Ok(())
+ }
+}
+impl Display for NodeID {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_str("+")?;
+ f.write_str(&hex::encode(self.0))?;
+ Ok(())
+ }
+}
+impl Display for NodeIDOrSlug {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ NodeIDOrSlug::ID(x) => x.fmt(f),
+ NodeIDOrSlug::Slug(x) => x.fmt(f),
+ }
+ }
+}
+impl From<NodeID> for NodeIDOrSlug {
+ fn from(value: NodeID) -> Self {
+ Self::ID(value)
+ }
+}
+impl From<String> for NodeIDOrSlug {
+ fn from(value: String) -> Self {
+ Self::Slug(value)
+ }
+}