Skip to content

Commit ffea345

Browse files
committed
Cleanup
1 parent eb4322b commit ffea345

1 file changed

Lines changed: 16 additions & 46 deletions

File tree

src/bin/tectonic/v2cli.rs

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -766,20 +766,18 @@ impl ShowUserCacheDirCommand {
766766
#[derive(Debug, Eq, PartialEq, StructOpt)]
767767
struct ShowMetadataCommand {
768768
#[structopt(help = "The metadata key to get")]
769-
key: Option<String>,
769+
key: String,
770770
}
771771

772772

773-
// If parts is none, print this value as a string.
774-
// This is used to recursively stringify arrays and tables.
775-
fn get_metadata(meta: &toml::Value, mut parts: Option<core::str::Split<&str>>) -> Option<String> {
773+
fn get_metadata(meta: &toml::Value, mut parts: core::str::Split<&str>) -> Option<String> {
776774
match meta {
777775
toml::Value::String(_)
778776
| toml::Value::Boolean(_)
779777
| toml::Value::Integer(_)
780778
| toml::Value::Float(_)
781779
| toml::Value::Datetime(_) => {
782-
if parts.is_some() && parts.unwrap().next().is_some() {
780+
if parts.next().is_some() {
783781
// None of these types may be indexed further
784782
return None;
785783
}
@@ -792,27 +790,12 @@ fn get_metadata(meta: &toml::Value, mut parts: Option<core::str::Split<&str>>) -
792790
}
793791

794792
toml::Value::Array(a) => {
795-
let idx = {
796-
if parts.is_some() {
797-
parts.as_mut().unwrap().next()
798-
} else {
799-
None
800-
}
801-
};
802-
803-
match idx {
804-
None => {
805-
// Print the whole array
806-
let s = a
807-
.iter()
808-
.map(|x| get_metadata(x, None).unwrap())
809-
.collect::<Vec<String>>()
810-
.join(", ");
811-
return Some(format!("[{}]", s));
812-
}
793+
match parts.next() {
794+
None => return None,
813795
Some(s) => {
814796
if s == "len" {
815-
// Magic key to get array length
797+
// Magic key to get array length.
798+
// Allows easy iteration.
816799
return Some(format!("{}", a.len()));
817800
} else {
818801
let i: usize = match s.parse() {
@@ -832,15 +815,7 @@ fn get_metadata(meta: &toml::Value, mut parts: Option<core::str::Split<&str>>) -
832815
}
833816

834817
toml::Value::Table(t) => {
835-
let idx = {
836-
if parts.is_some() {
837-
parts.as_mut().unwrap().next()
838-
} else {
839-
None
840-
}
841-
};
842-
843-
match idx {
818+
match parts.next() {
844819
None => return None,
845820
Some(s) => {
846821
let meta = match t.get(s) {
@@ -862,34 +837,29 @@ impl ShowMetadataCommand {
862837
cc.always_stderr = true;
863838
}
864839

865-
fn execute(mut self, _config: PersistentConfig, status: &mut dyn StatusBackend) -> Result<i32> {
866-
// `tectonic show metadata` should be shell-script-friendly.
867-
// Namely,it should either print a metadata value or return error code 1,
868-
// with NO OTHER OUTPUT.
840+
fn execute(self, _config: PersistentConfig, _status: &mut dyn StatusBackend) -> Result<i32> {
841+
// `tectonic show metadata` should be as shell-script-friendly as possible.
842+
// It should either print a value or return an error code, and produce NO OTHER OUTPUT.
843+
//
844+
// We return code 1 if `key` doesn't exist
845+
// We return code 2 if we can't open a workspace.
869846

870847
let meta = match Workspace::open_from_environment() {
871848
Ok(ws) => {
872849
let doc = ws.first_document();
873850
let meta = doc.metadata.clone();
874851
if meta.is_none() {
875-
// This workspace has no metadata.
876852
return Ok(1);
877853
}
878854
meta.unwrap()
879855
}
880856

881857
Err(_) => {
882-
//tt_error!(status, "could not load metadata. Are you in a workspace?");
883-
return Ok(1);
858+
return Ok(2);
884859
}
885860
};
886861

887-
let parts;
888-
if self.key.is_none() {
889-
parts = None;
890-
} else {
891-
parts = Some(self.key.as_mut().unwrap().split("."));
892-
}
862+
let parts = self.key.split(".");
893863

894864
let meta = get_metadata(&meta, parts);
895865
if meta.is_none() {

0 commit comments

Comments
 (0)