1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use std::str;

use crate::{Basic, Decode, Encode, EncodingFormat};
use crate::{SharedData, SimpleDecode};
use crate::{Variant, VariantError};

/// String that identifies the type of an encoded value.
///
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Signature(String);

impl Signature {
    /// Create a new `Signature`.
    pub fn new(signature: impl Into<Signature>) -> Self {
        signature.into()
    }

    /// The signature as a string.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

// FIXME: Find a way to share code with String implementation.
impl Encode for Signature {
    const SIGNATURE_CHAR: char = 'g';
    const SIGNATURE_STR: &'static str = "g";
    const ALIGNMENT: usize = 1;

    // No padding needed because of 1-byte format and hence encoding format is ignored everywhere.

    fn encode_into(&self, bytes: &mut Vec<u8>, _format: EncodingFormat) {
        let len = self.0.len();

        bytes.push(len as u8);
        bytes.extend(self.0.as_bytes());
        bytes.push(b'\0');
    }

    fn to_variant(self) -> Variant {
        Variant::Signature(self)
    }

    fn is(variant: &Variant) -> bool {
        if let Variant::Signature(_) = variant {
            true
        } else {
            false
        }
    }
}

impl Decode for Signature {
    fn slice_data(
        data: impl Into<SharedData>,
        signature: impl Into<Signature>,
        _format: EncodingFormat,
    ) -> Result<SharedData, VariantError> {
        Self::ensure_correct_signature(signature)?;
        let data = data.into();
        if data.len() < 1 {
            return Err(VariantError::InsufficientData);
        }

        let bytes = data.bytes();
        let last_index = bytes[0] as usize + 2;
        crate::ensure_sufficient_bytes(bytes, last_index)?;

        Ok(data.head(last_index))
    }

    fn decode(
        data: impl Into<SharedData>,
        signature: impl Into<Signature>,
        _format: EncodingFormat,
    ) -> Result<Self, VariantError> {
        Self::ensure_correct_signature(signature)?;

        let data = data.into();
        let last_index = data.len() - 1;
        let bytes = data.bytes();
        crate::ensure_sufficient_bytes(bytes, last_index)?;

        str::from_utf8(&bytes[1..last_index])
            .map(Self::new)
            .map_err(|_| VariantError::InvalidUtf8)
    }

    fn take_from_variant(variant: Variant) -> Result<Self, VariantError> {
        if let Variant::Signature(value) = variant {
            Ok(value)
        } else {
            Err(VariantError::IncorrectType)
        }
    }

    fn from_variant(variant: &Variant) -> Result<&Self, VariantError> {
        if let Variant::Signature(value) = variant {
            Ok(value)
        } else {
            Err(VariantError::IncorrectType)
        }
    }
}
impl SimpleDecode for Signature {}
impl Basic for Signature {}

impl From<&str> for Signature {
    fn from(value: &str) -> Self {
        Self(String::from(value))
    }
}

impl From<String> for Signature {
    fn from(value: String) -> Self {
        Self(value)
    }
}

impl std::ops::Deref for Signature {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.as_str()
    }
}

impl PartialEq<&str> for Signature {
    fn eq(&self, other: &&str) -> bool {
        self.as_str() == *other
    }
}