|
1 | 1 | //! # fzn-rs |
2 | 2 | //! |
3 | | -//! `fzn-rs` is a crate that allows for easy parsing of FlatZinc instances in Rust. |
4 | | -//! |
5 | | -//! ## Comparison to other FlatZinc crates |
6 | | -//! There are two well-known crates for parsing FlatZinc files: |
7 | | -//! - [flatzinc](https://docs.rs/flatzinc), for parsing the original `fzn` format, |
8 | | -//! - and [flatzinc-serde](https://docs.rs/flatzinc-serde), for parsing `fzn.json`. |
9 | | -//! |
10 | | -//! The goal of this crate is to be able to parse both the original `fzn` format, as well as the |
11 | | -//! newer `fzn.json` format. Additionally, there is a derive macro that allows for strongly-typed |
12 | | -//! constraints as they are supported by your application. Finally, our aim is to improve the error |
13 | | -//! messages that are encountered when parsing invalid FlatZinc files. |
14 | | -//! |
15 | | -//! ## Typed Instance |
16 | | -//! The main type exposed by the crate is [`TypedInstance`], which is a fully typed representation |
17 | | -//! of a FlatZinc model. |
| 3 | +//! `fzn-rs` is a crate that allows for easy parsing of FlatZinc instances in Rust. It facilitates |
| 4 | +//! type-driven parsing of a FlatZinc file using derive macros. |
18 | 5 | //! |
| 6 | +//! ## Example |
19 | 7 | //! ``` |
20 | | -//! use fzn_rs::TypedInstance; |
21 | | -//! |
22 | | -//! enum Constraints { |
23 | | -//! // ... |
24 | | -//! } |
25 | | -//! |
26 | | -//! type Instance = TypedInstance<i32, Constraints>; |
27 | | -//! ``` |
28 | | -//! |
29 | | -//! ## Derive Macro |
30 | | -//! When parsing a FlatZinc file, the result is an [`ast::Ast`]. That type describes any valid |
31 | | -//! FlatZinc file. However, when consuming FlatZinc, typically you need to process that AST |
32 | | -//! further. For example, to support the [`int_lin_le`][1] constraint, you have to validate that the |
33 | | -//! [`ast::Constraint`] has three arguments, and that each of the arguments has the correct type. |
34 | | -//! |
35 | | -//! When using this crate with the `derive` feature, you can instead do the following: |
36 | | -//! ```rust |
37 | 8 | //! use fzn_rs::ArrayExpr; |
38 | 9 | //! use fzn_rs::FlatZincConstraint; |
| 10 | +//! use fzn_rs::TypedInstance; |
39 | 11 | //! use fzn_rs::VariableExpr; |
40 | 12 | //! |
| 13 | +//! /// The FlatZincConstraint derive macro enables the parsing of a strongly typed constraint |
| 14 | +//! /// based on the FlatZinc Ast. |
41 | 15 | //! #[derive(FlatZincConstraint)] |
42 | 16 | //! pub enum MyConstraints { |
43 | 17 | //! /// The variant name is converted to snake_case to serve as the constraint identifier by |
|
69 | 43 | //! b: VariableExpr<i64>, |
70 | 44 | //! c: VariableExpr<i64>, |
71 | 45 | //! } |
| 46 | +//! |
| 47 | +//! /// The `TypedInstance` is parameterized by the constraint type, as well as any annotations you |
| 48 | +//! /// may need to parse. |
| 49 | +//! type MyInstance = TypedInstance<i64, MyConstraints>; |
| 50 | +//! |
| 51 | +//! fn parse_flatzinc(source: &str) -> MyInstance { |
| 52 | +//! // First, the source string is parsed into a structured representation. |
| 53 | +//! // |
| 54 | +//! // Note: the `fzn_rs::fzn` module is only available with the `fzn` feature enabled. |
| 55 | +//! let ast = fzn_rs::fzn::parse(source).expect("source is valid flatzinc"); |
| 56 | +//! |
| 57 | +//! // Then, the strongly-typed instance is created from the AST |
| 58 | +//! MyInstance::from_ast(ast).expect("type-checking passes") |
| 59 | +//! } |
72 | 60 | //! ``` |
73 | | -//! The macro automatically implements [`FlatZincConstraint`] and will handle the parsing |
74 | | -//! of arguments for you. |
| 61 | +//! |
| 62 | +//! ## Derive Macros |
| 63 | +//! When parsing a FlatZinc file, the result is an [`ast::Ast`]. That type describes any valid |
| 64 | +//! FlatZinc file. However, when consuming FlatZinc, typically you need to process that AST |
| 65 | +//! further. For example, to support the [`int_lin_le`][1] constraint, you have to validate that the |
| 66 | +//! [`ast::Constraint`] has three arguments, and that each of the arguments has the correct type. |
75 | 67 | //! |
76 | 68 | //! Similar to typed constraints, the derive macro for [`FlatZincAnnotation`] allows for easy |
77 | 69 | //! parsing of annotations: |
|
101 | 93 | //! annotation whose name does not match one of the variants in the enum, then the annotation is |
102 | 94 | //! simply ignored. |
103 | 95 | //! |
| 96 | +//! ## Comparison to other FlatZinc crates |
| 97 | +//! There are two well-known crates for parsing FlatZinc files: |
| 98 | +//! - [flatzinc](https://docs.rs/flatzinc), for parsing the original `fzn` format, |
| 99 | +//! - and [flatzinc-serde](https://docs.rs/flatzinc-serde), for parsing `fzn.json`. |
| 100 | +//! |
| 101 | +//! These crates produce what we call the [`ast::Ast`] in this crate, although the concrete types |
| 102 | +//! can be different. `fzn-rs` builds the strong typing of constraints and annotations on-top of |
| 103 | +//! a unified AST for both file formats. Finally, our aim is to improve the error messages that |
| 104 | +//! are encountered when parsing invalid FlatZinc files. |
| 105 | +//! |
104 | 106 | //! [1]: https://docs.minizinc.dev/en/stable/lib-flatzinc-int.html#int-lin-le |
105 | 107 |
|
106 | 108 | mod error; |
|
0 commit comments