use json::Json;
use properties::Properties;
use yaml::Yaml;
pub mod json;
pub mod properties;
pub mod yaml;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Failed to get JSON namespace: {0}")]
Json(#[from] json::Error),
#[error("Failed to get YAML namespace: {0}")]
Yaml(#[from] yaml::Error),
#[error("Failed to get text namespace: {0}")]
Text(String),
#[error("Failed to get XML namespace: {0}")]
Xml(String),
}
#[derive(Clone, Debug)]
pub enum Namespace {
Properties(Properties),
Json(Json),
Yaml(Yaml),
Text(String),
}
impl From<Namespace> for wasm_bindgen::JsValue {
fn from(val: Namespace) -> Self {
match val {
Namespace::Properties(properties) => properties.into(),
Namespace::Json(json) => json.into(),
Namespace::Yaml(yaml) => yaml.into(),
Namespace::Text(text) => text.into(),
}
}
}
#[derive(Clone, Debug, PartialEq)]
enum NamespaceType {
Properties,
Json,
Yaml,
Xml,
Text,
}
fn get_namespace_type(namespace: &str) -> NamespaceType {
let parts = namespace.split('.').collect::<Vec<&str>>();
if parts.len() == 1 {
NamespaceType::Properties
} else {
match parts.last().unwrap().to_lowercase().as_str() {
"json" => NamespaceType::Json,
"yaml" | "yml" => NamespaceType::Yaml,
"xml" => NamespaceType::Xml,
_ => NamespaceType::Text,
}
}
}
pub(crate) fn get_namespace(namespace: &str, value: serde_json::Value) -> Result<Namespace, Error> {
match get_namespace_type(namespace) {
NamespaceType::Properties => Ok(Namespace::Properties(properties::Properties::from(value))),
NamespaceType::Json => Ok(Namespace::Json(json::Json::try_from(value)?)),
NamespaceType::Yaml => Ok(Namespace::Yaml(yaml::Yaml::try_from(value)?)),
NamespaceType::Text => {
let text_content = match value.get("content") {
Some(serde_json::Value::String(s)) => s.clone(),
_ => {
return Err(Error::Text(
"Failed to get text content from JSON value".to_string(),
));
}
};
Ok(Namespace::Text(text_content))
}
NamespaceType::Xml => {
Err(Error::Xml("XML format is not yet supported".to_string()))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_namespace_type_properties() {
assert_eq!(get_namespace_type("application"), NamespaceType::Properties);
assert_eq!(get_namespace_type("config"), NamespaceType::Properties);
assert_eq!(get_namespace_type("database"), NamespaceType::Properties);
assert_eq!(
get_namespace_type("app-settings"),
NamespaceType::Properties
);
}
#[test]
fn test_get_namespace_type_json() {
assert_eq!(get_namespace_type("config.json"), NamespaceType::Json);
assert_eq!(get_namespace_type("settings.json"), NamespaceType::Json);
assert_eq!(get_namespace_type("app.config.json"), NamespaceType::Json);
assert_eq!(get_namespace_type("data.JSON"), NamespaceType::Json); }
#[test]
fn test_get_namespace_type_yaml() {
assert_eq!(get_namespace_type("config.yaml"), NamespaceType::Yaml);
assert_eq!(get_namespace_type("settings.yml"), NamespaceType::Yaml);
assert_eq!(get_namespace_type("app.config.yaml"), NamespaceType::Yaml);
assert_eq!(get_namespace_type("data.YAML"), NamespaceType::Yaml); assert_eq!(get_namespace_type("config.YML"), NamespaceType::Yaml); }
#[test]
fn test_get_namespace_type_xml() {
assert_eq!(get_namespace_type("config.xml"), NamespaceType::Xml);
assert_eq!(get_namespace_type("settings.xml"), NamespaceType::Xml);
assert_eq!(get_namespace_type("app.config.xml"), NamespaceType::Xml);
assert_eq!(get_namespace_type("data.XML"), NamespaceType::Xml); }
#[test]
fn test_get_namespace_type_text() {
assert_eq!(get_namespace_type("readme.txt"), NamespaceType::Text);
assert_eq!(get_namespace_type("notes.txt"), NamespaceType::Text);
assert_eq!(get_namespace_type("config.TXT"), NamespaceType::Text); }
#[test]
fn test_get_namespace_type_unsupported_extensions() {
assert_eq!(get_namespace_type("config.ini"), NamespaceType::Text);
assert_eq!(get_namespace_type("settings.cfg"), NamespaceType::Text);
assert_eq!(get_namespace_type("app.properties"), NamespaceType::Text);
assert_eq!(get_namespace_type("data.csv"), NamespaceType::Text);
assert_eq!(get_namespace_type("config.toml"), NamespaceType::Text);
assert_eq!(get_namespace_type("settings.conf"), NamespaceType::Text);
assert_eq!(get_namespace_type("app.unknown"), NamespaceType::Text);
assert_eq!(get_namespace_type("file.xyz"), NamespaceType::Text);
}
#[test]
fn test_get_namespace_type_edge_cases() {
assert_eq!(get_namespace_type(""), NamespaceType::Properties); assert_eq!(get_namespace_type(".json"), NamespaceType::Json); assert_eq!(get_namespace_type("file."), NamespaceType::Text); assert_eq!(get_namespace_type("file..json"), NamespaceType::Json); assert_eq!(
get_namespace_type("config.json.backup"),
NamespaceType::Text
); }
}