Struct cdx::prelude::TextFileMode
source · [−]pub struct TextFileMode {
pub cdx: bool,
pub head_mode: HeadMode,
pub col_mode: ColumnMode,
pub delim: u8,
pub line_break: u8,
}
Expand description
how to pase a text file into lines and columns
Fields
cdx: bool
does it have a cdx header
head_mode: HeadMode
yes, no, maybe
col_mode: ColumnMode
plain, quote, backslash
delim: u8
column delimiter
line_break: u8
line delimiter
Implementations
sourceimpl TextFileMode
impl TextFileMode
sourcepub fn new(spec: &str) -> Result<Self>
pub fn new(spec: &str) -> Result<Self>
new from spec
Examples found in repository?
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
pub fn consume(&mut self, args: &[ArgValue]) -> Result<()> {
for x in args {
if x.name == "header" {
self.checker.mode = HeaderMode::from_str(&x.value)?;
} else if x.name == "text-in" {
self.text_in = TextFileMode::new(&x.value)?;
} else if x.name == "text-out" {
self.text_out = Some(TextFileMode::new(&x.value)?);
} else if x.name == "std-agg" {
AggMaker::help();
return cdx_err(CdxError::NoError);
} else if x.name == "std-comp" {
CompMaker::help();
return cdx_err(CdxError::NoError);
} else if x.name == "std-const" {
expr::show_const();
return cdx_err(CdxError::NoError);
} else if x.name == "std-func" {
expr::show_func();
return cdx_err(CdxError::NoError);
} else if x.name == "std-gen" {
GenMaker::help();
return cdx_err(CdxError::NoError);
} else if x.name == "std-match" {
MatchMaker::help();
return cdx_err(CdxError::NoError);
} else if x.name == "std-trans" {
TransMaker::help();
return cdx_err(CdxError::NoError);
} else if x.name == "foo" {
eprintln!("Something with side effect");
} else {
unreachable!();
}
}
Ok(())
}
sourcepub fn split(&self, line: &mut TextLine)
pub fn split(&self, line: &mut TextLine)
split data line into columns
Examples found in repository?
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
pub fn getline(&mut self) -> Result<bool> {
self.loc.bytes += self.curr().line.len();
self.incr();
if self
.cont
.text
.read_line(&mut *self.file, &mut self.lines[self.curr].line)?
{
self.cont.is_done = true;
} else if self.do_split {
self.cont.text.split(&mut self.lines[self.curr]);
}
Ok(self.cont.is_done)
}
sourcepub fn split_head(&self, line: &mut StringLine)
pub fn split_head(&self, line: &mut StringLine)
split data line into columns
Examples found in repository?
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
fn read_header(&mut self, file: &mut impl BufRead, line: &mut TextLine) -> Result<()> {
self.is_empty = self.text.read_header(file, &mut self.header.line)?;
if self.is_empty {
return Ok(());
}
self.has_header = !self.header.line.is_empty();
if self.has_header {
self.text.split_head(&mut self.header);
}
self.is_done = self.text.read_line(file, &mut line.line)?;
if self.is_done {
return Ok(());
}
line.split(self.text.delim);
if !self.has_header {
let mut head_str = String::new();
for i in 1..=line.len() {
head_str += "c";
head_str += &i.to_string();
head_str += "\t";
}
head_str.pop();
head_str += "\n";
let mut fake_head = head_str.as_bytes();
self.header.read(&mut fake_head)?;
self.header.split(self.text.delim);
}
Ok(())
}
sourcepub fn ensure_eof(&self, line: &mut Vec<u8>) -> Result<bool>
pub fn ensure_eof(&self, line: &mut Vec<u8>) -> Result<bool>
append a newline, if line does not already end with one. Return false.
Examples found in repository?
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
pub fn read_line<T: BufRead>(&self, f: &mut T, line: &mut Vec<u8>) -> Result<bool> {
line.clear();
let sz = f.read_until(self.line_break, line)?;
if sz == 0 {
return Ok(true);
}
if self.col_mode != ColumnMode::Quote {
return self.ensure_eof(line);
}
let mut within = false;
let mut skip = 0;
loop {
for ch in line.iter().skip(skip) {
if *ch == b'"' {
within = !within;
}
}
skip = line.len();
if !within {
return self.ensure_eof(line);
}
let sz = f.read_until(self.line_break, line)?;
if sz == 0 {
return self.ensure_eof(line);
}
}
}
sourcepub fn read_string<T: BufRead>(
&self,
f: &mut T,
line: &mut String
) -> Result<bool>
pub fn read_string<T: BufRead>(
&self,
f: &mut T,
line: &mut String
) -> Result<bool>
read a line, dealing with quotes if necessary, return as String
Examples found in repository?
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
pub fn read_header<T: BufRead>(&self, f: &mut T, line: &mut String) -> Result<bool> {
line.clear();
let start = f.fill_buf()?;
if start.is_empty() {
return Ok(true);
}
match self.head_mode {
HeadMode::Yes => self.read_string(f, line),
HeadMode::No => Ok(false),
HeadMode::Maybe => {
if start.starts_with(b" CDX") {
self.read_string(f, line)
} else {
Ok(false)
}
}
HeadMode::Skip => {
let start = f.fill_buf()?;
if start.starts_with(b" CDX") {
self.read_string(f, line)?;
line.clear();
}
let start = f.fill_buf()?;
Ok(start.is_empty())
}
}
}
sourcepub fn read_header<T: BufRead>(
&self,
f: &mut T,
line: &mut String
) -> Result<bool>
pub fn read_header<T: BufRead>(
&self,
f: &mut T,
line: &mut String
) -> Result<bool>
read a line, dealing with quotes if necessary. Return true if file empty
Examples found in repository?
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
fn read_header(&mut self, file: &mut impl BufRead, line: &mut TextLine) -> Result<()> {
self.is_empty = self.text.read_header(file, &mut self.header.line)?;
if self.is_empty {
return Ok(());
}
self.has_header = !self.header.line.is_empty();
if self.has_header {
self.text.split_head(&mut self.header);
}
self.is_done = self.text.read_line(file, &mut line.line)?;
if self.is_done {
return Ok(());
}
line.split(self.text.delim);
if !self.has_header {
let mut head_str = String::new();
for i in 1..=line.len() {
head_str += "c";
head_str += &i.to_string();
head_str += "\t";
}
head_str.pop();
head_str += "\n";
let mut fake_head = head_str.as_bytes();
self.header.read(&mut fake_head)?;
self.header.split(self.text.delim);
}
Ok(())
}
sourcepub fn read_line<T: BufRead>(
&self,
f: &mut T,
line: &mut Vec<u8>
) -> Result<bool>
pub fn read_line<T: BufRead>(
&self,
f: &mut T,
line: &mut Vec<u8>
) -> Result<bool>
read a line, dealing with quotes if necessary. return true if no bytes read.
Examples found in repository?
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
pub fn read_string<T: BufRead>(&self, f: &mut T, line: &mut String) -> Result<bool> {
let mut x = Vec::new();
if self.read_line(f, &mut x)? {
return Ok(true);
}
*line = String::from_utf8(x)?;
Ok(false)
}
/// read a line, dealing with quotes if necessary. Return true if file empty
pub fn read_header<T: BufRead>(&self, f: &mut T, line: &mut String) -> Result<bool> {
line.clear();
let start = f.fill_buf()?;
if start.is_empty() {
return Ok(true);
}
match self.head_mode {
HeadMode::Yes => self.read_string(f, line),
HeadMode::No => Ok(false),
HeadMode::Maybe => {
if start.starts_with(b" CDX") {
self.read_string(f, line)
} else {
Ok(false)
}
}
HeadMode::Skip => {
let start = f.fill_buf()?;
if start.starts_with(b" CDX") {
self.read_string(f, line)?;
line.clear();
}
let start = f.fill_buf()?;
Ok(start.is_empty())
}
}
}
/// read a line, dealing with quotes if necessary. return true if no bytes read.
pub fn read_line<T: BufRead>(&self, f: &mut T, line: &mut Vec<u8>) -> Result<bool> {
line.clear();
let sz = f.read_until(self.line_break, line)?;
if sz == 0 {
return Ok(true);
}
if self.col_mode != ColumnMode::Quote {
return self.ensure_eof(line);
}
let mut within = false;
let mut skip = 0;
loop {
for ch in line.iter().skip(skip) {
if *ch == b'"' {
within = !within;
}
}
skip = line.len();
if !within {
return self.ensure_eof(line);
}
let sz = f.read_until(self.line_break, line)?;
if sz == 0 {
return self.ensure_eof(line);
}
}
}
}
/*
CDX mode yes/no (cx)
if cdx mode, yes,no,maybe,skip (ynms)
else yes or no (yn)
delim : auto escape stnl
quoting : omit, quote, backslash (oqb)
line delimiter auto escape stnr, C for crlf
default is cmton
regular csv is xy,qC
regular tsv is xytbn
plain tsv xy
cdx.maybe.Ct.literal.Ln
--text-in
*/
/// pointers into a vector, simulating a slice without the ownership issues
#[derive(Debug, Clone, Copy, Default)]
pub struct FakeSlice {
begin: u32,
end: u32,
}
impl FakeSlice {
/// new from text, like '7' or '3-6'
pub fn new(spec: &str) -> Result<Self> {
if let Some((a, b)) = spec.split_once('-') {
let begin = a.to_usize_whole(spec.as_bytes(), "range")? as u32;
let end = b.to_usize_whole(spec.as_bytes(), "range")? as u32;
if begin == 0 || end == 0 {
err!("Invalid range, both number must be greater than zero")
} else if begin > end {
err!("Invalid range, begin is greater than end")
} else {
Ok(Self {
begin: begin - 1,
end,
})
}
} else {
let num = spec.to_usize_whole(spec.as_bytes(), "position")? as u32;
if num == 0 {
err!("Invalid offset, must be greater than zero")
} else {
Ok(Self {
begin: num - 1,
end: num,
})
}
}
}
/// get slice from FakeSlice
pub fn get<'a>(&self, data: &'a [u8]) -> &'a [u8] {
&data[self.begin as usize..self.end as usize]
}
/// get slice from FakeSlice, but don't fall off the end.
pub fn get_safe<'a>(&self, data: &'a [u8]) -> &'a [u8] {
&data[cmp::min(self.begin as usize, data.len())..cmp::min(self.end as usize, data.len())]
}
/// len
pub const fn len(&self) -> usize {
(self.end - self.begin) as usize
}
/// is empty?
pub const fn is_empty(&self) -> bool {
self.begin == self.end
}
}
/// split a line by a delimiter. Remove trailing newline, if any.
pub fn split_plain(parts: &mut Vec<FakeSlice>, line: &[u8], delim: u8) {
parts.clear();
let mut begin: u32 = 0;
let mut end: u32 = 0;
#[allow(clippy::explicit_counter_loop)] // I need the counter to be u32
for ch in line.iter() {
if *ch == delim {
parts.push(FakeSlice { begin, end });
begin = end + 1;
}
end += 1;
}
if begin != end {
let mut f = FakeSlice { begin, end };
if line[(end - 1) as usize] == b'\n' {
f.end -= 1;
}
parts.push(f);
}
}
/// A line of a text file, broken into columns.
/// A line ends with a newline character, but column values do not.
/// An empty line contains one empty column
///```
/// use std::io::BufRead;
/// let mut data = b"one\ttwo\tthree\n";
/// let mut dp = &data[..];
/// let mut line = cdx::util::TextLine::new();
/// let eof = line.read(&mut dp).unwrap();
/// assert_eq!(eof, false);
/// assert_eq!(line.strlen(), 14);
/// line.split(b'\t');
/// assert_eq!(line.len(), 3);
/// assert_eq!(line.get(1), b"two");
///```
#[derive(Debug, Clone, Default)]
// pub(crate) because the borrow checker can be a nuisance.
pub struct TextLine {
// The whole input line, with newline, decoded as necessary
pub(crate) line: Vec<u8>,
// the individual columns, without newline
pub(crate) parts: Vec<FakeSlice>,
// the original input line, if any decoding was necessary
pub(crate) orig: Vec<u8>,
}
/// as TextLine, but [String] rather than `Vec<u8>`
#[derive(Debug, Clone, Default)]
pub struct StringLine {
/// The whole input line, with newline
pub line: String,
/// the individual columns
pub parts: Vec<FakeSlice>,
// the original input line, if any decoding was necessary
pub(crate) orig: String,
}
impl fmt::Display for TextLine {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for i in self {
write!(f, "{} ", str::from_utf8(i).unwrap())?;
}
Ok(())
}
}
impl fmt::Display for StringLine {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for i in self {
write!(f, "{} ", i)?;
}
Ok(())
}
}
impl std::ops::Index<usize> for TextLine {
type Output = [u8];
fn index(&self, pos: usize) -> &Self::Output {
self.get(pos)
}
}
impl std::ops::Index<usize> for StringLine {
type Output = str;
fn index(&self, pos: usize) -> &Self::Output {
self.get(pos)
}
}
/// write a buffer. If non-empty, ensure trailing newline
pub fn write_all_nl(w: &mut impl Write, buf: &[u8]) -> Result<()> {
if !buf.is_empty() {
w.write_all(buf)?;
if buf[buf.len() - 1] != b'\n' {
w.write_all(&[b'\n'])?;
}
}
Ok(())
}
impl TextLine {
/// whole line, with newline
pub fn parts(&mut self) -> &mut Vec<FakeSlice> {
&mut self.parts
}
/// whole line, with newline
pub fn line(&self) -> &[u8] {
&self.line
}
/// whole line, without newline
pub fn line_nl(&self) -> &[u8] {
&self.line[..self.line.len() - 1]
}
/// whole line, with newline, as Vec
pub fn raw(&mut self) -> &mut Vec<u8> {
&mut self.line
}
/// assign TextLine into existing TextLine, avoiding allocation if possible
pub fn assign(&mut self, x: &Self) {
self.line.clear();
self.line.extend_from_slice(&x.line[..]);
self.parts.clear();
self.parts.extend_from_slice(&x.parts[..]);
}
/// make a new TextLine
pub const fn new() -> Self {
Self {
line: Vec::new(),
parts: Vec::new(),
orig: Vec::new(),
}
}
/// Iterator over columns in the line
pub const fn iter(&self) -> TextLineIter<'_> {
TextLineIter {
line: self,
index: 0,
}
}
/// empty the line
pub fn clear(&mut self) {
self.parts.clear();
self.line.clear();
}
/// How many column in the line
pub fn len(&self) -> usize {
self.parts.len()
}
/// How many bytes in the line
pub fn strlen(&self) -> usize {
self.line.len()
}
/// should always be false, but required by clippy
pub fn is_empty(&self) -> bool {
self.parts.is_empty()
}
/// Get one column. Return an empty column if index is too big.
pub fn get(&self, index: usize) -> &[u8] {
if index >= self.parts.len() {
&self.line[0..0]
} else {
&self.line[self.parts[index].begin as usize..self.parts[index].end as usize]
}
}
/// Read a new line from a file, should generally be followed by `split`
pub fn read<T: BufRead>(&mut self, f: &mut T) -> Result<bool> {
self.clear();
let sz = f.read_until(b'\n', &mut self.line)?;
if sz == 0 {
Ok(true)
} else {
if self.line.last() != Some(&b'\n') {
self.line.push(b'\n');
}
Ok(false)
}
}
/// split the line into columns
/// hypothetically you could split on one delimiter, do some work, then split on a different delimiter.
pub fn split(&mut self, delim: u8) {
split_plain(&mut self.parts, &self.line, delim);
}
/// return all parts as a vector
pub fn vec(&self) -> Vec<&[u8]> {
self.iter().collect()
}
}
impl StringLine {
/// make a new StringLine
pub const fn new() -> Self {
Self {
line: String::new(),
parts: Vec::new(),
orig: String::new(),
}
}
/// Iterator over columns in the line
pub const fn iter(&self) -> StringLineIter<'_> {
StringLineIter {
line: self,
index: 0,
}
}
/// create a fake CDX header with columns c1,c2...
pub fn fake(&mut self, num_cols: usize, delim: u8) {
self.line = " CDX".to_string();
for i in 1..=num_cols {
self.line += std::str::from_utf8(&[delim]).unwrap();
self.line += "c";
self.line += &i.to_string();
}
self.line += "\n";
}
fn clear(&mut self) {
self.parts.clear();
self.line.clear();
}
/// How many column in the line
pub fn len(&self) -> usize {
self.parts.len()
}
/// How many bytes in the line
pub fn strlen(&self) -> usize {
self.line.len()
}
/// should always be false, but required by clippy
pub fn is_empty(&self) -> bool {
self.parts.is_empty()
}
/// Get one column. Return an empty column if index is too big.
pub fn get(&self, index: usize) -> &str {
if index >= self.parts.len() {
&self.line[0..0]
} else {
&self.line[self.parts[index].begin as usize..self.parts[index].end as usize]
}
}
/// Read a new line from a file, should generally be followed by `split`
pub fn read<T: BufRead>(&mut self, f: &mut T) -> Result<bool> {
self.clear();
let sz = f.read_line(&mut self.line)?;
if sz == 0 {
Ok(true)
} else {
if self.line.as_bytes().last() != Some(&b'\n') {
self.line.push('\n');
}
Ok(false)
}
}
/// split the line into columns
/// hypothetically you could split on one delimiter, do some work, then split on a different delimiter.
pub fn split(&mut self, delim: u8) {
split_plain(&mut self.parts, self.line.as_bytes(), delim);
}
/// return all parts as a vector
pub fn vec(&self) -> Vec<&str> {
self.iter().collect()
}
}
impl<'a> IntoIterator for &'a TextLine {
type Item = &'a [u8];
type IntoIter = TextLineIter<'a>;
fn into_iter(self) -> Self::IntoIter {
TextLineIter {
line: self,
index: 0,
}
}
}
impl<'a> IntoIterator for &'a StringLine {
type Item = &'a str;
type IntoIter = StringLineIter<'a>;
fn into_iter(self) -> Self::IntoIter {
StringLineIter {
line: self,
index: 0,
}
}
}
/// Iterator over the columns in a TextLine
#[derive(Debug, Clone)]
pub struct TextLineIter<'a> {
line: &'a TextLine,
index: usize,
}
/// Iterator over the columns in a StringLine
#[derive(Debug, Clone)]
pub struct StringLineIter<'a> {
line: &'a StringLine,
index: usize,
}
impl<'a> Iterator for TextLineIter<'a> {
// we will be counting with usize
type Item = &'a [u8];
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.line.len() {
None
} else {
self.index += 1;
Some(&self.line[self.index - 1])
}
}
}
impl<'a> Iterator for StringLineIter<'a> {
// we will be counting with usize
type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.line.parts.len() {
None
} else {
self.index += 1;
Some(&self.line[self.index - 1])
}
}
}
struct S3Reader {
// name : String,
rt: tokio::runtime::Runtime,
// client : aws_sdk_s3::Client,
f: aws_sdk_s3::output::GetObjectOutput,
left: Option<bytes::Bytes>,
}
impl S3Reader {
fn new(bucket: &str, key: &str) -> Result<Self> {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;
let shared_config = rt.block_on(aws_config::load_from_env());
let client = aws_sdk_s3::Client::new(&shared_config);
let obj = rt.block_on(client.get_object().bucket(bucket).key(key).send())?;
Ok(Self {
// name : key.to_string(),
rt,
// client,
f: obj,
left: None,
})
}
fn new_path(spec: &str) -> Result<Self> {
if let Some(name) = spec.strip_prefix("s3://") {
if let Some((a, b)) = name.split_once('/') {
Self::new(a, b)
} else {
err!("Not an S3 file spec '{}'", spec)
}
} else {
err!("Not an S3 file '{}'", spec)
}
}
}
impl Read for S3Reader {
fn read(&mut self, buf: &mut [u8]) -> std::result::Result<usize, std::io::Error> {
if let Some(bytes) = &self.left {
if bytes.len() > buf.len() {
buf.clone_from_slice(&bytes[..buf.len()]);
self.left = Some(bytes.slice(buf.len()..));
return Ok(buf.len());
} else {
let len = bytes.len();
buf[0..len].clone_from_slice(bytes);
self.left = None;
return Ok(len);
}
}
let bytes_res = self.rt.block_on(self.f.body.try_next());
if bytes_res.is_err() {
return Err(std::io::Error::new(std::io::ErrorKind::Other, "oh no"));
}
self.left = bytes_res.unwrap();
if let Some(bytes) = &self.left {
if bytes.len() > buf.len() {
buf.clone_from_slice(&bytes[..buf.len()]);
self.left = Some(bytes.slice(buf.len()..));
Ok(buf.len())
} else {
let len = bytes.len();
buf[0..bytes.len()].clone_from_slice(bytes);
self.left = None;
Ok(len)
}
} else {
Ok(0)
}
}
}
/// Input file. Wrapped in a type so I can 'impl Debug'
pub struct Infile(
/// The file being read
pub io::BufReader<Box<dyn Read>>,
pub String,
);
impl Infile {
/// create a new input file
pub fn new(f: io::BufReader<Box<dyn Read>>, n: &str) -> Self {
Self(f, n.to_string())
}
}
impl Default for Infile {
fn default() -> Self {
Self::new(io::BufReader::new(Box::new(io::empty())), "")
}
}
impl fmt::Debug for Infile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Infile : {}", self.1)
}
}
impl Deref for Infile {
type Target = io::BufReader<Box<dyn Read>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Infile {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl AsRef<io::BufReader<Box<dyn Read>>> for Infile {
fn as_ref(&self) -> &io::BufReader<Box<dyn Read>> {
&self.0
}
}
impl AsMut<io::BufReader<Box<dyn Read>>> for Infile {
fn as_mut(&mut self) -> &mut io::BufReader<Box<dyn Read>> {
&mut self.0
}
}
/// output file type
pub struct Outfile(pub io::BufWriter<Box<dyn Write>>, pub String);
impl Outfile {
/// create a new input file
pub fn new(f: io::BufWriter<Box<dyn Write>>, n: &str) -> Self {
Self(f, n.to_string())
}
}
impl Default for Outfile {
fn default() -> Self {
Self::new(io::BufWriter::new(Box::new(io::sink())), "")
}
}
impl fmt::Debug for Outfile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Outfile : {}", self.1)
}
}
impl Deref for Outfile {
type Target = io::BufWriter<Box<dyn Write>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Outfile {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl AsRef<io::BufWriter<Box<dyn Write>>> for Outfile {
fn as_ref(&self) -> &io::BufWriter<Box<dyn Write>> {
&self.0
}
}
impl AsMut<io::BufWriter<Box<dyn Write>>> for Outfile {
fn as_mut(&mut self) -> &mut io::BufWriter<Box<dyn Write>> {
&mut self.0
}
}
/// Make an Outfile from a file name
pub fn get_writer(name: &str) -> Result<Outfile> {
let inner: Box<dyn Write> = {
if name == "-" {
Box::new(io::stdout())
} else if name == "--" {
Box::new(io::stderr())
} else {
Box::new(fs::OpenOptions::new().write(true).create(true).open(name)?)
}
};
Ok(Outfile::new(io::BufWriter::new(inner), name))
}
// should return Cow<>
fn unescape_vec(data: &[u8]) -> Vec<u8> {
let mut ret: Vec<u8> = Vec::with_capacity(data.len());
let mut last_was_slash = false;
for x in data {
if last_was_slash {
ret.push(match x {
b'n' => b'\n',
b't' => b'\t',
b's' => b' ',
ch => *ch,
});
last_was_slash = false;
} else if x == &b'\\' {
last_was_slash = true;
} else {
ret.push(*x);
}
}
if last_was_slash {
ret.push(b'\\');
}
ret
}
/// Make an Infile from a file name
pub fn get_reader(name: &str) -> Result<Infile> {
let inner: Box<dyn Read> = {
if name == "-" {
// unsafe { Box::new(std::fs::File::from_raw_fd(1)) }
Box::new(io::stdin())
} else if name.starts_with("s3://") {
Box::new(S3Reader::new_path(name)?)
} else if let Some(stripped) = name.strip_prefix("<<") {
Box::new(std::io::Cursor::new(unescape_vec(stripped.as_bytes())))
} else {
Box::new(fs::File::open(name)?)
}
};
let mut outer = io::BufReader::new(inner);
let start = outer.fill_buf()?;
if start.starts_with(&[0x1fu8, 0x8bu8, 0x08u8]) {
outer = io::BufReader::new(Box::new(MultiGzDecoder::new(outer)));
}
Ok(Infile::new(outer, name))
}
#[derive(Debug, Default)]
/// shared context for any input file type
pub struct InfileContext {
// CDX header, contructed if necessary
header: StringLine,
// have we read all the btes of the file
is_done: bool,
// is the file length zero
is_empty: bool,
// was there a CDX header?
has_header: bool,
text: TextFileMode,
}
/// create appropriate header from first line of file
pub fn make_header(line: &[u8]) -> StringLine {
let mut s = StringLine::new();
if is_cdx(line) {
s.line = String::from_utf8_lossy(&line[5..]).to_string();
} else {
s.line = String::new();
for x in 1..=line.split(|ch| *ch == b'\t').count() {
s.line.push_str(&format!("c{}\t", x));
}
s.line.pop();
}
s.split(b'\t');
s
}
// if CDX and specified and different, then strip header
/// Reader header line, if any, and first line of text
impl InfileContext {
const fn new(text_in: &TextFileMode) -> Self {
Self {
header: StringLine::new(),
is_done: true,
is_empty: true,
has_header: false,
text: *text_in,
}
}
fn read_header(&mut self, file: &mut impl BufRead, line: &mut TextLine) -> Result<()> {
self.is_empty = self.text.read_header(file, &mut self.header.line)?;
if self.is_empty {
return Ok(());
}
self.has_header = !self.header.line.is_empty();
if self.has_header {
self.text.split_head(&mut self.header);
}
self.is_done = self.text.read_line(file, &mut line.line)?;
if self.is_done {
return Ok(());
}
line.split(self.text.delim);
if !self.has_header {
let mut head_str = String::new();
for i in 1..=line.len() {
head_str += "c";
head_str += &i.to_string();
head_str += "\t";
}
head_str.pop();
head_str += "\n";
let mut fake_head = head_str.as_bytes();
self.header.read(&mut fake_head)?;
self.header.split(self.text.delim);
}
Ok(())
}
}
/// where are we, in which file?
#[derive(Debug, Default, Clone)]
pub struct FileLocData {
/// full file name
name: String,
/// byte offset, uncompressed
bytes: usize,
/// line number
line: usize,
}
/// types of file location data
#[derive(Debug, Copy, Clone)]
enum FileLocItem {
/// byte offset of start of line
Bytes,
/// 1-based line number
Line,
/// file name, with given number of parts
Name(usize),
}
impl FileLocItem {
fn new(spec: &str) -> Result<Self> {
if spec.eq_ignore_ascii_case("bytes") {
Ok(Self::Bytes)
} else if spec.eq_ignore_ascii_case("line") {
Ok(Self::Line)
} else if spec.eq_ignore_ascii_case("name") {
Ok(Self::Name(0))
} else if let Some((a, b)) = spec.split_once('.') {
if a.eq_ignore_ascii_case("name") {
Ok(Self::Name(
b.to_usize_whole(spec.as_bytes(), "File location")?,
))
} else {
err!("File Loc must be once of Bytes, Line, Name : '{}'", spec)
}
} else {
err!("File Loc must be once of Bytes, Line, Name : '{}'", spec)
}
}
const fn dflt_name(&self) -> &'static str {
match self {
Self::Bytes => "bytes",
Self::Line => "line",
Self::Name(_) => "filename",
}
}
fn write_data(&mut self, data: &mut impl Write, loc: &FileLocData) -> Result<()> {
match self {
Self::Bytes => write!(data, "{}", loc.bytes).unwrap(),
Self::Line => write!(data, "{}", loc.line).unwrap(),
Self::Name(n) => {
if *n == 0 {
data.write_all(loc.name.as_bytes())?;
} else {
// FIXME - this should really be cached somehow
data.write_all(loc.name.tail_path_u8(*n, b'/').as_bytes())?;
}
}
}
Ok(())
}
}
/// FileLocItem with column name
#[derive(Debug, Clone)]
struct FileLoc {
col_name: String,
item: FileLocItem,
}
impl FileLoc {
fn new(spec: &str) -> Result<Self> {
if let Some((a, b)) = spec.split_once(':') {
Ok(Self {
col_name: a.to_string(),
item: FileLocItem::new(b)?,
})
} else {
let item = FileLocItem::new(spec)?;
Ok(Self {
col_name: item.dflt_name().to_string(),
item,
})
}
}
fn write_data(&mut self, data: &mut impl Write, loc: &FileLocData) -> Result<()> {
self.item.write_data(data, loc)
}
}
/// List of FileLoc
#[derive(Default, Debug, Clone)]
pub struct FileLocList {
v: Vec<FileLoc>,
}
impl FileLocList {
/// new
pub fn new() -> Self {
Self::default()
}
/// new
pub fn is_empty(&self) -> bool {
self.v.is_empty()
}
/// add Name:Spec
pub fn push(&mut self, spec: &str) -> Result<()> {
for x in spec.split(',') {
self.v.push(FileLoc::new(x)?);
}
Ok(())
}
/// fill data with file loc data
pub fn write_data(
&mut self,
data: &mut impl Write,
delim: u8,
loc: &FileLocData,
) -> Result<()> {
for x in &mut self.v {
x.write_data(data, loc)?;
data.write_all(&[delim])?;
}
Ok(())
}
/// fill data with column names
pub fn write_names(&mut self, data: &mut String, delim: u8) {
for x in &mut self.v {
data.push_str(&x.col_name);
data.push(delim as char);
}
}
/// add new columns to header
pub fn add(&self, header: &mut ColumnHeader) -> Result<()> {
for x in &self.v {
header.push(&x.col_name)?;
}
Ok(())
}
}
/// Text file reader. Lines broken into columns, with lookback
pub struct Reader {
file: Infile,
lines: Vec<TextLine>,
cont: InfileContext,
do_split: bool,
curr: usize,
loc: FileLocData,
}
impl fmt::Debug for Reader {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Reader")
}
}
impl Default for Reader {
fn default() -> Self {
Self::new(&TextFileMode::default())
}
}
impl Reader {
/// loc
pub const fn loc(&self) -> &FileLocData {
&self.loc
}
/// make a new Reader
pub fn new(text_in: &TextFileMode) -> Self {
Self::new_with(1, text_in)
}
/// set to false to skip breaking into columns
pub fn do_split(&mut self, val: bool) {
self.do_split = val;
}
/// make a new Reader, with explicit lookback
pub fn new_with(lookback: usize, text_in: &TextFileMode) -> Self {
let mut lines: Vec<TextLine> = Vec::new();
lines.resize(lookback + 1, TextLine::new());
Self {
file: Infile::default(),
lines,
cont: InfileContext::new(text_in),
do_split: true,
curr: 0,
loc: FileLocData::default(),
}
}
/// make a new Reader, with deault text, FIXME - delete this
pub fn new_open2(name: &str) -> Result<Self> {
Self::new_open_with(name, 1, &TextFileMode::default())
}
/// make a new Reader
pub fn new_open(name: &str, text_in: &TextFileMode) -> Result<Self> {
Self::new_open_with(name, 1, text_in)
}
/// make a new Reader
pub fn new_open_with(name: &str, lookback: usize, text_in: &TextFileMode) -> Result<Self> {
let mut lines: Vec<TextLine> = Vec::new();
lines.resize(lookback + 1, TextLine::new());
let mut tmp = Self {
file: get_reader(name)?,
lines,
cont: InfileContext::new(text_in),
do_split: true,
curr: 0,
loc: FileLocData::default(),
};
tmp.cont.read_header(&mut *tmp.file, &mut tmp.lines[0])?;
tmp.loc.name = name.to_string();
tmp.loc.line = 1;
tmp.loc.bytes = if tmp.has_header() {
tmp.header().line.len()
} else {
0
};
Ok(tmp)
}
/// get current line contents, without the trailing newline
pub fn curr_nl(&self) -> &[u8] {
let line = self.curr_line();
&line.line[0..line.line.len() - 1]
}
/// get previous line contents, without the trailing newline
pub fn prev_nl(&self, n: usize) -> &[u8] {
let line = self.prev_line(n);
&line.line[0..line.line.len() - 1]
}
/// get delimiter
pub const fn delim(&self) -> u8 {
self.cont.text.delim
}
/// get column names
pub fn names(&self) -> Vec<&str> {
self.cont.header.vec()
}
/// write the current text line with newline
pub fn write(&self, w: &mut impl Write) -> Result<()> {
w.write_all(&self.curr_line().line)?;
Ok(())
}
/// open file for reading
pub fn open(&mut self, name: &str) -> Result<()> {
self.file = get_reader(name)?;
self.cont.read_header(&mut *self.file, &mut self.lines[0])
}
/// The full text of the header, without the trailing newline
pub const fn header_line(&self) -> &String {
&self.cont.header.line
}
/// was file zero bytes?
pub const fn is_empty(&self) -> bool {
self.cont.is_empty
}
/// have we hit EOF?
pub const fn is_done(&self) -> bool {
self.cont.is_done
}
/// line number of curr_line
pub const fn line_number(&self) -> usize {
self.loc.line
}
fn incr(&mut self) {
self.loc.line += 1;
self.curr += 1;
if self.curr >= self.lines.len() {
self.curr = 0;
}
}
/// get next line of text
pub fn getline(&mut self) -> Result<bool> {
self.loc.bytes += self.curr().line.len();
self.incr();
if self
.cont
.text
.read_line(&mut *self.file, &mut self.lines[self.curr].line)?
{
self.cont.is_done = true;
} else if self.do_split {
self.cont.text.split(&mut self.lines[self.curr]);
}
Ok(self.cont.is_done)
}
Trait Implementations
sourceimpl Clone for TextFileMode
impl Clone for TextFileMode
sourcefn clone(&self) -> TextFileMode
fn clone(&self) -> TextFileMode
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for TextFileMode
impl Debug for TextFileMode
sourceimpl Default for TextFileMode
impl Default for TextFileMode
impl Copy for TextFileMode
Auto Trait Implementations
impl RefUnwindSafe for TextFileMode
impl Send for TextFileMode
impl Sync for TextFileMode
impl Unpin for TextFileMode
impl UnwindSafe for TextFileMode
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more