Crate weldr

Source
Expand description

§weldr

weldr is a Rust library to manipulate LDraw files (format specification), which are files describing 3D models of LEGO®* pieces.

weldr allows building command-line tools and applications leveraging the fantastic database of pieces contributed by the LDraw community.

§Example

Parse a single LDraw file containing 2 commands:

  • A comment : “this is a comment”
  • A segment command to draw a segment between 2 vertices
extern crate weldr;

use weldr::{parse_raw, Command, CommentCmd, LineCmd, Vec3};

fn main() {}

#[test]
fn parse_ldr() {
  let ldr = b"0 this is a comment\n2 16 0 0 0 1 1 1";
  let cmds = parse_raw(ldr);
  let cmd0 = Command::Comment(CommentCmd::new("this is a comment"));
  let cmd1 = Command::Line(LineCmd{
    color: 16,
    vertices: [
      Vec3{ x: 0.0, y: 0.0, z: 0.0 },
      Vec3{ x: 1.0, y: 1.0, z: 1.0 }
    ]
  });
  assert_eq!(cmds, vec![cmd0, cmd1]);
}

A slightly more involved but more powerful approach is to load and resolve a file and all its sub-file references recursively using the parse() function. This requires implementing the FileRefResolver trait to load file content by reference filename.

The code is available on GitHub.

§Technical features

weldr leverages the nom parser combinator library to efficiently and reliably parse LDraw files, and transform them into in-memory data structures for consumption. All parsing is done on &[u8] input expected to contain specification-compliant LDraw content. In particular, this means:

  • UTF-8 encoded input
  • Both DOS/Windows <CR><LF> and Unix <LF> line termination accepted

§Copyrights

The current code repository is licensed under the MIT license.

LDraw™ is a trademark owned and licensed by the Estate of James Jessiman, which does not sponsor, endorse, or authorize this project.

*LEGO® is a registered trademark of the LEGO Group, which does not sponsor, endorse, or authorize this project.

Re-exports§

pub use error::Error;
pub use error::ParseError;
pub use error::ResolveError;

Modules§

error
Error management

Structs§

CategoryCmd
Line Type 0 META command: !CATEGORY language extension.
Color
RGB color in sRGB color space.
ColourCmd
Line Type 0 META command: !COLOUR language extension.
CommandIterator
Iterator over all drawing commands of a SourceFile and all its referenced sub-files.
CommentCmd
Line Type 0 comment.
DrawContext
Drawing context used when iterating over all drawing commands of a file via SourceFile::iter().
GlitterMaterial
Glitter material definition of a color definition (!COLOUR language extension).
KeywordsCmd
Line Type 0 META command: !KEYWORDS language extension.
LineCmd
Line Type 2 LDraw command: Draw a segment between 2 vertices.
LocalCommandIterator
Iterator over all local commands of a SourceFile.
OptLineCmd
Line Type 5 LDraw command: Draw an optional segment between two vertices, aided by 2 control points.
QuadCmd
Line Type 4 LDraw command: Draw a quad between 4 vertices.
SourceFile
Single LDraw source file loaded and optionally parsed.
SourceFileRef
Reference to a single SourceFile instance in a given SourceMap.
SourceMap
Collection of SourceFile accessible from their reference filename.
SpeckleMaterial
Speckle material definition of a color definition (!COLOUR language extension).
SubFileRefCmd
Line Type 1 LDraw command: Reference a sub-file from the current file.
TriangleCmd
Line Type 3 LDraw command: Draw a triangle between 3 vertices.

Enums§

ColorFinish
Finish for color definitions (!COLOUR language extension).
Command
Types of commands contained in a LDraw file.
GrainSize
Grain size variants for the optional MATERIAL part of color definition (!COLOUR language extension).
MaterialFinish
Finish for optional MATERIAL part of color definition (!COLOUR language extension).
SubFileRef
Reference to a sub-file from inside another file.

Traits§

FileRefResolver
Resolver trait for sub-file references (Line Type 1 LDraw command).

Functions§

parse
Parse a single file and its sub-file references recursively.
parse_raw
Parse raw LDR content without sub-file resolution.

Type Aliases§

Mat4
Vec3
Vec4