|
5 | 5 |
|
6 | 6 | // spell-checker:ignore bitlen |
7 | 7 |
|
8 | | -use std::ffi::OsStr; |
| 8 | +use std::ffi::{OsStr, OsString}; |
9 | 9 | use std::fs::File; |
10 | 10 | use std::io::{self, BufReader, Read, Write}; |
11 | 11 | use std::path::Path; |
12 | 12 |
|
13 | | -use crate::checksum::{ChecksumError, SizedAlgoKind, digest_reader, escape_filename}; |
| 13 | +use crate::checksum::{AlgoKind, ChecksumError, SizedAlgoKind, digest_reader, escape_filename}; |
14 | 14 | use crate::error::{FromIo, UResult, USimpleError}; |
15 | 15 | use crate::line_ending::LineEnding; |
16 | 16 | use crate::sum::DigestOutput; |
@@ -103,42 +103,76 @@ impl OutputFormat { |
103 | 103 | fn is_raw(&self) -> bool { |
104 | 104 | *self == Self::Raw |
105 | 105 | } |
106 | | -} |
107 | 106 |
|
108 | | -/// Use already-processed arguments to decide the output format. |
109 | | -pub fn figure_out_output_format( |
110 | | - algo: SizedAlgoKind, |
111 | | - tag: bool, |
112 | | - binary: bool, |
113 | | - raw: bool, |
114 | | - base64: bool, |
115 | | -) -> OutputFormat { |
116 | | - // Raw output format takes precedence over anything else. |
117 | | - if raw { |
118 | | - return OutputFormat::Raw; |
119 | | - } |
| 107 | + /// Find the correct output format for cksum. |
| 108 | + pub fn from_cksum(algo: AlgoKind, tag: bool, binary: bool, raw: bool, base64: bool) -> Self { |
| 109 | + // Raw output format takes precedence over anything else. |
| 110 | + if raw { |
| 111 | + return Self::Raw; |
| 112 | + } |
| 113 | + |
| 114 | + // Then, if the algo is legacy, takes precedence over the rest |
| 115 | + if algo.is_legacy() { |
| 116 | + return Self::Legacy; |
| 117 | + } |
120 | 118 |
|
121 | | - // Then, if the algo is legacy, takes precedence over the rest |
122 | | - if algo.is_legacy() { |
123 | | - return OutputFormat::Legacy; |
| 119 | + let digest_format = if base64 { |
| 120 | + DigestFormat::Base64 |
| 121 | + } else { |
| 122 | + DigestFormat::Hexadecimal |
| 123 | + }; |
| 124 | + |
| 125 | + // After that, decide between tagged and untagged output |
| 126 | + if tag { |
| 127 | + Self::Tagged(digest_format) |
| 128 | + } else { |
| 129 | + let reading_mode = if binary { |
| 130 | + ReadingMode::Binary |
| 131 | + } else { |
| 132 | + ReadingMode::Text |
| 133 | + }; |
| 134 | + Self::Untagged(digest_format, reading_mode) |
| 135 | + } |
124 | 136 | } |
125 | 137 |
|
126 | | - let digest_format = if base64 { |
127 | | - DigestFormat::Base64 |
128 | | - } else { |
129 | | - DigestFormat::Hexadecimal |
130 | | - }; |
| 138 | + /// Find the correct output format for a standalone checksum util (b2sum, |
| 139 | + /// md5sum, etc) |
| 140 | + /// |
| 141 | + /// Since standalone utils can't use the Raw or Legacy output format, it is |
| 142 | + /// decided only using the --tag, --binary and --text arguments. |
| 143 | + pub fn from_standalone(args: impl Iterator<Item = OsString>) -> UResult<Self> { |
| 144 | + let mut text = true; |
| 145 | + let mut tag = false; |
| 146 | + |
| 147 | + for arg in args { |
| 148 | + if arg == "--" { |
| 149 | + break; |
| 150 | + } else if arg == "--tag" { |
| 151 | + tag = true; |
| 152 | + text = false; |
| 153 | + } else if arg == "--binary" || arg == "-b" { |
| 154 | + text = false; |
| 155 | + } else if arg == "--text" || arg == "-t" { |
| 156 | + // Finding a `--text` after `--tag` is an error. |
| 157 | + if tag { |
| 158 | + return Err(ChecksumError::TextAfterTag.into()); |
| 159 | + } |
| 160 | + text = true; |
| 161 | + } |
| 162 | + } |
131 | 163 |
|
132 | | - // After that, decide between tagged and untagged output |
133 | | - if tag { |
134 | | - OutputFormat::Tagged(digest_format) |
135 | | - } else { |
136 | | - let reading_mode = if binary { |
137 | | - ReadingMode::Binary |
| 164 | + if tag { |
| 165 | + Ok(Self::Tagged(DigestFormat::Hexadecimal)) |
138 | 166 | } else { |
139 | | - ReadingMode::Text |
140 | | - }; |
141 | | - OutputFormat::Untagged(digest_format, reading_mode) |
| 167 | + Ok(Self::Untagged( |
| 168 | + DigestFormat::Hexadecimal, |
| 169 | + if text { |
| 170 | + ReadingMode::Text |
| 171 | + } else { |
| 172 | + ReadingMode::Binary |
| 173 | + }, |
| 174 | + )) |
| 175 | + } |
142 | 176 | } |
143 | 177 | } |
144 | 178 |
|
|
0 commit comments