summaryrefslogtreecommitdiff
path: root/dhall_syntax/src/printer.rs
diff options
context:
space:
mode:
authorNadrieril2019-05-12 18:44:28 +0200
committerNadrieril2019-05-12 18:44:44 +0200
commitc2b4a2d9b40efbe4f6cb6fd04f6cb90639f4985f (patch)
tree02e0f7b57b56f949240cedbdfabac234d9486834 /dhall_syntax/src/printer.rs
parent2d1a333d6c1e8571ca91d29333c284104153b0ef (diff)
Implement binary encoding
Closes #39
Diffstat (limited to 'dhall_syntax/src/printer.rs')
-rw-r--r--dhall_syntax/src/printer.rs29
1 files changed, 18 insertions, 11 deletions
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)?
}