use crate::error::{Diagnostic, Severity};
use crate::value::QuillValue;
use super::{FileTreeNode, Quill, QuillConfig};
fn diag(message: impl Into<String>, code: &str) -> Diagnostic {
Diagnostic::new(Severity::Error, message.into()).with_code(code.to_string())
}
impl Quill {
pub fn from_tree(root: FileTreeNode) -> Result<Self, Vec<Diagnostic>> {
let quill_yaml_bytes = root.get_file("Quill.yaml").ok_or_else(|| {
vec![diag(
"Quill.yaml not found in file tree",
"quill::missing_file",
)]
})?;
let quill_yaml_content = String::from_utf8(quill_yaml_bytes.to_vec()).map_err(|e| {
vec![diag(
format!("Quill.yaml is not valid UTF-8: {}", e),
"quill::invalid_utf8",
)]
})?;
let (config, _warnings) = QuillConfig::from_yaml_with_warnings(&quill_yaml_content)?;
Self::from_config(config, root)
}
fn from_config(config: QuillConfig, root: FileTreeNode) -> Result<Self, Vec<Diagnostic>> {
let mut metadata: std::collections::HashMap<String, QuillValue> =
std::collections::HashMap::new();
metadata.insert(
"backend".to_string(),
QuillValue::from_json(serde_json::Value::String(config.backend.clone())),
);
metadata.insert(
"description".to_string(),
QuillValue::from_json(serde_json::Value::String(config.description.clone())),
);
metadata.insert(
"author".to_string(),
QuillValue::from_json(serde_json::Value::String(config.author.clone())),
);
metadata.insert(
"version".to_string(),
QuillValue::from_json(serde_json::Value::String(config.version.clone())),
);
for (key, value) in &config.backend_config {
metadata.insert(format!("{}_{}", config.backend, key), value.clone());
}
let plate_content: Option<String> = if let Some(ref plate_file_name) = config.plate_file {
let plate_bytes = root.get_file(plate_file_name).ok_or_else(|| {
vec![diag(
format!("Plate file '{}' not found in file tree", plate_file_name),
"quill::plate_missing",
)]
})?;
let content = String::from_utf8(plate_bytes.to_vec()).map_err(|e| {
vec![diag(
format!("Plate file '{}' is not valid UTF-8: {}", plate_file_name, e),
"quill::invalid_utf8",
)]
})?;
Some(content)
} else {
None
};
let source = Quill {
metadata,
plate: plate_content,
config,
files: root,
};
Ok(source)
}
}