Skip to main content

opql/
lib.rs

1#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
2#![cfg_attr(test, allow(clippy::needless_pass_by_value))]
3#![cfg_attr(test, allow(clippy::missing_panics_doc))]
4#![allow(clippy::wildcard_imports)]
5#![allow(unused_imports)]
6
7use std::{
8    any::Any,
9    borrow::Borrow,
10    cmp, convert, fmt, io, mem,
11    num::{ParseFloatError, ParseIntError},
12    ops, ptr,
13    rc::Rc,
14    str::FromStr,
15};
16
17use bitflags::bitflags;
18use openpql_macro::*;
19pub use openpql_pql_parser::parse_pql;
20use openpql_pql_parser::{Error as SyntaxError, Spanned, *};
21use openpql_prelude::{CardGen, HandN, ParseError, PlayerIdx};
22use openpql_range_parser::{
23    BoardRangeChecker, Error as RangeError, RangeChecker,
24};
25use runner_output::*;
26
27mod error;
28mod functions;
29mod helper_loc;
30mod output_aggregator;
31mod runner;
32mod runner_output;
33mod types;
34mod vm;
35
36pub use error::*;
37use functions::*;
38use helper_loc::*;
39use output_aggregator::*;
40pub use runner::*;
41#[cfg(test)]
42pub use tests::*;
43pub use types::*;
44use vm::{
45    Vm, VmBinOpCmp, VmCache, VmExecContext, VmProgram, VmSampledData,
46    VmStackValue,
47};
48
49type HeapIdx = usize;
50type FractionInner = i32;
51type RangeSrc = String;
52type FnCheckRange = Box<dyn Fn(&[PQLCard]) -> bool>;
53
54fn parse_cards(text: &str) -> Option<PQLCardSet> {
55    let mut res = PQLCardSet::default();
56    let mut iter = text.chars().filter(|c| !c.is_whitespace());
57
58    while let Some(rank) = iter.next() {
59        let suit = iter.next()?;
60
61        dbg!(suit);
62        res.set(PQLCard::new(
63            PQLRank::from_char(rank)?,
64            PQLSuit::from_char(suit)?,
65        ));
66    }
67
68    Some(res)
69}
70
71#[cfg(test)]
72pub mod tests {
73    pub use std::fmt::Write;
74
75    pub use itertools::Itertools;
76    use openpql_pql_parser::*;
77    pub use openpql_prelude::{CardN, c64, card, cards, r16};
78    pub use quickcheck::{Arbitrary, TestResult};
79    pub use quickcheck_macros::quickcheck;
80    pub use rand::{SeedableRng, prelude::*, rngs};
81
82    pub use super::{
83        PQLBoardRange, PQLGame, PQLHiRating, PQLRange,
84        functions::{PQLFnContext, TestPQLFnContext, rate_hi_hand},
85    };
86    use super::{PQLCard, PQLCardCount, PQLError, PQLErrorKind, PQLRank};
87
88    pub fn count_suits(cs: &[PQLCard]) -> PQLCardCount {
89        cs.iter().map(|c| c.suit).unique().count().to_le_bytes()[0]
90    }
91
92    pub fn get_ranks(cs: &[PQLCard]) -> Vec<PQLRank> {
93        cs.iter().map(|c| c.rank).collect()
94    }
95
96    pub fn mk_ranges(
97        game: PQLGame,
98        player: &[&str],
99        board: &str,
100    ) -> (Vec<PQLRange>, PQLBoardRange) {
101        let player_ranges = player
102            .iter()
103            .map(|&s| (game, s).try_into().unwrap())
104            .collect();
105
106        let board_range = (game, board).try_into().unwrap();
107
108        (player_ranges, board_range)
109    }
110
111    pub fn mk_rating(text: &str) -> PQLHiRating {
112        rate_hi_hand(&PQLFnContext::default(), &text.to_string()).unwrap()
113    }
114}