1use std::path::Path;
9use std::sync::Arc;
10
11use crate::JsonCodec;
12#[cfg(feature = "toml")]
13use crate::TomlCodec;
14use crate::codec::Codec;
15
16#[must_use]
19pub fn codec_for_name(name: &str) -> Option<Arc<dyn Codec>> {
20 match name.to_ascii_lowercase().as_str() {
21 #[cfg(feature = "toml")]
22 "toml" => Some(Arc::new(TomlCodec)),
23 "json" => Some(Arc::new(JsonCodec::default())),
24 _ => None,
25 }
26}
27
28#[must_use]
30pub fn codec_for_path(path: &Path) -> Option<Arc<dyn Codec>> {
31 let ext = path.extension().and_then(|ext| ext.to_str())?;
32 codec_for_name(ext)
33}
34
35#[cfg(test)]
36mod tests {
37 use super::*;
38
39 #[test]
40 fn selects_json_by_name_and_path() {
41 assert_eq!(codec_for_name("json").unwrap().name(), "json");
42 assert_eq!(codec_for_name("JSON").unwrap().name(), "json");
43 assert_eq!(
44 codec_for_path(Path::new("/etc/app/config.json"))
45 .unwrap()
46 .name(),
47 "json"
48 );
49 }
50
51 #[cfg(feature = "toml")]
52 #[test]
53 fn selects_toml_by_name_and_path() {
54 assert_eq!(codec_for_name("toml").unwrap().name(), "toml");
55 assert_eq!(
56 codec_for_path(Path::new("config.toml")).unwrap().name(),
57 "toml"
58 );
59 }
60
61 #[test]
62 fn returns_none_for_unknown_or_missing_extension() {
63 assert!(codec_for_name("yaml").is_none());
64 assert!(codec_for_path(Path::new("config")).is_none());
65 }
66}