Struct mz_sql_parser::ast::Ident
source · pub struct Ident(pub(crate) String);
Expand description
An identifier.
Tuple Fields§
§0: String
Implementations§
source§impl Ident
impl Ident
sourcepub const MAX_LENGTH: usize = 255usize
pub const MAX_LENGTH: usize = 255usize
Maximum length of an identifier in Materialize.
sourcepub fn new<S>(s: S) -> Result<Self, IdentError>
pub fn new<S>(s: S) -> Result<Self, IdentError>
Create a new Ident
with the given value, checking our invariants.
§Examples
use mz_sql_parser::ast::Ident;
let id = Ident::new("hello_world").unwrap();
assert_eq!(id.as_str(), "hello_world");
let too_long = "I am a very long identifier that is more than 255 bytes long which is the max length for idents.\
😊😁😅😂😬🍻😮💨😮🗽🛰️🌈😊😁😅😂😬🍻😮💨😮🗽🛰️🌈😊😁😅😂😬🍻😮💨😮🗽🛰️🌈";
assert_eq!(too_long.len(), 258);
let too_long_id = Ident::new(too_long);
assert!(too_long_id.is_err());
let invalid_name_dot = Ident::new(".");
assert!(invalid_name_dot.is_err());
let invalid_name_dot_dot = Ident::new("..");
assert!(invalid_name_dot_dot.is_err());
sourcepub fn new_lossy<S: Into<String>>(value: S) -> Self
pub fn new_lossy<S: Into<String>>(value: S) -> Self
Create a new Ident
modifying the given value as necessary to meet our invariants.
§Examples
use mz_sql_parser::ast::Ident;
let too_long = "🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢\
🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵\
🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴";
let id = Ident::new_lossy(too_long);
// `new_lossy` will truncate the provided string, since it's too long. Note the missing
// `🔴` characters.
assert_eq!(id.as_str(), "🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵");
sourcepub fn new_unchecked<S: Into<String>>(value: S) -> Self
pub fn new_unchecked<S: Into<String>>(value: S) -> Self
Create a new Ident
without checking any of our invariants.
NOTE: Generally you should not use this function! If you’re trying to create an
Ident
from a &'static str
you know is valid, use the ident!
macro. For all other
use cases, see Ident::new
which correctly checks our invariants.
sourcepub fn try_generate_name<P, S, F, E>(
prefix: P,
suffix: S,
is_valid: F,
) -> Result<Self, E>
pub fn try_generate_name<P, S, F, E>( prefix: P, suffix: S, is_valid: F, ) -> Result<Self, E>
Generate a valid Ident
with the provided prefix
and suffix
.
§Examples
use mz_sql_parser::ast::{Ident, IdentError};
let good_id =
Ident::try_generate_name("hello", "_world", |_| Ok::<_, IdentError>(true)).unwrap();
assert_eq!(good_id.as_str(), "hello_world");
// Return invalid once.
let mut attempts = 0;
let one_failure = Ident::try_generate_name("hello", "_world", |_candidate| {
if attempts == 0 {
attempts += 1;
Ok::<_, IdentError>(false)
} else {
Ok(true)
}
})
.unwrap();
// We "hello_world" was invalid, so we appended "_1".
assert_eq!(one_failure.as_str(), "hello_world_1");
sourcepub fn append_lossy<S: Into<String>>(&mut self, suffix: S)
pub fn append_lossy<S: Into<String>>(&mut self, suffix: S)
Append the provided suffix
, truncating self
as necessary to satisfy our invariants.
Note: We soft-assert that the provided suffix
is not too long, if it is, we’ll
truncate it.
§Examples
use mz_sql_parser::{
ident,
ast::Ident,
};
let mut id = ident!("🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵");
id.append_lossy("🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴");
// We truncated the original ident, removing all '🔵' chars.
assert_eq!(id.as_str(), "🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴");
§Too long suffix
If the provided suffix is too long, we’ll also truncate that.
use mz_sql_parser::{
ident,
ast::Ident,
};
let mut stem = ident!("hello___world");
let too_long_suffix = "\
🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢\
🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢\
🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢\
🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🔵🔵\
";
stem.append_lossy(too_long_suffix);
// Notice the "hello___world" stem got truncated, as did the "🔵🔵" characters from the suffix.
let result = "hello___wor\
🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢\
🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢\
🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢\
🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢\
";
assert_eq!(stem.as_str(), result);
sourcepub fn can_be_printed_bare(&self) -> bool
pub fn can_be_printed_bare(&self) -> bool
An identifier can be printed in bare mode if
- it matches the regex [a-z_][a-z0-9_]* and
- it is not a “reserved keyword.”
pub fn as_str(&self) -> &str
pub fn as_keyword(&self) -> Option<Keyword>
pub fn into_string(self) -> String
Trait Implementations§
source§impl AstDisplay for Ident
impl AstDisplay for Ident
More-or-less a direct translation of the Postgres function for doing the same thing:
https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/ruleutils.c#L10730-L10812
Quotation is forced when printing in Stable mode.
fn fmt<W: Write>(&self, f: &mut AstFormatter<W>)
fn to_ast_string(&self) -> String
fn to_ast_string_stable(&self) -> String
fn to_ast_string_redacted(&self) -> String
source§impl<'de> Deserialize<'de> for Ident
impl<'de> Deserialize<'de> for Ident
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl Ord for Ident
impl Ord for Ident
source§impl PartialOrd for Ident
impl PartialOrd for Ident
source§impl<'ast, T: AstInfo> VisitMutNode<'ast, T> for Ident
impl<'ast, T: AstInfo> VisitMutNode<'ast, T> for Ident
impl Eq for Ident
impl StructuralPartialEq for Ident
Auto Trait Implementations§
impl Freeze for Ident
impl RefUnwindSafe for Ident
impl Send for Ident
impl Sync for Ident
impl Unpin for Ident
impl UnwindSafe for Ident
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<T> FutureExt for T
impl<T> FutureExt for T
source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request