From 1776aa854793ce8cf03f4626b3456450cb4dccd8 Mon Sep 17 00:00:00 2001 From: Marius Kriegerowski Date: Tue, 18 May 2021 20:33:48 +0200 Subject: [PATCH 1/5] add pycharm's idea file to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 61b1936..ef5e4b0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ **/*.rs.bk /Cargo.lock /src/libmseed + +# pycharm +.idea From d2514de2ef8b39da38582905b6d163d37e3a6904 Mon Sep 17 00:00:00 2001 From: Marius Kriegerowski Date: Tue, 18 May 2021 20:36:31 +0200 Subject: [PATCH 2/5] run cargo fmt in ci and as pre-commit hook --- .github/workflows/rust.yml | 2 ++ Cargo.toml | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 3c13d1b..1607f93 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -20,3 +20,5 @@ jobs: run: cargo build --verbose - name: Run tests run: cargo test --verbose + - name: Linter + run: cargo fmt --all -- --check diff --git a/Cargo.toml b/Cargo.toml index ca535e6..bca4f79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,8 +16,12 @@ chrono = "0.4" glob = "0.2" libc = "0.2" num = "0.1" +pre-commit = "0.5.2" [build-dependencies] git2 = "0.13" bindgen = "0.58.1" +[package.metadata.precommit] +fmt = "cargo fmt --all -- --check 2>&1" + From 84e26c1246990c6c0875255b0fd68217b7c491e5 Mon Sep 17 00:00:00 2001 From: Marius Kriegerowski Date: Tue, 18 May 2021 20:57:12 +0200 Subject: [PATCH 3/5] run cargo fmt --- build.rs | 13 +--- src/lib.rs | 205 +++++++++++++++++++++++++++++-------------------- tests/parse.rs | 3 +- tests/read.rs | 8 +- 4 files changed, 131 insertions(+), 98 deletions(-) diff --git a/build.rs b/build.rs index d02cbc5..661ffd5 100644 --- a/build.rs +++ b/build.rs @@ -1,11 +1,9 @@ - - -extern crate git2; extern crate bindgen; +extern crate git2; -use std::path::{Path, PathBuf}; use git2::Repository; use std::env; +use std::path::{Path, PathBuf}; use std::process::Command; const REPO_URL: &str = "https://github.com/iris-edu/libmseed"; @@ -15,7 +13,7 @@ const GIT_REF: &str = "refs/remotes/origin/2.x"; // libmseed v3 broke the ABI fn fetch_libmseed(dir: &str) { println!("fetching libmseed"); let repo = match Repository::clone(REPO_URL, dir) { - Ok( creepo ) => { creepo }, + Ok(creepo) => creepo, Err(e) => panic!("Failed to update/clone repo: {}", e), }; @@ -33,7 +31,6 @@ fn make_libmseed(dir: &str) { } fn main() { - if !Path::new(BUILD_DIR).is_dir() { fetch_libmseed(BUILD_DIR); } @@ -51,7 +48,7 @@ fn main() { let path = env::current_dir().unwrap(); println!("The current directory is {}", path.display()); - let path : PathBuf = [BUILD_DIR, "libmseed.h"].iter().collect(); + let path: PathBuf = [BUILD_DIR, "libmseed.h"].iter().collect(); if !path.exists() { panic!("libmseed header file: libmseed.h does not exist"); } @@ -76,5 +73,3 @@ fn main() { .write_to_file(out_path.join("bindings.rs")) .expect("couldn't write bindings"); } - - diff --git a/src/lib.rs b/src/lib.rs index 1bde49c..87c07b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,3 @@ - //! MiniSEED Library for rust //! //! This is an wrapper around the IRIS libmseed library at @@ -36,16 +35,16 @@ extern crate chrono; extern crate libc; extern crate num; -use chrono::Utc; -use chrono::NaiveDate; -use chrono::NaiveDateTime; use chrono::DateTime; use chrono::Duration; +use chrono::NaiveDate; +use chrono::NaiveDateTime; +use chrono::Utc; //use chrono::Timelike; +use libc::{c_char, c_int, c_void}; use std::ffi::CString; use std::path::Path; -use libc::{c_char, c_int, c_void}; use std::fs::File; use std::io::Write; @@ -71,7 +70,6 @@ pub struct ms_record(*mut MSRecord); // #[derive(Debug)] // pub struct ms_trace(*mut MSTrace); - // macro_rules! trace_loop { // ($name:ident, $tfunc:ident, $vfunc:ident, $t:ty) => { // pub fn $name(&self) -> Option<$t> { @@ -90,18 +88,30 @@ pub struct ms_record(*mut MSRecord); // } macro_rules! cast { - ($x:ident, $t:ty) => { ($x as *mut _) as *mut $t }; - (ptr, $x:ident, $t:ty) => { ((&mut $x) as *mut _) as *mut *mut $t }; + ($x:ident, $t:ty) => { + ($x as *mut _) as *mut $t + }; + (ptr, $x:ident, $t:ty) => { + ((&mut $x) as *mut _) as *mut *mut $t + }; } pub fn fmin(v: &[T]) -> T { let mut vf = v[0]; - for vi in v { if *vi < vf { vf = *vi; } } + for vi in v { + if *vi < vf { + vf = *vi; + } + } vf } pub fn fmax(v: &[T]) -> T { let mut vf = v[0]; - for vi in v { if *vi > vf { vf = *vi; } } + for vi in v { + if *vi > vf { + vf = *vi; + } + } vf } // fn tmin(v: &[DateTime]) -> DateTime { @@ -122,8 +132,8 @@ pub fn utc_to_f64(t: &DateTime) -> f64 { pub fn f64_to_utc(t: f64) -> DateTime { let i = t.trunc() as i64; let f = (t.fract() * 1e9) as u32; - let t = NaiveDateTime::from_timestamp(i,f); - DateTime::::from_utc(t,Utc) + let t = NaiveDateTime::from_timestamp(i, f); + DateTime::::from_utc(t, Utc) } // fn tmax_to_f64(v: &[DateTime]) -> f64 { @@ -253,7 +263,6 @@ pub fn f64_to_utc(t: f64) -> DateTime { // } // } - // impl ms_group { // pub fn new() -> ms_group { // let null = std::ptr::null_mut() as *mut MSTraceGroup; @@ -371,14 +380,25 @@ impl BTime { pub fn as_mut_ptr(&mut self) -> *mut BTime { self as *mut BTime } - pub fn zero() -> BTime { - BTime { year:0,day:0,hour:0,min:0,sec:0,fract:0,unused:0} + pub fn zero() -> BTime { + BTime { + year: 0, + day: 0, + hour: 0, + min: 0, + sec: 0, + fract: 0, + unused: 0, + } } pub fn to_datetime(&self) -> DateTime { // Convert Year/DayOfYear and Hour/Minute/Second/MicroSecond to DateTime - let d = NaiveDate::from_yo(self.year as i32, self.day as u32) - .and_hms_micro(self.hour as u32, self.min as u32, - self.sec as u32, self.fract as u32 *100); + let d = NaiveDate::from_yo(self.year as i32, self.day as u32).and_hms_micro( + self.hour as u32, + self.min as u32, + self.sec as u32, + self.fract as u32 * 100, + ); // Convert to UTC DateTime DateTime::::from_utc(d, Utc) } @@ -391,7 +411,7 @@ pub enum Data<'a> { Ascii(&'a [u8]), } -impl <'a> Data<'a> { +impl<'a> Data<'a> { pub fn to_f64(&self) -> Vec { match self { &Data::Int(y) => y.iter().map(|&i| i as f64).collect(), @@ -409,16 +429,16 @@ pub struct ms_input { impl ms_input { pub fn open>(file: S) -> ms_input { - let sfile : String = file.as_ref().to_string_lossy().into_owned(); + let sfile: String = file.as_ref().to_string_lossy().into_owned(); let cfile = CString::new(sfile).unwrap(); return ms_input { _filename: cfile, pmsfp: std::ptr::null_mut() as *mut MSFileParam, - } + }; } pub fn filename(&self) -> &str { - return self._filename.to_str().unwrap() + return self._filename.to_str().unwrap(); } } @@ -446,7 +466,7 @@ unsafe extern "C" fn pack_handler_wrapper(buffer: *mut c_char, buflen: c_int, pt impl ms_output { pub fn open>(filename: S) -> std::io::Result { - return File::create(filename).map(|fh| ms_output {file: fh}) + return File::create(filename).map(|fh| ms_output { file: fh }); } pub fn write(&mut self, record: &ms_record) { @@ -454,8 +474,14 @@ impl ms_output { let rec_ptr: *const MSRecord = &(record.ptr()); let rec_mut_ptr: *mut MSRecord = rec_ptr as *mut MSRecord_s; unsafe { - msr_pack(rec_mut_ptr, Some(pack_handler_wrapper), ptr, - std::ptr::null_mut(), 1, 0); + msr_pack( + rec_mut_ptr, + Some(pack_handler_wrapper), + ptr, + std::ptr::null_mut(), + 1, + 0, + ); } } } @@ -479,39 +505,41 @@ impl ms_record { /// assert_eq!(rec.to_string(), "PN_PPNAF_00_HHZ, 1, D, 512, 206 samples, 100 Hz, 2016-10-30 18:02:58.230 UTC"); /// ``` pub fn read(file: S) -> ms_record - where S: AsRef + where + S: AsRef, { - let sfile : String = file.as_ref().to_string_lossy().into_owned(); + let sfile: String = file.as_ref().to_string_lossy().into_owned(); let cfile = CString::new(sfile).unwrap(); let mut pmsfp = std::ptr::null_mut() as *mut MSFileParam; return ms_record::read_next(&cfile, &mut pmsfp).unwrap(); } - pub fn read_next(file: &CString, pmsfp: &mut *mut MSFileParam) -> Option - { - let verbose : flag = 1; - let dataflag : flag = 1; - let skipnotdata : flag = 1; + pub fn read_next(file: &CString, pmsfp: &mut *mut MSFileParam) -> Option { + let verbose: flag = 1; + let dataflag: flag = 1; + let skipnotdata: flag = 1; let mut pmsr = ms_record::null(); let retcode = unsafe { // WTF: https://github.com/rust-lang/rust/issues/17417 - ms_readmsr_r ( ((pmsfp) as *mut _) as *mut *mut MSFileParam, - ((&mut pmsr) as *mut _) as *mut *mut MSRecord, - file.as_ptr(), - 0, - std::ptr::null_mut(), // fpos - std::ptr::null_mut(), // last - skipnotdata, - dataflag, - verbose) + ms_readmsr_r( + ((pmsfp) as *mut _) as *mut *mut MSFileParam, + ((&mut pmsr) as *mut _) as *mut *mut MSRecord, + file.as_ptr(), + 0, + std::ptr::null_mut(), // fpos + std::ptr::null_mut(), // last + skipnotdata, + dataflag, + verbose, + ) }; if retcode == MS_NOERROR as i32 { return Some(ms_record(pmsr)); } else if retcode == MS_ENDOFFILE as i32 { - return None + return None; } else { panic!("readmsr_r retcode: {}", retcode) } @@ -527,7 +555,7 @@ impl ms_record { /// ``` pub fn header(&self) -> fsdh_s { let m = self.ptr(); - unsafe { * (m.fsdh as *mut fsdh_s) as fsdh_s } + unsafe { *(m.fsdh as *mut fsdh_s) as fsdh_s } } /// Return the network code @@ -580,7 +608,7 @@ impl ms_record { let n = self.npts(); let b = self.start(); let dt = self.delta(); - b + Duration::microseconds( ( (n-1) as f64 * dt * 1e6) as i64 ) + b + Duration::microseconds(((n - 1) as f64 * dt * 1e6) as i64) } /// Return the time of the next sample beyond the record /// assuming a constant sample rate @@ -595,7 +623,7 @@ impl ms_record { let n = self.npts(); let b = self.start(); let dt = self.delta(); - b + Duration::microseconds( ( n as f64 * dt * 1e6) as i64 ) + b + Duration::microseconds((n as f64 * dt * 1e6) as i64) } /// Return the sample rate /// @@ -610,7 +638,7 @@ impl ms_record { 1.0 / m.samprate } /// Return the data sample type - /// + /// /// - c - Character data /// - i - i32 data /// - f - f32 data @@ -670,35 +698,38 @@ impl ms_record { let n = self.npts(); let y = match self.dtype() { 'i' => { - let y : &[i32] = unsafe {from_raw_parts_mut(p as *mut i32, n) }; + let y: &[i32] = unsafe { from_raw_parts_mut(p as *mut i32, n) }; Data::Int(y) - }, + } 'f' => { - let y : &[f32] = unsafe { from_raw_parts_mut(p as *mut f32, n) }; + let y: &[f32] = unsafe { from_raw_parts_mut(p as *mut f32, n) }; Data::Float(y) - }, + } 'd' => { - let y : &[f64] = unsafe { from_raw_parts_mut(p as *mut f64, n) }; + let y: &[f64] = unsafe { from_raw_parts_mut(p as *mut f64, n) }; Data::Double(y) - }, + } 'a' => { - let y : &[u8] = unsafe { from_raw_parts_mut(p as *mut u8, n) }; + let y: &[u8] = unsafe { from_raw_parts_mut(p as *mut u8, n) }; Data::Ascii(y) } _ => { println!("Unknown data type: {}", self.dtype()); return None; - }, + } }; Some(y) } fn check_data_type(&self, want: char) { if self.dtype() != want { - panic!("Incorrect data type: requested: '{}, current: '{}'", - want, self.dtype()); + panic!( + "Incorrect data type: requested: '{}, current: '{}'", + want, + self.dtype() + ); } } - + /// Return the data as f64 /// /// ```panic @@ -730,7 +761,7 @@ impl ms_record { unsafe { from_raw_parts_mut(p as *mut f32, n) } } /// Return the data as i32 - /// + /// /// ``` /// # use miniseed::ms_record; /// let file = "tests/sample.miniseed"; @@ -754,7 +785,7 @@ impl ms_record { /// ``` pub fn min(&self) -> f64 { match self.data_type() { - 'i' => *self.data_i32().iter().min().unwrap() as f64, + 'i' => *self.data_i32().iter().min().unwrap() as f64, 'f' => fmin(self.data_f32()) as f64, 'd' => fmin(self.data_f64()) as f64, 'a' => panic!("attempt to take min of ascii data"), @@ -771,7 +802,7 @@ impl ms_record { /// ``` pub fn max(&self) -> f64 { match self.data_type() { - 'i' => *self.data_i32().iter().max().unwrap() as f64, + 'i' => *self.data_i32().iter().max().unwrap() as f64, 'f' => fmax(self.data_f32()) as f64, 'd' => fmax(self.data_f64()) as f64, 'a' => panic!("attempt to take min of ascii data"), @@ -800,7 +831,7 @@ impl ms_record { /// # use miniseed::ms_record; /// use std::fs::File; /// use std::io::Read; - /// + /// /// let mut file = File::open("tests/sample.miniseed").unwrap(); /// let mut buf = vec![]; /// let _ = file.read_to_end(&mut buf).unwrap(); @@ -809,10 +840,10 @@ impl ms_record { /// assert_eq!(rec.to_string(), "PN_PPNAF_00_HHZ, 1, D, 512, 206 samples, 100 Hz, 2016-10-30 18:02:58.230 UTC"); /// ``` pub fn parse(record: &[u8]) -> ms_record { - let verbose = 1; - let data = 1; + let verbose = 1; + let data = 1; - // Copy Data + // Copy Data let mut rec = record.to_vec(); // Get Pointer to memory slice let prec = &mut rec[..]; @@ -827,51 +858,61 @@ impl ms_record { if ret != MS_NOERROR as i32 { println!("retcode: {}", ret); } - ms_record( pmsr ) + ms_record(pmsr) } // Get Character data, if available pub fn as_string(&self) -> Option { match self.data() { Some(Data::Ascii(x)) => Some(String::from_utf8(x.to_vec()).unwrap()), - _ => None + _ => None, } } - } fn i8_to_string(vin: &[i8]) -> String { - let v : Vec = vin.iter() - .map(|x| *x as u8) // cast i8 as u8 + let v: Vec = vin + .iter() + .map(|x| *x as u8) // cast i8 as u8 .filter(|x| *x != 0u8) // remove null terminators .collect(); - String::from_utf8(v).unwrap() // convert to string + String::from_utf8(v).unwrap() // convert to string } use std::fmt; impl fmt::Display for ms_record { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let v = self.ptr(); let s = self.id(); - write!(f, "{}, {}, {}, {}, {} samples, {} Hz, {}", s, v.sequence_number, - v.dataquality as u8 as char, v.reclen, v.samplecnt, - v.samprate, self.start()) + write!( + f, + "{}, {}, {}, {}, {} samples, {} Hz, {}", + s, + v.sequence_number, + v.dataquality as u8 as char, + v.reclen, + v.samplecnt, + v.samprate, + self.start() + ) } } impl Drop for ms_record { fn drop(&mut self) { //println!("Dropping ms_record: {:?}!", self.ptr); unsafe { - ms_readmsr (&mut (self.0 as *mut _), - std::ptr::null_mut(), - 0, - std::ptr::null_mut(), - std::ptr::null_mut(), - 0, 0, 0); + ms_readmsr( + &mut (self.0 as *mut _), + std::ptr::null_mut(), + 0, + std::ptr::null_mut(), + std::ptr::null_mut(), + 0, + 0, + 0, + ); } } } #[cfg(test)] -mod tests { - -} +mod tests {} diff --git a/tests/parse.rs b/tests/parse.rs index f041e7a..c5df228 100644 --- a/tests/parse.rs +++ b/tests/parse.rs @@ -1,4 +1,3 @@ - extern crate miniseed; use miniseed::ms_record; @@ -9,7 +8,7 @@ fn parse() { let mut file = std::fs::File::open("tests/ff00b5d8b3124f1aa2de549070709634").unwrap(); let mut buf = vec![]; let _ = file.read_to_end(&mut buf).unwrap(); - buf.drain(..8); // First 8 bytes are the seedlink header + buf.drain(..8); // First 8 bytes are the seedlink header let r = ms_record::parse(&buf); println!("{}", r); } diff --git a/tests/read.rs b/tests/read.rs index 26b778f..7f8e902 100644 --- a/tests/read.rs +++ b/tests/read.rs @@ -1,8 +1,7 @@ - -extern crate miniseed; extern crate glob; +extern crate miniseed; -use miniseed::{ms_record, ms_input, ms_output}; +use miniseed::{ms_input, ms_output, ms_record}; #[test] fn read() { @@ -21,7 +20,7 @@ fn read() { #[test] fn read_multiple() { let input = ms_input::open("tests/multiple.seed"); - let ms : Vec<_> = input.collect(); + let ms: Vec<_> = input.collect(); for m in &ms { println!("{}", m); } @@ -31,5 +30,4 @@ fn read_multiple() { for m in &ms { out.write(m); } - } From 93936d95e24ac55686d87ae25091bccaf4a2152b Mon Sep 17 00:00:00 2001 From: Brian Savage Date: Tue, 18 May 2021 15:43:44 -0400 Subject: [PATCH 4/5] Modified to remove automatic cargo fmt --- .github/workflows/rust.yml | 3 +-- Cargo.toml | 3 --- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 1607f93..f0a113c 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -20,5 +20,4 @@ jobs: run: cargo build --verbose - name: Run tests run: cargo test --verbose - - name: Linter - run: cargo fmt --all -- --check + diff --git a/Cargo.toml b/Cargo.toml index bca4f79..df09937 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,12 +16,9 @@ chrono = "0.4" glob = "0.2" libc = "0.2" num = "0.1" -pre-commit = "0.5.2" [build-dependencies] git2 = "0.13" bindgen = "0.58.1" -[package.metadata.precommit] -fmt = "cargo fmt --all -- --check 2>&1" From b75f0e5a27fb583a2ac56f9e1b54bbe17d6f8b74 Mon Sep 17 00:00:00 2001 From: Brian Savage Date: Tue, 18 May 2021 15:45:13 -0400 Subject: [PATCH 5/5] Remove empty newlines at the end of a file --- .github/workflows/rust.yml | 1 - Cargo.toml | 1 - 2 files changed, 2 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index f0a113c..3c13d1b 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -20,4 +20,3 @@ jobs: run: cargo build --verbose - name: Run tests run: cargo test --verbose - diff --git a/Cargo.toml b/Cargo.toml index df09937..ca535e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,3 @@ num = "0.1" git2 = "0.13" bindgen = "0.58.1" -