knowdit_repo_model/
lang.rs1use serde::{Deserialize, Serialize};
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Hash)]
24pub enum SourceLanguage {
25 Solidity,
26 Move,
27}
28
29impl SourceLanguage {
30 pub fn display_name(self) -> &'static str {
31 match self {
32 Self::Solidity => "Solidity",
33 Self::Move => "Move",
34 }
35 }
36
37 pub fn extension(self) -> &'static str {
38 match self {
39 Self::Solidity => "sol",
40 Self::Move => "move",
41 }
42 }
43
44 pub fn code_fence(self) -> &'static str {
45 match self {
46 Self::Solidity => "solidity",
47 Self::Move => "move",
48 }
49 }
50
51 pub fn parse_lenient(s: &str) -> Option<Self> {
58 match s.trim().to_ascii_lowercase().as_str() {
59 "solidity" | "sol" => Some(Self::Solidity),
60 "move" | "sui move" | "sui_move" => Some(Self::Move),
61 _ => None,
62 }
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn parse_lenient_known_spellings() {
72 for s in ["Solidity", "solidity", "SOLIDITY", "sol", " Solidity "] {
73 assert_eq!(
74 SourceLanguage::parse_lenient(s),
75 Some(SourceLanguage::Solidity),
76 "input={s:?}"
77 );
78 }
79 for s in ["Move", "move", "MOVE", "Sui Move", "sui_move"] {
80 assert_eq!(
81 SourceLanguage::parse_lenient(s),
82 Some(SourceLanguage::Move),
83 "input={s:?}"
84 );
85 }
86 }
87
88 #[test]
89 fn parse_lenient_rejects_unknown() {
90 for s in ["", "rust", "Solana", "vyper", "Solidity!", "Move 2", "??"] {
91 assert!(
92 SourceLanguage::parse_lenient(s).is_none(),
93 "should reject {s:?}"
94 );
95 }
96 }
97
98 #[test]
99 fn json_roundtrip() {
100 for lang in [SourceLanguage::Solidity, SourceLanguage::Move] {
101 let s = serde_json::to_string(&lang).unwrap();
102 let back: SourceLanguage = serde_json::from_str(&s).unwrap();
103 assert_eq!(back, lang);
104 }
105 }
106}