|
1 | 1 | use crate::read_datafile;
|
2 | 2 | use anyhow::{anyhow, Context};
|
3 |
| -use argh::FromArgs; |
4 | 3 | use avrow::{Header, Reader};
|
5 | 4 | use std::io::Read;
|
6 | 5 | use std::path::PathBuf;
|
7 | 6 | use std::str;
|
8 | 7 |
|
9 |
| -#[derive(FromArgs, PartialEq, Debug)] |
10 |
| -/// Get metadata information of the avro datafile. |
11 |
| -#[argh(subcommand, name = "getmeta")] |
12 |
| -pub struct GetMeta { |
13 |
| - /// datafile as input |
14 |
| - #[argh(option, short = 'd')] |
15 |
| - datafile: PathBuf, |
16 |
| -} |
17 |
| - |
18 |
| -impl GetMeta { |
19 |
| - pub fn getmeta(&self) -> Result<(), anyhow::Error> { |
20 |
| - let mut avro_datafile = read_datafile(&self.datafile)?; |
21 |
| - let header = Header::from_reader(&mut avro_datafile)?; |
22 |
| - for (k, v) in header.metadata() { |
23 |
| - print!("{}\t", k); |
24 |
| - println!( |
25 |
| - "{}", |
26 |
| - str::from_utf8(v).expect("Invalid UTF-8 in avro header") |
27 |
| - ); |
28 |
| - } |
29 |
| - Ok(()) |
| 8 | +pub fn metadata(datafile: &PathBuf) -> Result<(), anyhow::Error> { |
| 9 | + let mut avro_datafile = read_datafile(datafile)?; |
| 10 | + let header = Header::from_reader(&mut avro_datafile)?; |
| 11 | + for (k, v) in header.metadata() { |
| 12 | + print!("{}\t", k); |
| 13 | + println!( |
| 14 | + "{}", |
| 15 | + str::from_utf8(v).expect("Invalid UTF-8 in avro header") |
| 16 | + ); |
30 | 17 | }
|
| 18 | + Ok(()) |
31 | 19 | }
|
32 | 20 |
|
33 |
| -#[derive(FromArgs, PartialEq, Debug)] |
34 |
| -/// Prints data from datafile in debug format. |
35 |
| -#[argh(subcommand, name = "read")] |
36 |
| -pub struct ReadData { |
37 |
| - /// datafile as input |
38 |
| - #[argh(option, short = 'd')] |
39 |
| - datafile: PathBuf, |
40 |
| -} |
41 |
| -impl ReadData { |
42 |
| - pub fn read_data(&self) -> Result<(), anyhow::Error> { |
43 |
| - let mut avro_datafile = read_datafile(&self.datafile)?; |
44 |
| - let reader = Reader::new(&mut avro_datafile)?; |
45 |
| - // TODO: remove irrelevant fields |
46 |
| - for i in reader { |
47 |
| - println!("{:#?}", i?); |
48 |
| - } |
49 |
| - |
50 |
| - Ok(()) |
| 21 | +pub fn read(datafile: &PathBuf) -> Result<(), anyhow::Error> { |
| 22 | + let mut avro_datafile = read_datafile(datafile)?; |
| 23 | + let reader = Reader::new(&mut avro_datafile)?; |
| 24 | + // TODO: remove irrelevant fields |
| 25 | + for i in reader { |
| 26 | + println!("{:?}", i?); |
51 | 27 | }
|
52 |
| -} |
53 | 28 |
|
54 |
| -#[derive(FromArgs, PartialEq, Debug)] |
55 |
| -/// Dumps the avro datafile as bytes for debugging purposes |
56 |
| -#[argh(subcommand, name = "tobytes")] |
57 |
| -pub struct ToBytes { |
58 |
| - /// datafile as input |
59 |
| - #[argh(option, short = 'd')] |
60 |
| - datafile: PathBuf, |
| 29 | + Ok(()) |
61 | 30 | }
|
62 | 31 |
|
63 |
| -impl ToBytes { |
64 |
| - pub fn tobytes(&self) -> Result<(), anyhow::Error> { |
65 |
| - let mut avro_datafile = read_datafile(&self.datafile)?; |
66 |
| - let mut v = vec![]; |
| 32 | +pub fn bytes(datafile: &PathBuf) -> Result<(), anyhow::Error> { |
| 33 | + let mut avro_datafile = read_datafile(datafile)?; |
| 34 | + let mut v = vec![]; |
67 | 35 |
|
68 |
| - avro_datafile |
69 |
| - .read_to_end(&mut v) |
70 |
| - .with_context(|| "Failed to read data file in memory")?; |
| 36 | + avro_datafile |
| 37 | + .read_to_end(&mut v) |
| 38 | + .with_context(|| "Failed to read datafile")?; |
71 | 39 |
|
72 |
| - println!("{:?}", v); |
73 |
| - Ok(()) |
74 |
| - } |
| 40 | + println!("{:?}", v); |
| 41 | + Ok(()) |
75 | 42 | }
|
76 | 43 |
|
77 |
| -#[derive(FromArgs, PartialEq, Debug)] |
78 |
| -/// Prints the writer's schema encoded in the provided datafile. |
79 |
| -#[argh(subcommand, name = "getschema")] |
80 |
| -pub struct GetSchema { |
81 |
| - /// datafile as input |
82 |
| - #[argh(option, short = 'd')] |
83 |
| - datafile: PathBuf, |
| 44 | +pub fn schema(datafile: &PathBuf) -> Result<(), anyhow::Error> { |
| 45 | + let mut avro_datafile = read_datafile(datafile)?; |
| 46 | + let header = Header::from_reader(&mut avro_datafile)?; |
| 47 | + // TODO print human readable schema |
| 48 | + println!("{}", header.schema()); |
| 49 | + Ok(()) |
84 | 50 | }
|
85 | 51 |
|
86 |
| -impl GetSchema{ |
87 |
| - pub fn getschema(&self) -> Result<(), anyhow::Error> { |
88 |
| - let mut avro_datafile = read_datafile(&self.datafile)?; |
89 |
| - let header = Header::from_reader(&mut avro_datafile)?; |
90 |
| - // TODO print human readable schema |
91 |
| - dbg!(header.schema()); |
92 |
| - Ok(()) |
93 |
| - } |
94 |
| -} |
95 |
| - |
96 |
| -#[derive(FromArgs, PartialEq, Debug)] |
97 |
| -/// Prints fingerprint of the canonical form of writer's schema. |
98 |
| -#[argh(subcommand, name = "fingerprint")] |
99 |
| -pub struct Fingerprint { |
100 |
| - /// datafile as input |
101 |
| - #[argh(option, short = 'd')] |
102 |
| - datafile: String, |
103 |
| - /// the fingerprinting algorithm (rabin64 (default), sha256, md5) |
104 |
| - #[argh(option, short = 'f')] |
105 |
| - fingerprint: String, |
106 |
| -} |
107 |
| -impl Fingerprint { |
108 |
| - pub fn fingerprint(&self) -> Result<(), anyhow::Error> { |
109 |
| - let mut avro_datafile = read_datafile(&self.datafile)?; |
110 |
| - let header = Header::from_reader(&mut avro_datafile)?; |
111 |
| - match self.fingerprint.as_ref() { |
112 |
| - "rabin64" => { |
113 |
| - println!("0x{:x}", header.schema().canonical_form().rabin64()); |
114 |
| - }, |
115 |
| - "sha256" => { |
116 |
| - let mut fingerprint_str = String::new(); |
117 |
| - let sha256 = header.schema().canonical_form().sha256(); |
118 |
| - for i in sha256 { |
119 |
| - let a = format!("{:x}", i); |
120 |
| - fingerprint_str.push_str(&a); |
121 |
| - } |
122 |
| - |
123 |
| - println!("{}", fingerprint_str); |
| 52 | +pub fn fingerprint(datafile: &PathBuf, fingerprint: &str) -> Result<(), anyhow::Error> { |
| 53 | + let mut avro_datafile = read_datafile(datafile)?; |
| 54 | + let header = Header::from_reader(&mut avro_datafile)?; |
| 55 | + match fingerprint.as_ref() { |
| 56 | + "rabin64" => { |
| 57 | + println!("0x{:x}", header.schema().canonical_form().rabin64()); |
| 58 | + } |
| 59 | + "sha256" => { |
| 60 | + let mut fingerprint_str = String::new(); |
| 61 | + let sha256 = header.schema().canonical_form().sha256(); |
| 62 | + for i in sha256 { |
| 63 | + let a = format!("{:x}", i); |
| 64 | + fingerprint_str.push_str(&a); |
124 | 65 | }
|
125 |
| - "md5" => { |
126 |
| - let mut fingerprint_str = String::new(); |
127 |
| - let md5 = header.schema().canonical_form().md5(); |
128 |
| - for i in md5 { |
129 |
| - let a = format!("{:x}", i); |
130 |
| - fingerprint_str.push_str(&a); |
131 |
| - } |
132 |
| - |
133 |
| - println!("{}", fingerprint_str); |
| 66 | + |
| 67 | + println!("{}", fingerprint_str); |
| 68 | + } |
| 69 | + "md5" => { |
| 70 | + let mut fingerprint_str = String::new(); |
| 71 | + let md5 = header.schema().canonical_form().md5(); |
| 72 | + for i in md5 { |
| 73 | + let a = format!("{:x}", i); |
| 74 | + fingerprint_str.push_str(&a); |
134 | 75 | }
|
135 |
| - other => return Err(anyhow!("invalid or unsupported fingerprint: {}", other)) |
| 76 | + |
| 77 | + println!("{}", fingerprint_str); |
136 | 78 | }
|
137 |
| - Ok(()) |
| 79 | + other => return Err(anyhow!("invalid or unsupported fingerprint: {}", other)), |
138 | 80 | }
|
| 81 | + Ok(()) |
139 | 82 | }
|
140 | 83 |
|
141 |
| -#[derive(FromArgs, PartialEq, Debug)] |
142 |
| -/// Prints the canonical form of writer's schema encoded in the provided datafile. |
143 |
| -#[argh(subcommand, name = "canonical")] |
144 |
| -pub struct Canonical { |
145 |
| - /// datafile as input |
146 |
| - #[argh(option, short = 'd')] |
147 |
| - datafile: String, |
148 |
| -} |
149 |
| - |
150 |
| -impl Canonical { |
151 |
| - pub fn canonical(&self) -> Result<(), anyhow::Error> { |
152 |
| - let mut avro_datafile = read_datafile(&self.datafile)?; |
153 |
| - let header = Header::from_reader(&mut avro_datafile)?; |
154 |
| - println!("{}", header.schema().canonical_form()); |
155 |
| - Ok(()) |
156 |
| - } |
| 84 | +pub fn canonical(datafile: &PathBuf) -> Result<(), anyhow::Error> { |
| 85 | + let mut avro_datafile = read_datafile(datafile)?; |
| 86 | + let header = Header::from_reader(&mut avro_datafile)?; |
| 87 | + println!("{}", header.schema().canonical_form()); |
| 88 | + Ok(()) |
157 | 89 | }
|
0 commit comments