pgevolve_core/ir/
collation.rs1use serde::{Deserialize, Serialize};
8
9use crate::identifier::{Identifier, QualifiedName};
10
11#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
13pub struct Collation {
14 pub qname: QualifiedName,
16 pub provider: CollationProvider,
18 pub lc_collate: String,
20 pub lc_ctype: String,
22 pub deterministic: bool,
24 pub version: Option<String>,
28 pub owner: Option<Identifier>,
30 pub comment: Option<String>,
32}
33
34#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
37#[serde(rename_all = "snake_case")]
38pub enum CollationProvider {
39 Libc,
41 Icu,
43 Builtin,
45}
46
47pub const BUILTIN_COLLATIONS: &[&str] =
50 &["default", "C", "POSIX", "und-x-icu", "unicode", "ucs_basic"];
51
52impl CollationProvider {
53 #[must_use]
55 pub const fn sql_keyword(self) -> &'static str {
56 match self {
57 Self::Libc => "libc",
58 Self::Icu => "icu",
59 Self::Builtin => "builtin",
60 }
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 fn id(s: &str) -> Identifier {
69 Identifier::from_unquoted(s).unwrap()
70 }
71 fn qn(s: &str, n: &str) -> QualifiedName {
72 QualifiedName::new(id(s), id(n))
73 }
74
75 #[test]
76 fn collation_serde_round_trip() {
77 let c = Collation {
78 qname: qn("app", "case_insensitive"),
79 provider: CollationProvider::Icu,
80 lc_collate: "und".into(),
81 lc_ctype: "und".into(),
82 deterministic: false,
83 version: None,
84 owner: None,
85 comment: Some("CI collation".into()),
86 };
87 let json = serde_json::to_string(&c).unwrap();
88 let back: Collation = serde_json::from_str(&json).unwrap();
89 assert_eq!(c, back);
90 }
91
92 #[test]
93 fn provider_sql_keywords() {
94 assert_eq!(CollationProvider::Libc.sql_keyword(), "libc");
95 assert_eq!(CollationProvider::Icu.sql_keyword(), "icu");
96 assert_eq!(CollationProvider::Builtin.sql_keyword(), "builtin");
97 }
98}