Skip to content

Commit

Permalink
added Default impls
Browse files Browse the repository at this point in the history
  • Loading branch information
cvhammond committed Sep 2, 2023
1 parent d749024 commit 628e427
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 95 deletions.
10 changes: 8 additions & 2 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub struct Bytes {
pub data_start_block_index: usize,
}

impl Bytes {
pub fn new() -> Bytes {
impl Default for Bytes {
fn default() -> Self {
Bytes {
header: [0; 512],
parameter: Vec::new(),
Expand All @@ -26,3 +26,9 @@ impl Bytes {
}
}
}

impl Bytes {
pub fn new() -> Bytes {
Bytes::default()
}
}
41 changes: 13 additions & 28 deletions src/c3d.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::parameters::{ParameterData, Parameters};

use crate::bytes::Bytes;
use crate::data::Data;
use crate::events::Events;
use crate::processor::Processor;
use crate::{C3d, C3dParseError};
Expand All @@ -21,7 +19,7 @@ impl PartialEq for C3d {
impl C3d {
/// Parses a C3D file from a file path.
pub fn load(file_name: &str) -> Result<C3d, C3dParseError> {
let c3d = C3d::new()?;
let c3d = C3d::new();
let (c3d, mut file) = c3d.open_file(file_name)?;
Ok(c3d
.parse_basic_info(&mut file)?
Expand All @@ -32,7 +30,7 @@ impl C3d {

/// Parses a C3D file from a byte slice.
pub fn from_bytes(bytes: &[u8]) -> Result<C3d, C3dParseError> {
Ok(C3d::new()?
Ok(C3d::new()
.parse_basic_info_from_bytes(bytes)?
.parse_header()?
.parse_parameters()?
Expand All @@ -41,7 +39,7 @@ impl C3d {

/// Parses a C3D file with just the header data.
pub fn load_header(file_name: &str) -> Result<C3d, C3dParseError> {
let c3d = C3d::new()?;
let c3d = C3d::new();
let (c3d, mut file) = c3d.open_file(file_name)?;
Ok(c3d.parse_basic_info(&mut file)?.parse_header()?)
}
Expand All @@ -51,27 +49,22 @@ impl C3d {
/// The parameter data is parsed into a `Parameters` struct.
/// The `Parameters` struct can be accessed via the `parameters` field.
pub fn load_parameters(file_name: &str) -> Result<C3d, C3dParseError> {
let c3d = C3d::new()?;
let c3d = C3d::new();
let (c3d, mut file) = c3d.open_file(file_name)?;
Ok(c3d.parse_basic_info(&mut file)?
Ok(c3d
.parse_basic_info(&mut file)?
.parse_header()?
.parse_parameters()?)
}

pub fn new() -> Result<C3d, C3dParseError> {
Ok(C3d {
file_path: None,
bytes: Bytes::new(),
parameters: Parameters::new(),
processor: Processor::new(),
data: Data::new(),
events: Events::new(),
})
pub fn new() -> C3d {
C3d::default()
}

fn open_file(mut self, file_name: &str) -> Result<(C3d, File), C3dParseError> {
self.file_path = Some(PathBuf::from(file_name));
let file = File::open(self.file_path.clone().unwrap()).map_err(|e| C3dParseError::ReadError(e))?;
let file =
File::open(self.file_path.clone().unwrap()).map_err(|e| C3dParseError::ReadError(e))?;
Ok((self, file))
}

Expand Down Expand Up @@ -211,12 +204,8 @@ impl C3d {

fn parse_data_bytes(mut self) -> Result<C3d, C3dParseError> {
self.data.point_frames = match self.parameters.get_data("POINT", "FRAMES") {
Some(ParameterData::Integer(frames, _)) => {
frames[0] as u16 as usize
}
Some(ParameterData::Float(frames, _)) => {
frames[0] as u16 as usize
}
Some(ParameterData::Integer(frames, _)) => frames[0] as u16 as usize,
Some(ParameterData::Float(frames, _)) => frames[0] as u16 as usize,
_ => 0,
};

Expand All @@ -231,7 +220,6 @@ impl C3d {
/// to determine where a file failed to load or parse.
#[derive(Debug, PartialEq)]
pub enum ProcessStep {
MakeEmpty,
LoadFile,
ParseBasicInfo,
ParseHeader,
Expand All @@ -244,10 +232,7 @@ pub enum ProcessStep {
/// This is not a comprehensive test, but it is a good first check.
/// It is not intended to be used as a unit test.
pub fn test_load_file(file_name: &str) -> ProcessStep {
let c3d = match C3d::new() {
Ok(c3d) => c3d,
Err(_) => return ProcessStep::MakeEmpty,
};
let c3d = C3d::new();
let (c3d, mut file) = match c3d.open_file(file_name) {
Ok(c3d) => c3d,
Err(_) => return ProcessStep::LoadFile,
Expand Down
10 changes: 8 additions & 2 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ pub enum DataFormat {
Unknown,
}

impl Data {
pub fn new() -> Data {
impl Default for Data {
fn default() -> Self {
Data {
points: DMatrix::from_vec(0, 0, vec![]),
cameras: DMatrix::from_vec(0, 0, vec![]),
Expand All @@ -84,6 +84,12 @@ impl Data {
analog_bytes_per_frame: 0,
}
}
}

impl Data {
pub fn new() -> Data {
Data::default()
}

fn calc_num_frames(
&mut self,
Expand Down
15 changes: 9 additions & 6 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,29 @@ use crate::processor::Processor;
use crate::C3dParseError;

/// The `Events` struct contains the events from the C3D file header.
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Events {
pub supports_events_labels: bool,
events: Vec<Event>,
}

/// The `Event` struct contains the information for a single event.
#[derive(Debug, Copy, Clone, PartialEq)]
#[derive(Debug, Copy, Clone, PartialEq, Default)]
struct Event {
pub label: [char; 4],
pub display_flag: bool,
pub time: f32,
}

impl Event {
pub fn new() -> Event {
Event::default()
}
}

impl Events {
pub fn new() -> Events {
Events {
supports_events_labels: false,
events: Vec::new(),
}
Events::default()
}

pub fn from_header_block(
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl fmt::Display for C3dParseError {
/// let c3d = C3d::load("tests/data/short.c3d");
/// assert!(c3d.is_ok());
/// ```
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct C3d {
pub file_path: Option<PathBuf>,
bytes: Bytes,
Expand Down
65 changes: 14 additions & 51 deletions src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::C3dParseError;
/// The `raw_parameters` field is a `HashMap` of `HashMap`s.
/// The first key is the group name, and the second key is the parameter name.
/// The value is a tuple of the parameter data and the description.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct Parameters {
pub group_descriptions: HashMap<String, String>,
pub raw_parameters: HashMap<String, HashMap<String, (ParameterData, String)>>,
Expand All @@ -26,18 +26,7 @@ pub struct Parameters {

impl Parameters {
pub fn new() -> Parameters {
Parameters {
group_descriptions: HashMap::new(),
raw_parameters: HashMap::new(),
point: PointParameters::default(),
analog: AnalogParameters::default(),
force_platform: ForcePlatformParameters::default(),
trial: TrialParameters::new(),
event: EventParameters::new(),
event_context: EventContextParameters::new(),
manufacturer: ManufacturerParameters::new(),
seg: SegParameters::default(),
}
Parameters::default()
}

pub fn parse_parameter_blocks(
Expand Down Expand Up @@ -286,16 +275,14 @@ impl AnalogParameters {
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ForcePlatformCorners {
corners: Vec<[f32; 12]>,
}

impl ForcePlatformCorners {
pub fn new() -> Self {
ForcePlatformCorners {
corners: Vec::new(),
}
ForcePlatformCorners::default()
}

pub fn from_raw(
Expand Down Expand Up @@ -333,14 +320,14 @@ impl ForcePlatformCorners {
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ForcePlatformOrigin {
origin: Vec<[f32; 3]>,
}

impl ForcePlatformOrigin {
pub fn new() -> Self {
ForcePlatformOrigin { origin: Vec::new() }
ForcePlatformOrigin::default()
}

pub fn from_raw(
Expand Down Expand Up @@ -411,7 +398,7 @@ impl ForcePlatformParameters {
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct TrialParameters {
pub actual_start_field: Option<usize>,
pub actual_end_field: Option<usize>,
Expand All @@ -420,11 +407,7 @@ pub struct TrialParameters {

impl TrialParameters {
pub fn new() -> Self {
TrialParameters {
actual_start_field: None,
actual_end_field: None,
camera_rate: None,
}
TrialParameters::default()
}

pub fn from_raw(
Expand Down Expand Up @@ -461,7 +444,7 @@ impl TrialParameters {
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct EventParameters {
pub used: Option<i16>,
pub contexts: Option<Vec<String>>,
Expand All @@ -475,16 +458,7 @@ pub struct EventParameters {

impl EventParameters {
pub fn new() -> Self {
EventParameters {
used: None,
contexts: None,
labels: None,
descriptions: None,
times: None,
subjects: None,
icon_ids: None,
generic_flags: None,
}
EventParameters::default()
}

pub fn from_raw(
Expand All @@ -503,7 +477,7 @@ impl EventParameters {
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct EventContextParameters {
pub used: Option<i16>,
pub icon_ids: Option<Vec<u16>>,
Expand All @@ -514,13 +488,7 @@ pub struct EventContextParameters {

impl EventContextParameters {
pub fn new() -> Self {
EventContextParameters {
used: None,
icon_ids: None,
labels: None,
descriptions: None,
colours: None,
}
EventContextParameters::default()
}

pub fn from_raw(
Expand All @@ -544,7 +512,7 @@ pub enum ManufacturerVersion {
Array(Vec<u16>),
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ManufacturerParameters {
pub company: Option<String>,
pub software: Option<String>,
Expand All @@ -554,12 +522,7 @@ pub struct ManufacturerParameters {

impl ManufacturerParameters {
pub fn new() -> Self {
ManufacturerParameters {
company: None,
software: None,
version: None,
edited: None,
}
ManufacturerParameters::default()
}

pub fn from_raw(
Expand Down
9 changes: 4 additions & 5 deletions src/processor.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
use crate::C3dParseError;

#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, Default)]
pub struct Processor {
pub processor_type: ProcessorType,
}

#[derive(Debug, Copy, Clone, PartialEq)]
#[derive(Debug, Copy, Clone, PartialEq, Default)]
pub enum ProcessorType {
Dec,
Intel,
SgiMips,
#[default]
Unknown,
}

impl Processor {
pub fn new() -> Processor {
Processor {
processor_type: ProcessorType::Unknown,
}
Processor::default()
}
pub fn from_parameter_start_block(
parameter_start_block: [u8; 512],
Expand Down

0 comments on commit 628e427

Please sign in to comment.