/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/

use crate::helper::A;
use jellycommon::Asset;
use rocket::{
    http::uri::fmt::{FromUriParam, Path, UriDisplay},
    request::{FromParam, FromSegments},
};
use std::fmt::Write;

impl<'a> FromParam<'a> for A<Asset> {
    type Error = ();
    fn from_param(param: &'a str) -> Result<Self, Self::Error> {
        Ok(A(Asset(param.to_owned())))
    }
}
impl UriDisplay<Path> for A<Asset> {
    fn fmt(&self, f: &mut rocket::http::uri::fmt::Formatter<'_, Path>) -> std::fmt::Result {
        write!(f, "{}", self.0 .0)
    }
}
impl FromUriParam<Path, Asset> for A<Asset> {
    type Target = A<Asset>;
    fn from_uri_param(param: Asset) -> Self::Target {
        A(param)
    }
}
impl<'r> FromSegments<'r> for A<Asset> {
    type Error = ();
    fn from_segments(segments: rocket::http::uri::Segments<'r, Path>) -> Result<Self, Self::Error> {
        Ok(A(Asset(segments.collect::<Vec<_>>().join("/"))))
    }
}
