hcl/ident.rs
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
use crate::expr::Variable;
use crate::{Error, InternalString, Result};
use hcl_primitives::Ident;
use serde::{Deserialize, Serialize};
use std::borrow::{Borrow, Cow};
use std::fmt;
use std::ops;
// @NOTE(mohmann): Eventually, the `Identifier` type should be entirely replaced with
// `hcl_primitives::Ident`, but this is a breaking change because of various intentional API
// changes.
/// Represents an HCL identifier.
#[derive(Deserialize, Serialize, Clone, PartialEq, Eq, Hash)]
#[serde(transparent)]
pub struct Identifier(Ident);
impl Identifier {
/// Create a new `Identifier` after validating that it only contains characters that are
/// allowed in HCL identifiers.
///
/// See [`Identifier::sanitized`][Identifier::sanitized] for an infallible alternative to this
/// function.
///
/// # Example
///
/// ```
/// # use hcl::Identifier;
/// assert!(Identifier::new("some_ident").is_ok());
/// assert!(Identifier::new("").is_err());
/// assert!(Identifier::new("1two3").is_err());
/// assert!(Identifier::new("with whitespace").is_err());
/// ```
///
/// # Errors
///
/// If `ident` contains characters that are not allowed in HCL identifiers or if it is empty an
/// error will be returned.
pub fn new<T>(ident: T) -> Result<Self>
where
T: Into<InternalString>,
{
Ident::try_new(ident).map(Identifier).map_err(Error::new)
}
/// Create a new `Identifier` after sanitizing the input if necessary.
///
/// If `ident` contains characters that are not allowed in HCL identifiers will be sanitized
/// according to the following rules:
///
/// - An empty `ident` results in an identifier containing a single underscore.
/// - Invalid characters in `ident` will be replaced with underscores.
/// - If `ident` starts with a character that is invalid in the first position but would be
/// valid in the rest of an HCL identifier it is prefixed with an underscore.
///
/// See [`Identifier::new`][Identifier::new] for a fallible alternative to this function if
/// you prefer rejecting invalid identifiers instead of sanitizing them.
///
/// # Example
///
/// ```
/// # use hcl::Identifier;
/// assert_eq!(Identifier::sanitized("some_ident").as_str(), "some_ident");
/// assert_eq!(Identifier::sanitized("").as_str(), "_");
/// assert_eq!(Identifier::sanitized("1two3").as_str(), "_1two3");
/// assert_eq!(Identifier::sanitized("with whitespace").as_str(), "with_whitespace");
/// ```
pub fn sanitized<T>(ident: T) -> Self
where
T: AsRef<str>,
{
Identifier(Ident::new_sanitized(ident))
}
/// Create a new `Identifier` without checking if it is valid.
///
/// It is the caller's responsibility to ensure that the identifier is valid.
///
/// For most use cases [`Identifier::new`][Identifier::new] or
/// [`Identifier::sanitized`][Identifier::sanitized] should be preferred.
///
/// # Safety
///
/// This function is not marked as unsafe because it does not cause undefined behaviour.
/// However, attempting to serialize an invalid identifier to HCL will produce invalid output.
pub fn unchecked<T>(ident: T) -> Self
where
T: Into<InternalString>,
{
Identifier(Ident::new_unchecked(ident))
}
/// Consume `self` and return the wrapped `String`.
pub fn into_inner(self) -> String {
self.0.into_string()
}
/// Return a reference to the wrapped `str`.
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl From<Ident> for Identifier {
fn from(ident: Ident) -> Self {
Identifier(ident)
}
}
impl From<Identifier> for Ident {
fn from(ident: Identifier) -> Self {
ident.0
}
}
impl From<String> for Identifier {
fn from(s: String) -> Self {
Identifier::sanitized(s)
}
}
impl From<&str> for Identifier {
fn from(s: &str) -> Self {
Identifier::sanitized(s)
}
}
impl<'a> From<Cow<'a, str>> for Identifier {
fn from(s: Cow<'a, str>) -> Self {
Identifier::sanitized(s)
}
}
impl From<Variable> for Identifier {
fn from(variable: Variable) -> Self {
variable.into_inner()
}
}
impl fmt::Debug for Identifier {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Identifier({self})")
}
}
impl fmt::Display for Identifier {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self)
}
}
impl ops::Deref for Identifier {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl AsRef<str> for Identifier {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Borrow<str> for Identifier {
fn borrow(&self) -> &str {
self.as_str()
}
}