Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
src/nix: make CallOpts::argstr take an OsString
Browse files Browse the repository at this point in the history
nix is content with arbitrary bytestrings, which is nice if you have
e.g. a path that contains non-UTF-8 characters.

So we don’t require a rust `str`, rather an OsStr(ing).

It can still be used with normal strings, because of the `AsRef`
instances.
  • Loading branch information
Profpatsch committed Oct 11, 2019
1 parent c6b4981 commit bbcc370
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
17 changes: 11 additions & 6 deletions src/nix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
use osstrlines;
use serde_json;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use vec1::Vec1;
Expand All @@ -45,7 +45,7 @@ use vec1::Vec1;
pub struct CallOpts {
input: Input,
attribute: Option<String>,
argstrs: HashMap<String, String>,
argstrs: HashMap<OsString, OsString>,
}

/// Which input to give nix.
Expand Down Expand Up @@ -153,8 +153,13 @@ impl CallOpts {
/// output.unwrap(), "Hello, Jill!"
/// );
/// ```
pub fn argstr(&mut self, name: &str, value: &str) -> &mut Self {
self.argstrs.insert(name.to_string(), value.to_string());
pub fn argstr<P, Q>(&mut self, name: P, value: Q) -> &mut Self
where
P: AsRef<OsStr>,
Q: AsRef<OsStr>,
{
self.argstrs
.insert(name.as_ref().to_owned(), value.as_ref().to_owned());
self
}

Expand Down Expand Up @@ -344,8 +349,8 @@ impl CallOpts {

for (name, value) in self.argstrs.iter() {
ret.push(OsStr::new("--argstr"));
ret.push(OsStr::new(name));
ret.push(OsStr::new(value));
ret.push(&name);
ret.push(&value);
}

match self.input {
Expand Down
9 changes: 2 additions & 7 deletions src/ops/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,11 @@ pub fn main(upgrade_target: cli::UpgradeTo, cas: &ContentAddressable) -> OpResul
match src {
UpgradeSource::Branch(b) => {
expr.argstr("type", "branch");
expr.argstr("branch", &b);
expr.argstr("branch", b);
}
UpgradeSource::Local(p) => {
expr.argstr("type", "local");
expr.argstr(
"path",
p.to_str()
// TODO: this is unnecessary, argstr() should take an OsStr()
.expect("Requested Lorri source directory not UTF-8 clean"),
);
expr.argstr("path", p);
}
}
// ugly hack to prevent expr from being mutable outside,
Expand Down

0 comments on commit bbcc370

Please sign in to comment.