|
| 1 | +use gitql_core::signature::Function; |
| 2 | +use gitql_core::signature::Signature; |
| 3 | +use gitql_core::types::DataType; |
| 4 | +use gitql_core::value::Value; |
| 5 | +use gitql_std::function::standard_function_signatures; |
| 6 | +use gitql_std::function::standard_functions; |
| 7 | +use std::collections::HashMap; |
| 8 | +use std::sync::OnceLock; |
| 9 | + |
| 10 | +pub fn fileql_std_functions() -> &'static HashMap<&'static str, Function> { |
| 11 | + static HASHMAP: OnceLock<HashMap<&'static str, Function>> = OnceLock::new(); |
| 12 | + HASHMAP.get_or_init(|| { |
| 13 | + let mut map: HashMap<&'static str, Function> = |
| 14 | + HashMap::from(standard_functions().to_owned()); |
| 15 | + map.insert("files_count", files_count); |
| 16 | + map |
| 17 | + }) |
| 18 | +} |
| 19 | + |
| 20 | +pub fn fileql_std_signatures() -> &'static HashMap<&'static str, Signature> { |
| 21 | + static HASHMAP: OnceLock<HashMap<&'static str, Signature>> = OnceLock::new(); |
| 22 | + HASHMAP.get_or_init(|| { |
| 23 | + let mut map: HashMap<&'static str, Signature> = |
| 24 | + HashMap::from(standard_function_signatures().to_owned()); |
| 25 | + map.insert( |
| 26 | + "files_count", |
| 27 | + Signature { |
| 28 | + parameters: vec![DataType::Text], |
| 29 | + return_type: DataType::Integer, |
| 30 | + }, |
| 31 | + ); |
| 32 | + map |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +fn files_count(values: &[Value]) -> Value { |
| 37 | + let path = values[0].as_text(); |
| 38 | + if let Ok(entries) = std::fs::read_dir(path) { |
| 39 | + let count = entries.flatten().count(); |
| 40 | + return Value::Integer(count as i64); |
| 41 | + } |
| 42 | + Value::Integer(0) |
| 43 | +} |
0 commit comments