From c2b4a2d9b40efbe4f6cb6fd04f6cb90639f4985f Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 12 May 2019 18:44:28 +0200 Subject: Implement binary encoding Closes #39 --- dhall_syntax/src/printer.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'dhall_syntax/src/printer.rs') diff --git a/dhall_syntax/src/printer.rs b/dhall_syntax/src/printer.rs index 0162693..f1ce230 100644 --- a/dhall_syntax/src/printer.rs +++ b/dhall_syntax/src/printer.rs @@ -353,23 +353,21 @@ impl Display for Hash { } impl Display for ImportHashed { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - use std::path::PathBuf; use FilePrefix::*; use ImportLocation::*; - let quoted_path_component = |s: &str| -> String { + let fmt_remote_path_component = |s: &str| -> String { + use percent_encoding::{ + utf8_percent_encode, PATH_SEGMENT_ENCODE_SET, + }; + utf8_percent_encode(s, PATH_SEGMENT_ENCODE_SET).to_string() + }; + let fmt_local_path_component = |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_path_component(c.to_string_lossy().as_ref())) - .join("/"); - f.write_str(&res) - }; match &self.location { Local(prefix, path) => { @@ -380,11 +378,20 @@ impl Display for ImportHashed { Absolute => "", }; write!(f, "{}/", prefix)?; - fmt_path(f, path)?; + let path: String = path + .iter() + .map(|c| fmt_local_path_component(c.as_ref())) + .join("/"); + f.write_str(&path)?; } Remote(url) => { write!(f, "{}://{}/", url.scheme, url.authority,)?; - fmt_path(f, &url.path)?; + let path: String = url + .path + .iter() + .map(|c| fmt_remote_path_component(c.as_ref())) + .join("/"); + f.write_str(&path)?; if let Some(q) = &url.query { write!(f, "?{}", q)? } -- cgit v1.2.3