From fe38fd6a8859447a154a5698a3e21d9203262be2 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 22 Mar 2019 00:01:30 +0100 Subject: Parse a lot more of the import types --- dhall_core/src/printer.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) (limited to 'dhall_core/src/printer.rs') diff --git a/dhall_core/src/printer.rs b/dhall_core/src/printer.rs index bd38863..7b108d5 100644 --- a/dhall_core/src/printer.rs +++ b/dhall_core/src/printer.rs @@ -1,4 +1,5 @@ use crate::*; +use itertools::Itertools; use std::fmt::{self, Display}; // There used to be a one-to-one correspondence between the formatters in this section @@ -286,8 +287,25 @@ impl Display for Const { impl Display for Import { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + use std::path::PathBuf; use FilePrefix::*; use ImportLocation::*; + use ImportMode::*; + let quoted = |s: &str| -> String { + if s.chars().all(|c| c.is_ascii_alphanumeric()) { + s.to_owned() + } else { + format!("\"{}\"", s) + } + }; + let fmt_path = |f: &mut fmt::Formatter, p: &PathBuf| { + let res: String = p + .iter() + .map(|c| quoted(c.to_string_lossy().as_ref())) + .join("/"); + f.write_str(&res) + }; + match &self.location { Local(prefix, path) => { let prefix = match prefix { @@ -296,9 +314,28 @@ impl Display for Import { Home => "~", Absolute => "", }; - write!(f, "{}/{}", prefix, path.to_str().unwrap()) + write!(f, "{}/", prefix)?; + fmt_path(f, path)?; + } + Remote(url) => { + write!(f, "{}://{}/", url.scheme, url.authority,)?; + fmt_path(f, &url.path)?; + if let Some(q) = &url.query { + write!(f, "?{}", q)? + } } + Env(e) => { + write!(f, "env:{}", quoted(e))?; + } + Missing => { + write!(f, "missing")?; + } + } + match self.mode { + Code => {} + RawText => write!(f, " as Text")?, } + Ok(()) } } @@ -339,6 +376,16 @@ impl Display for Builtin { } } +impl Display for Scheme { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + use crate::Scheme::*; + f.write_str(match *self { + HTTP => "http", + HTTPS => "https", + }) + } +} + impl Display for V { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { let V(ref x, ref n) = *self; -- cgit v1.2.3