Skip to content

Commit

Permalink
add resource and description fields for TypeDef
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Jul 28, 2023
1 parent 53938b1 commit e2bb0f0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/extensions/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::collections::HashMap;
use pyo3::prelude::*;

use crate::ops::constant::CustomConst;
use crate::resource::{OpDef, ResourceSet, TypeDef};
use crate::resource::{OpDef, ResourceId, ResourceSet, TypeDef};
use crate::types::type_param::TypeArg;
use crate::types::{CustomType, SimpleRow, SimpleType};
use crate::Resource;
Expand Down Expand Up @@ -58,6 +58,13 @@ impl Type {
}
}

pub const fn description(&self) -> &str {
match self {
Type::Angle => "Floating point angle",
Type::Quaternion => "Quaternion specifying rotation.",
}
}

pub fn custom_type(self) -> CustomType {
CustomType::new(self.name(), [])
}
Expand All @@ -66,6 +73,8 @@ impl Type {
TypeDef {
name: self.name(),
params: vec![],
description: self.description().to_string(),
resource: ResourceId::default(),
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ pub struct TypeDef {
///
/// [`TypeArg`]: crate::types::type_param::TypeArg
pub params: Vec<TypeParam>,
/// The unique Resource owning this TypeDef (of which this TypeDef is a member)
pub resource: ResourceId,
/// Human readable description of the type definition.
pub description: String,
}

/// A unique identifier for a resource.
Expand Down Expand Up @@ -344,18 +348,24 @@ impl Resource {
}

/// Add an exported type to the resource.
pub fn add_type(&mut self, ty: TypeDef) {
pub fn add_type(&mut self, ty: TypeDef) -> Result<(), String> {
if ty.resource != ResourceId::default() {
return Err(format!(
"TypeDef {} owned by another resource {}",
ty.name, ty.resource
));
}
match self.types.entry(ty.name.clone()) {
Entry::Occupied(_) => panic!("Resource already has a type called {}", &ty.name),
Entry::Vacant(ve) => {
ve.insert(ty);
}
}
Ok(())
}

/// Add an operation definition to the resource.
pub fn add_op(&mut self, mut op: OpDef) -> Result<(), String> {
// if op.resource != self.name {
if op.resource != ResourceId::default() {
return Err(format!(
"OpDef {} owned by another resource {}",
Expand Down

0 comments on commit e2bb0f0

Please sign in to comment.