use crate::cel::{Key, Value};
use crate::parse::params::string_parts;
use crate::resolve::{Document, DocumentId, Origin, Resolved};
use crate::settings::{DataImport, DataSource};
use crate::tree::StringPart;
use compmd_pack::{FilePath, Place};
use std::fmt::Write as _;
use std::rc::Rc;
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum Target {
File(Origin),
Remote(String),
Host(String),
}
pub(super) fn target(
resolved: &Resolved,
id: DocumentId,
import: &DataImport,
input: &Value,
) -> Result<Target, String> {
let document = resolved.document(id);
let doc = Value::from_json(&serde_json::Value::Object(document.settings.doc.clone()));
let input = if id == resolved.main {
input
} else {
&Value::Null
};
let mut from = String::new();
for part in string_parts(&import.from.value) {
match part {
StringPart::Text(text) => from.push_str(&text.value),
StringPart::Interpolation(interpolation) => {
let value = lookup(&interpolation.path, &doc, input);
let text = value.written().ok().flatten().unwrap_or_default();
from.push_str(&encoded(&text));
}
}
}
match import.source {
DataSource::Host => Ok(Target::Host(
from.strip_prefix("host:").unwrap_or(&from).to_string(),
)),
DataSource::Remote if from.starts_with("https://") => Ok(Target::Remote(from)),
DataSource::Remote => Err(format!("`{from}` isn't an `https:` URL")),
DataSource::Pack => pack_file(document, &from),
DataSource::File => document
.origin
.join(&from)
.map(Target::File)
.map_err(|why| why.to_string()),
}
}
fn pack_file(document: &Document, from: &str) -> Result<Target, String> {
let mut segments = from.splitn(3, '/');
let pack = format!(
"{}/{}",
segments.next().unwrap_or_default(),
segments.next().unwrap_or_default()
);
let path = segments.next().unwrap_or_default();
let release = document
.packs
.iter()
.find_map(|(name, release)| (*name == pack.as_str()).then(|| release.clone()))
.ok_or_else(|| format!("`{pack}` isn't imported, so `{from}` can't be loaded"))?;
let path = FilePath::new(path).map_err(|_| format!("`{from}` leaves its pack"))?;
Ok(Target::File(Origin::Pack(Place::new(release, path))))
}
fn lookup(path: &str, doc: &Value, input: &Value) -> Value {
let mut segments = path.split('.');
let mut value = match (segments.next(), segments.next()) {
(Some("md"), Some("doc")) => doc.clone(),
(Some("md"), Some("input")) => input.clone(),
_ => return Value::Null,
};
for segment in segments {
value = match &value {
Value::Map(fields) => fields
.get(&Key::String(Rc::from(segment)))
.cloned()
.unwrap_or(Value::Null),
_ => Value::Null,
};
}
value
}
pub(super) fn encoded(text: &str) -> String {
let dots_only = !text.is_empty() && text.chars().all(|c| c == '.');
let mut out = String::new();
for byte in text.bytes() {
let kept = byte.is_ascii_alphanumeric()
|| matches!(byte, b'-' | b'_' | b'~')
|| (byte == b'.' && !dots_only);
if kept {
out.push(char::from(byte));
} else {
let _ = write!(out, "%{byte:02X}");
}
}
out
}
pub(super) fn path_of(url: &str) -> &str {
url.split(['?', '#']).next().unwrap_or(url)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn values_stay_in_their_segment() {
assert_eq!(encoded("eu west"), "eu%20west");
assert_eq!(encoded("a/b?c&d#e"), "a%2Fb%3Fc%26d%23e");
assert_eq!(encoded(".."), "%2E%2E");
assert_eq!(encoded("v1.2"), "v1.2");
assert_eq!(encoded("é"), "%C3%A9");
assert_eq!(encoded(""), "");
}
#[test]
fn paths_lose_their_query_and_fragment() {
assert_eq!(path_of("https://a.org/x.csv?y=1"), "https://a.org/x.csv");
assert_eq!(path_of("https://a.org/x.json#top"), "https://a.org/x.json");
}
}