#parse #parsed #data-file #source #dealing #dzn #mini-zinc

dzn-rs

A library for dealing with DZN files used in the MiniZinc language

2 releases

0.1.1 Nov 22, 2024
0.1.0 Nov 22, 2024

#604 in Algorithms

Download history 231/week @ 2024-11-22 10/week @ 2024-11-29 3/week @ 2024-12-06

244 downloads per month

MIT license

19KB
396 lines

DZN-RS

A Rust library for reading DZN files. The documentation can be found on https://docs.rs/dzn-rs.

The goal of the library is to be able to parse all DZN files correctly. Before that there will not be a 1.0 release. However, the library may be useful for many DZN files without being able to parse every DZN file.

To-Do

  • Primitive Values
    • Boolean
    • Integer
    • Float
  • Sets
    • Boolean
    • Integer
    • Float
  • Arrays (1d, 2d)
    • Boolean
    • Integer
    • Float
    • Set

Installation

To add this library as a dependecy to your project, run

cargo add dzn-rs

Contributing

I am open to receiving pull requests if you identify bugs or want to add features. If you are motivated to do so, please open an issue first.


lib.rs:

A DZN file is represented in the DataFile struct. It can be parsed from a source with the parse function.

Example

let source = r#"
int_param = 5;
bool_param = true;
array_1d = [1, 3, 5];
array_2d = [| true, false
            | false, true
            | false, false |];
"#;

let data_file = dzn_rs::parse::<i32>(source.as_bytes()).expect("valid dzn");

assert_eq!(Some(&5), data_file.get::<i32>("int_param"));
assert_eq!(Some(&true), data_file.get::<bool>("bool_param"));

let array_1d = data_file.array_1d::<i32>("array_1d", 3)
    .expect("key exists with requested length");
assert_eq!(&[3], array_1d.shape());
for (idx, value) in [1, 3, 5].iter().enumerate() {
    assert_eq!(Some(value), array_1d.get([idx]));
}

let array_2d = data_file.array_2d::<bool>("array_2d", [3, 2])
    .expect("key exists with requested shape");
assert_eq!(&[3, 2], array_2d.shape());

dbg!(&array_2d);
assert_eq!(Some(&true), array_2d.get([0, 0]));
assert_eq!(Some(&false), array_2d.get([0, 1]));
assert_eq!(Some(&false), array_2d.get([1, 0]));
assert_eq!(Some(&true), array_2d.get([1, 1]));
assert_eq!(Some(&false), array_2d.get([2, 0]));
assert_eq!(Some(&false), array_2d.get([2, 1]));

Dependencies

~4MB
~73K SLoC