Skip to content

Commit

Permalink
libdonet: Skeleton for DC field declaration elements
Browse files Browse the repository at this point in the history
i need new adhd meds. i am barely focusing on all things in life rn
  • Loading branch information
maxrdz committed Feb 18, 2024
1 parent 19f2fed commit b45854e
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 21 deletions.
8 changes: 4 additions & 4 deletions libdonet/src/dcarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::dctype::{DCTypeDefinition, DCTypeDefinitionInterface, DCTypeEnum};
use crate::hashgen::DCHashGenerator;

pub struct DCArrayType {
parent: DCTypeDefinition,
_dcarray_parent: DCTypeDefinition,
element_type: Option<DCTypeDefinition>,
array_size: u16,
array_range: Option<DCNumericRange>,
Expand All @@ -40,7 +40,7 @@ pub trait DCArrayTypeInterface {
impl DCArrayTypeInterface for DCArrayType {
fn new(element_type: Option<DCTypeDefinition>, size: Option<DCNumericRange>) -> Self {
let mut new_array_type: Self = Self {
parent: DCTypeDefinition::new(),
_dcarray_parent: DCTypeDefinition::new(),
element_type: element_type,
array_size: 0_u16,
array_range: size,
Expand Down Expand Up @@ -137,11 +137,11 @@ impl DCArrayTypeInterface for DCArrayType {
impl std::ops::Deref for DCArrayType {
type Target = DCTypeDefinition;
fn deref(&self) -> &Self::Target {
&self.parent
&self._dcarray_parent
}
}
impl std::ops::DerefMut for DCArrayType {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.parent
&mut self._dcarray_parent
}
}
30 changes: 30 additions & 0 deletions libdonet/src/dcatomic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// DONET SOFTWARE
// Copyright (c) 2024, Donet Authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License version 3.
// You should have received a copy of this license along
// with this source code in a file named "LICENSE."
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use crate::dcfield::DCField;

struct DCAtomicField {
_dcatomicfield_parent: DCField,
}

/// See issue #22.
impl std::ops::Deref for DCAtomicField {
type Target = DCField;
fn deref(&self) -> &Self::Target {
&self._dcatomicfield_parent
}
}
54 changes: 47 additions & 7 deletions libdonet/src/dcfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use crate::datagram::Datagram;
use crate::dckeyword::{DCKeywordList, DCKeywordListInterface, IdentifyKeyword};
use crate::dclass::DClass;
use crate::dcstruct::DCStruct;
use crate::dctype::{DCTypeDefinition, DCTypeDefinitionInterface};
use crate::globals;
use crate::hashgen::DCHashGenerator;
use std::sync::{Arc, Mutex};

/// A field of a Distributed Class. The DCField struct is a base for
/// struct and dclass fields. In the DC language, there are three types
/// of field declarations, which are: parameter, atomic, and molecular.
#[derive(Debug)]
pub struct DCField {
parent: DCKeywordList,
class: Option<Arc<Mutex<DClass>>>,
_dcfield_parent: DCKeywordList,
dclass: Option<Arc<Mutex<DClass>>>,
_struct: Option<Arc<Mutex<DCStruct>>>, // needs '_' due to reserved keyword
field_name: String,
field_id: globals::FieldId,
field_type: DCTypeDefinition,
parent_is_dclass: bool,
default_value_stale: bool,
has_default_value: bool,
Expand All @@ -38,14 +44,21 @@ pub struct DCField {

pub trait DCFieldInterface {
fn new(name: &str, id: globals::FieldId) -> Self;
fn generate_hash(&self, hashgen: &mut DCHashGenerator);
fn dcfield_generate_hash(&self, hashgen: &mut DCHashGenerator);

fn get_field_id(&self) -> globals::FieldId;
fn get_dclass(&self) -> Arc<Mutex<DClass>>;

fn set_field_id(&mut self, id: globals::FieldId);
fn set_field_name(&mut self, name: String);
fn set_default_value(&mut self, value: Vec<u8>);

fn set_parent_struct(&mut self, parent: Arc<Mutex<DCStruct>>);
fn set_parent_dclass(&mut self, parent: Arc<Mutex<DClass>>);

fn has_default_value(&self) -> bool;
fn validate_ranges(&self, packed_data: &Datagram) -> bool;

// Inline functions for Panda historical keywords
fn is_required(&self) -> bool;
fn is_broadcast(&self) -> bool;
Expand All @@ -58,14 +71,21 @@ pub trait DCFieldInterface {
fn is_airecv(&self) -> bool;
}

impl DCField {
fn refresh_default_value(&self) {
todo!()
}
}

impl DCFieldInterface for DCField {
fn new(name: &str, id: globals::FieldId) -> Self {
Self {
parent: DCKeywordList::new(),
class: None,
_dcfield_parent: DCKeywordList::new(),
dclass: None,
_struct: None,
field_name: name.to_owned(),
field_id: id,
field_type: DCTypeDefinition::new(),
parent_is_dclass: false,
default_value_stale: false,
has_default_value: false,
Expand All @@ -74,11 +94,19 @@ impl DCFieldInterface for DCField {
}
}

fn generate_hash(&self, hashgen: &mut DCHashGenerator) {
fn dcfield_generate_hash(&self, hashgen: &mut DCHashGenerator) {
self.dckeywordlist_generate_hash(hashgen);
// TODO!
}

fn get_field_id(&self) -> globals::FieldId {
todo!()
}

fn get_dclass(&self) -> Arc<Mutex<DClass>> {
todo!()
}

#[inline(always)]
fn set_field_id(&mut self, id: globals::FieldId) {
self.field_id = id
Expand All @@ -89,6 +117,10 @@ impl DCFieldInterface for DCField {
self.field_name = name
}

fn set_default_value(&mut self, value: Vec<u8>) {
todo!()
}

fn set_parent_struct(&mut self, parent: Arc<Mutex<DCStruct>>) {
todo!()
}
Expand All @@ -97,6 +129,14 @@ impl DCFieldInterface for DCField {
todo!()
}

fn has_default_value(&self) -> bool {
todo!()
}

fn validate_ranges(&self, packed_data: &Datagram) -> bool {
todo!()
}

#[inline(always)]
fn is_required(&self) -> bool {
self.has_keyword(IdentifyKeyword::ByName("required".to_owned()))
Expand Down Expand Up @@ -148,6 +188,6 @@ impl DCFieldInterface for DCField {
impl std::ops::Deref for DCField {
type Target = DCKeywordList;
fn deref(&self) -> &Self::Target {
&self.parent
&self._dcfield_parent
}
}
8 changes: 4 additions & 4 deletions libdonet/src/dclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ pub trait DClassInterface {
fn set_parent(&mut self, parent: Arc<Mutex<DClass>>);

fn get_name(&mut self) -> String;
fn get_class_id(&mut self) -> globals::DClassId;
fn set_class_id(&mut self, id: globals::DClassId);
fn get_dclass_id(&mut self) -> globals::DClassId;
fn set_dclass_id(&mut self, id: globals::DClassId);
fn get_num_parents(&mut self) -> usize;
fn get_parent(&mut self, index: usize) -> Option<Arc<Mutex<DClass>>>;
fn has_constructor(&mut self) -> bool;
Expand Down Expand Up @@ -82,11 +82,11 @@ impl DClassInterface for DClass {
self.class_name.clone()
}

fn get_class_id(&mut self) -> globals::DClassId {
fn get_dclass_id(&mut self) -> globals::DClassId {
self.class_id
}

fn set_class_id(&mut self, id: globals::DClassId) {
fn set_dclass_id(&mut self, id: globals::DClassId) {
self.class_id = id;
}

Expand Down
30 changes: 30 additions & 0 deletions libdonet/src/dcmolecular.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// DONET SOFTWARE
// Copyright (c) 2024, Donet Authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License version 3.
// You should have received a copy of this license along
// with this source code in a file named "LICENSE."
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use crate::dcfield::DCField;

struct DCMolecularField {
_dcmolecularfield_parent: DCField,
}

/// See issue #22.
impl std::ops::Deref for DCMolecularField {
type Target = DCField;
fn deref(&self) -> &Self::Target {
&self._dcmolecularfield_parent
}
}
6 changes: 3 additions & 3 deletions libdonet/src/dcnumeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl DCNumericRange {
// ---------- Numeric Type ---------- //

pub struct DCNumericType {
parent: DCTypeDefinition,
_dcnumeric_parent: DCTypeDefinition,
divisor: u16,
// These are the original range and modulus values from the file, unscaled by the divisor.
orig_modulus: f64,
Expand Down Expand Up @@ -161,7 +161,7 @@ impl DCNumericType {
impl DCNumericTypeInterface for DCNumericType {
fn new(base_type: DCTypeEnum) -> Self {
Self {
parent: {
_dcnumeric_parent: {
let mut parent_struct = DCTypeDefinition::new();
parent_struct.data_type = base_type;

Expand Down Expand Up @@ -269,6 +269,6 @@ impl DCNumericTypeInterface for DCNumericType {
impl std::ops::Deref for DCNumericType {
type Target = DCTypeDefinition;
fn deref(&self) -> &Self::Target {
&self.parent
&self._dcnumeric_parent
}
}
30 changes: 30 additions & 0 deletions libdonet/src/dcparameter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// DONET SOFTWARE
// Copyright (c) 2024, Donet Authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License version 3.
// You should have received a copy of this license along
// with this source code in a file named "LICENSE."
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use crate::dcfield::DCField;

struct DCParameterField {
_dcparameterfield_parent: DCField,
}

/// See issue #22.
impl std::ops::Deref for DCParameterField {
type Target = DCField;
fn deref(&self) -> &Self::Target {
&self._dcparameterfield_parent
}
}
2 changes: 1 addition & 1 deletion libdonet/src/dcparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ parser! {
use dclass::DClassInterface;

let next_class_id: usize = dc_file.get_num_dclasses();
dclass.set_class_id(next_class_id.try_into().unwrap());
dclass.set_dclass_id(next_class_id.try_into().unwrap());

dc_file.add_dclass(dclass);
},
Expand Down
4 changes: 2 additions & 2 deletions libdonet/src/dctype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use strum_macros::EnumIs;
* to keep compatibility with Astron's DC hash inputs.
*/
#[repr(u8)] // 8-bit alignment, unsigned
#[derive(Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
#[rustfmt::skip]
pub enum DCTypeEnum {
// Numeric Types
Expand All @@ -43,7 +43,7 @@ pub enum DCTypeEnum {
TInvalid = 21,
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct DCTypeDefinition {
alias: Option<String>,
pub data_type: DCTypeEnum,
Expand Down
3 changes: 3 additions & 0 deletions libdonet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ cfg_if! {
cfg_if! {
if #[cfg(feature = "dcfile")] {
pub mod dcarray;
pub mod dcatomic;
pub mod dcfield;
pub mod dcfile;
pub mod dckeyword;
pub mod dclass;
pub mod dclexer;
pub mod dcmolecular;
pub mod dcnumeric;
pub mod dcparameter;
pub mod dcparser;
pub mod dcstruct;
pub mod dctype;
Expand Down

0 comments on commit b45854e

Please sign in to comment.