-- Copyright 2026 Biglup Labs
-- SPDX-License-Identifier: Apache-2.0
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

-- | Validated native-asset names, containing zero through 32 arbitrary bytes.
-- Names identify assets by bytes; UTF-8 is an optional interpretation.
--
-- @
-- import qualified Cometa.AssetName as AssetName
--
-- roundTrip :: AssetName.AssetName -> Bool
-- roundTrip name = AssetName.fromBytes (AssetName.toBytes name) == Right name
-- @
module Cometa.AssetName
  ( AssetName,
    empty,
    fromBytes,
    toBytes,
    fromHex,
    toHex,
    fromText,
    toText,
    encodeCbor,
    decodeCbor,
  )
where

-- IMPORTS --------------------------------------------------------------------

import qualified Codec.CBOR.Decoding as D
import qualified Codec.CBOR.Encoding as E
import Cometa.Error (Error (..))
import qualified Cometa.Internal.Hex as Hex
import qualified Cometa.Internal.TransactionCbor as Cbor
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import Data.Text (Text)
import qualified Data.Text.Encoding as Text

-- TYPES ----------------------------------------------------------------------

-- | Zero through 32 bytes. Equality and ordering compare exact bytes
-- lexicographically. 'Show' is diagnostic output.
newtype AssetName = AssetName ByteString deriving (AssetName -> AssetName -> Bool
(AssetName -> AssetName -> Bool)
-> (AssetName -> AssetName -> Bool) -> Eq AssetName
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AssetName -> AssetName -> Bool
== :: AssetName -> AssetName -> Bool
$c/= :: AssetName -> AssetName -> Bool
/= :: AssetName -> AssetName -> Bool
Eq, Eq AssetName
Eq AssetName =>
(AssetName -> AssetName -> Ordering)
-> (AssetName -> AssetName -> Bool)
-> (AssetName -> AssetName -> Bool)
-> (AssetName -> AssetName -> Bool)
-> (AssetName -> AssetName -> Bool)
-> (AssetName -> AssetName -> AssetName)
-> (AssetName -> AssetName -> AssetName)
-> Ord AssetName
AssetName -> AssetName -> Bool
AssetName -> AssetName -> Ordering
AssetName -> AssetName -> AssetName
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: AssetName -> AssetName -> Ordering
compare :: AssetName -> AssetName -> Ordering
$c< :: AssetName -> AssetName -> Bool
< :: AssetName -> AssetName -> Bool
$c<= :: AssetName -> AssetName -> Bool
<= :: AssetName -> AssetName -> Bool
$c> :: AssetName -> AssetName -> Bool
> :: AssetName -> AssetName -> Bool
$c>= :: AssetName -> AssetName -> Bool
>= :: AssetName -> AssetName -> Bool
$cmax :: AssetName -> AssetName -> AssetName
max :: AssetName -> AssetName -> AssetName
$cmin :: AssetName -> AssetName -> AssetName
min :: AssetName -> AssetName -> AssetName
Ord, Int -> AssetName -> ShowS
[AssetName] -> ShowS
AssetName -> String
(Int -> AssetName -> ShowS)
-> (AssetName -> String)
-> ([AssetName] -> ShowS)
-> Show AssetName
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AssetName -> ShowS
showsPrec :: Int -> AssetName -> ShowS
$cshow :: AssetName -> String
show :: AssetName -> String
$cshowList :: [AssetName] -> ShowS
showList :: [AssetName] -> ShowS
Show)

-- DEFINITIONS ----------------------------------------------------------------

-- | The empty native-asset name, distinct from the Ada asset.
empty :: AssetName
empty :: AssetName
empty = ByteString -> AssetName
AssetName ByteString
ByteString.empty

-- | Import arbitrary bytes, including NUL and invalid UTF-8. More than 32 bytes
-- returns 'InvalidAssetNameLength' with the actual byte count.
fromBytes :: ByteString -> Either Error AssetName
fromBytes :: ByteString -> Either Error AssetName
fromBytes ByteString
bytes
  | ByteString -> Int
ByteString.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
32 = Error -> Either Error AssetName
forall a b. a -> Either a b
Left (Int -> Error
InvalidAssetNameLength (ByteString -> Int
ByteString.length ByteString
bytes))
  | Bool
otherwise = AssetName -> Either Error AssetName
forall a b. b -> Either a b
Right (ByteString -> AssetName
AssetName ByteString
bytes)

-- | Exact name bytes.
toBytes :: AssetName -> ByteString
toBytes :: AssetName -> ByteString
toBytes (AssetName ByteString
bytes) = ByteString
bytes

-- | Import zero through 64 ASCII hexadecimal digits, accepting either case.
-- Odd lengths, prefixes, whitespace and nonhexadecimal characters return
-- 'InvalidHex'; decoded names longer than 32 bytes return 'InvalidAssetNameLength'.
fromHex :: Text -> Either Error AssetName
fromHex :: Text -> Either Error AssetName
fromHex Text
text = Either Error AssetName
-> (ByteString -> Either Error AssetName)
-> Maybe ByteString
-> Either Error AssetName
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Error -> Either Error AssetName
forall a b. a -> Either a b
Left Error
InvalidHex) ByteString -> Either Error AssetName
fromBytes (Text -> Maybe ByteString
Hex.decodeHex Text
text)

-- | Lowercase hexadecimal without a prefix. The empty name renders as empty text.
toHex :: AssetName -> Text
toHex :: AssetName -> Text
toHex = ByteString -> Text
Hex.encodeHex (ByteString -> Text)
-> (AssetName -> ByteString) -> AssetName -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AssetName -> ByteString
toBytes

-- | Encode text as UTF-8, then validate the 32-byte bound. The limit counts
-- encoded bytes, not characters. No Unicode normalization is performed.
fromText :: Text -> Either Error AssetName
fromText :: Text -> Either Error AssetName
fromText = ByteString -> Either Error AssetName
fromBytes (ByteString -> Either Error AssetName)
-> (Text -> ByteString) -> Text -> Either Error AssetName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
Text.encodeUtf8

-- | Interpret exact bytes as UTF-8. Invalid UTF-8 returns 'Nothing'; no bytes
-- are replaced or discarded. An empty name returns @Just ""@.
toText :: AssetName -> Maybe Text
toText :: AssetName -> Maybe Text
toText AssetName
name = (UnicodeException -> Maybe Text)
-> (Text -> Maybe Text)
-> Either UnicodeException Text
-> Maybe Text
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe Text -> UnicodeException -> Maybe Text
forall a b. a -> b -> a
const Maybe Text
forall a. Maybe a
Nothing) Text -> Maybe Text
forall a. a -> Maybe a
Just (ByteString -> Either UnicodeException Text
Text.decodeUtf8' (AssetName -> ByteString
toBytes AssetName
name))

-- | Encode the exact name bytes as a shortest-length definite CBOR byte string.
encodeCbor :: AssetName -> ByteString
encodeCbor :: AssetName -> ByteString
encodeCbor = Encoding -> ByteString
Cbor.encode (Encoding -> ByteString)
-> (AssetName -> Encoding) -> AssetName -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Encoding
E.encodeBytes (ByteString -> Encoding)
-> (AssetName -> ByteString) -> AssetName -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AssetName -> ByteString
toBytes

-- | Decode exactly one CBOR byte string containing 0 through 32 bytes.
-- Nonminimal length encodings are accepted; invalid schemas, lengths or
-- trailing bytes return 'InvalidAssetNameCbor'. Fresh encoding is semantic
-- and does not retain the original CBOR representation.
decodeCbor :: ByteString -> Either Error AssetName
decodeCbor :: ByteString -> Either Error AssetName
decodeCbor = (Text -> Error)
-> (forall s. Decoder s AssetName)
-> ByteString
-> Either Error AssetName
forall a.
(Text -> Error)
-> (forall s. Decoder s a) -> ByteString -> Either Error a
Cbor.decodeAs Text -> Error
InvalidAssetNameCbor (Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes Decoder s ByteString
-> (ByteString -> Decoder s AssetName) -> Decoder s AssetName
forall a b. Decoder s a -> (a -> Decoder s b) -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Error -> Decoder s AssetName)
-> (AssetName -> Decoder s AssetName)
-> Either Error AssetName
-> Decoder s AssetName
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s AssetName
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s AssetName)
-> (Error -> String) -> Error -> Decoder s AssetName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) AssetName -> Decoder s AssetName
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error AssetName -> Decoder s AssetName)
-> (ByteString -> Either Error AssetName)
-> ByteString
-> Decoder s AssetName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Error AssetName
fromBytes)