Skip to content

Commit

Permalink
new helpers here and there
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Jun 20, 2023
1 parent 1041706 commit 2f90d9f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
6 changes: 3 additions & 3 deletions crates/re_types_builder/src/codegen/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl CodeGenerator for PythonCodeGenerator {
datatypes_path,
arrow_registry,
objs,
&objs.ordered_datatypes(),
&objs.ordered_objects(ObjectKind::Datatype.into()),
)
.0,
);
Expand All @@ -57,7 +57,7 @@ impl CodeGenerator for PythonCodeGenerator {
components_path,
arrow_registry,
objs,
&objs.ordered_components(),
&objs.ordered_objects(ObjectKind::Component.into()),
)
.0,
);
Expand All @@ -70,7 +70,7 @@ impl CodeGenerator for PythonCodeGenerator {
archetypes_path,
arrow_registry,
objs,
&objs.ordered_archetypes(),
&objs.ordered_objects(ObjectKind::Archetype.into()),
);
filepaths.extend(paths);

Expand Down
6 changes: 3 additions & 3 deletions crates/re_types_builder/src/codegen/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl CodeGenerator for RustCodeGenerator {
filepaths.extend(create_files(
datatypes_path,
arrow_registry,
&objects.ordered_datatypes(),
&objects.ordered_objects(ObjectKind::Datatype.into()),
));

let components_path = self.crate_path.join("src/components");
Expand All @@ -51,7 +51,7 @@ impl CodeGenerator for RustCodeGenerator {
filepaths.extend(create_files(
components_path,
arrow_registry,
&objects.ordered_components(),
&objects.ordered_objects(ObjectKind::Component.into()),
));

let archetypes_path = self.crate_path.join("src/archetypes");
Expand All @@ -61,7 +61,7 @@ impl CodeGenerator for RustCodeGenerator {
filepaths.extend(create_files(
archetypes_path,
arrow_registry,
&objects.ordered_archetypes(),
&objects.ordered_objects(ObjectKind::Archetype.into()),
));

filepaths
Expand Down
68 changes: 55 additions & 13 deletions crates/re_types_builder/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,18 @@ impl Objects {
.unwrap()
}

/// Returns all available datatypes, pre-sorted in ascending order based on their `order`
/// Returns all available objects, pre-sorted in ascending order based on their `order`
/// attribute.
pub fn ordered_datatypes(&self) -> Vec<&Object> {
self.ordered_objects(ObjectKind::Datatype.into())
}
pub fn ordered_objects_mut(&mut self, kind: Option<ObjectKind>) -> Vec<&mut Object> {
let objs = self
.objects
.values_mut()
.filter(|obj| kind.map_or(true, |kind| obj.kind == kind));

/// Returns all available components, pre-sorted in ascending order based on their `order`
/// attribute.
pub fn ordered_components(&self) -> Vec<&Object> {
self.ordered_objects(ObjectKind::Component.into())
}
let mut objs = objs.collect::<Vec<_>>();
objs.sort_by_key(|anyobj| anyobj.order());

/// Returns all available archetypes, pre-sorted in ascending order based on their `order`
/// attribute.
pub fn ordered_archetypes(&self) -> Vec<&Object> {
self.ordered_objects(ObjectKind::Archetype.into())
objs
}

/// Returns all available objects, pre-sorted in ascending order based on their `order`
Expand Down Expand Up @@ -666,6 +662,44 @@ impl Type {
_ => unreachable!("{typ:#?}"),
}
}

/// True if this is some kind of array/vector.
pub fn is_plural(&self) -> bool {
match self {
Type::Array {
elem_type: _,
length: _,
}
| Type::Vector { elem_type: _ } => true,
Type::UInt8
| Type::UInt16
| Type::UInt32
| Type::UInt64
| Type::Int8
| Type::Int16
| Type::Int32
| Type::Int64
| Type::Bool
| Type::Float16
| Type::Float32
| Type::Float64
| Type::String
| Type::Object(_) => false,
}
}

/// `Some(fqname)` if this is an `Object` or an `Array`/`Vector` of `Object`s.
pub fn fqname(&self) -> Option<&str> {
match self {
Type::Object(fqname) => Some(fqname.as_str()),
Type::Array {
elem_type,
length: _,
}
| Type::Vector { elem_type } => elem_type.fqname(),
_ => None,
}
}
}

/// The underlying element type for arrays/vectors/maps.
Expand Down Expand Up @@ -725,6 +759,14 @@ impl ElementType {
_ => unreachable!("{inner_type:#?}"),
}
}

/// `Some(fqname)` if this is an `Object`.
pub fn fqname(&self) -> Option<&str> {
match self {
ElementType::Object(fqname) => Some(fqname.as_str()),
_ => None,
}
}
}

// --- Common ---
Expand Down

0 comments on commit 2f90d9f

Please sign in to comment.