Skip to content

Commit cd6e3b3

Browse files
committed
Add input validation
1 parent 09ab79a commit cd6e3b3

35 files changed

Lines changed: 1117 additions & 126 deletions

src/plot/layer/geom/area.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//! Area geom implementation
22
3-
use crate::plot::layer::orientation::ALIGNED;
4-
use crate::plot::{types::DefaultAestheticValue, DefaultParam, DefaultParamValue};
3+
use crate::plot::layer::orientation::{ALIGNED, ORIENTATION_VALUES};
4+
use crate::plot::types::DefaultAestheticValue;
5+
use crate::plot::{DefaultParam, DefaultParamValue};
56
use crate::{naming, Mappings};
67

8+
use super::types::{ParamConstraint, POSITION_VALUES};
79
use super::{DefaultAesthetics, GeomTrait, GeomType, StatResult};
810

911
/// Area geom - filled area charts
@@ -34,16 +36,19 @@ impl GeomTrait for Area {
3436
}
3537

3638
fn default_params(&self) -> &'static [DefaultParam] {
37-
&[
39+
const PARAMS: &[DefaultParam] = &[
3840
DefaultParam {
3941
name: "position",
4042
default: DefaultParamValue::String("stack"),
43+
constraint: ParamConstraint::string_enum(POSITION_VALUES),
4144
},
4245
DefaultParam {
4346
name: "orientation",
4447
default: DefaultParamValue::String(ALIGNED),
48+
constraint: ParamConstraint::string_enum(ORIENTATION_VALUES),
4549
},
46-
]
50+
];
51+
PARAMS
4752
}
4853

4954
fn needs_stat_transform(&self, _aesthetics: &Mappings) -> bool {

src/plot/layer/geom/arrow.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Arrow geom implementation
22
3-
use super::{DefaultAesthetics, DefaultParam, DefaultParamValue, GeomTrait, GeomType};
3+
use super::types::POSITION_VALUES;
4+
use super::{
5+
DefaultAesthetics, DefaultParam, DefaultParamValue, GeomTrait, GeomType, ParamConstraint,
6+
};
47
use crate::plot::types::DefaultAestheticValue;
58

69
/// Arrow geom - line segments with arrowheads
@@ -29,10 +32,12 @@ impl GeomTrait for Arrow {
2932
}
3033

3134
fn default_params(&self) -> &'static [DefaultParam] {
32-
&[DefaultParam {
35+
const PARAMS: &[DefaultParam] = &[DefaultParam {
3336
name: "position",
3437
default: DefaultParamValue::String("identity"),
35-
}]
38+
constraint: ParamConstraint::string_enum(POSITION_VALUES),
39+
}];
40+
PARAMS
3641
}
3742
}
3843

src/plot/layer/geom/bar.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
use std::collections::HashMap;
44
use std::collections::HashSet;
55

6-
use super::types::get_column_name;
7-
use super::{DefaultAesthetics, DefaultParam, DefaultParamValue, GeomTrait, GeomType, StatResult};
6+
use super::types::{get_column_name, POSITION_VALUES};
7+
use super::{
8+
DefaultAesthetics, DefaultParam, DefaultParamValue, GeomTrait, GeomType, ParamConstraint,
9+
StatResult,
10+
};
811
use crate::naming;
912
use crate::plot::types::{DefaultAestheticValue, ParameterValue};
1013
use crate::reader::SqlDialect;
@@ -54,16 +57,19 @@ impl GeomTrait for Bar {
5457
}
5558

5659
fn default_params(&self) -> &'static [DefaultParam] {
57-
&[
60+
const PARAMS: &[DefaultParam] = &[
5861
DefaultParam {
5962
name: "width",
6063
default: DefaultParamValue::Number(0.9),
64+
constraint: ParamConstraint::number_range(0.0, 1.0),
6165
},
6266
DefaultParam {
6367
name: "position",
6468
default: DefaultParamValue::String("stack"),
69+
constraint: ParamConstraint::string_enum(POSITION_VALUES),
6570
},
66-
]
71+
];
72+
PARAMS
6773
}
6874

6975
fn stat_consumed_aesthetics(&self) -> &'static [&'static str] {

src/plot/layer/geom/boxplot.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
33
use std::collections::HashMap;
44

5+
use super::types::POSITION_VALUES;
56
use super::{DefaultAesthetics, GeomTrait, GeomType};
67
use crate::{
78
naming,
89
plot::{
910
geom::types::get_column_name, DefaultAestheticValue, DefaultParam, DefaultParamValue,
10-
ParameterValue, StatResult,
11+
ParamConstraint, ParameterValue, StatResult,
1112
},
1213
reader::SqlDialect,
1314
DataFrame, GgsqlError, Mappings, Result,
@@ -50,24 +51,29 @@ impl GeomTrait for Boxplot {
5051
}
5152

5253
fn default_params(&self) -> &'static [super::DefaultParam] {
53-
&[
54+
const PARAMS: &[DefaultParam] = &[
5455
DefaultParam {
5556
name: "outliers",
56-
default: super::DefaultParamValue::Boolean(true),
57+
default: DefaultParamValue::Boolean(true),
58+
constraint: ParamConstraint::boolean(),
5759
},
5860
DefaultParam {
5961
name: "coef",
6062
default: DefaultParamValue::Number(1.5),
63+
constraint: ParamConstraint::number_min(0.0),
6164
},
6265
DefaultParam {
6366
name: "width",
6467
default: DefaultParamValue::Number(0.9),
68+
constraint: ParamConstraint::number_range(0.0, 1.0),
6569
},
6670
DefaultParam {
6771
name: "position",
6872
default: DefaultParamValue::String("dodge"),
73+
constraint: ParamConstraint::string_enum(POSITION_VALUES),
6974
},
70-
]
75+
];
76+
PARAMS
7177
}
7278

7379
fn default_remappings(&self) -> &'static [(&'static str, DefaultAestheticValue)] {

src/plot/layer/geom/density.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//! Density geom implementation
22
3+
use super::types::POSITION_VALUES;
34
use super::{DefaultAesthetics, GeomTrait, GeomType};
45
use crate::{
56
naming,
67
plot::{
78
geom::types::get_column_name, DefaultAestheticValue, DefaultParam, DefaultParamValue,
8-
ParameterValue, StatResult,
9+
ParamConstraint, ParameterValue, StatResult,
910
},
1011
reader::SqlDialect,
1112
GgsqlError, Mappings, Result,
@@ -16,6 +17,18 @@ use std::collections::HashMap;
1617
/// Precomputed at compile time to avoid repeated SQRT and PI() calls in SQL
1718
const GAUSSIAN_NORM: f64 = 0.3989422804014327; // 1.0 / (2.0 * std::f64::consts::PI).sqrt()
1819

20+
/// Valid kernel types for density estimation
21+
const KERNEL_VALUES: &[&str] = &[
22+
"gaussian",
23+
"epanechnikov",
24+
"triangular",
25+
"rectangular",
26+
"uniform",
27+
"biweight",
28+
"quartic",
29+
"cosine",
30+
];
31+
1932
/// Density geom - kernel density estimation
2033
#[derive(Debug, Clone, Copy)]
2134
pub struct Density;
@@ -45,24 +58,29 @@ impl GeomTrait for Density {
4558
}
4659

4760
fn default_params(&self) -> &'static [DefaultParam] {
48-
&[
61+
const PARAMS: &[DefaultParam] = &[
4962
DefaultParam {
5063
name: "position",
5164
default: DefaultParamValue::String("identity"),
65+
constraint: ParamConstraint::string_enum(POSITION_VALUES),
5266
},
5367
DefaultParam {
5468
name: "bandwidth",
5569
default: DefaultParamValue::Null,
70+
constraint: ParamConstraint::number_min_exclusive(0.0),
5671
},
5772
DefaultParam {
5873
name: "adjust",
5974
default: DefaultParamValue::Number(1.0),
75+
constraint: ParamConstraint::number_min_exclusive(0.0),
6076
},
6177
DefaultParam {
6278
name: "kernel",
6379
default: DefaultParamValue::String("gaussian"),
80+
constraint: ParamConstraint::string_enum(KERNEL_VALUES),
6481
},
65-
]
82+
];
83+
PARAMS
6684
}
6785

6886
fn default_remappings(&self) -> &'static [(&'static str, DefaultAestheticValue)] {

src/plot/layer/geom/errorbar.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! ErrorBar geom implementation
22
3-
use super::{DefaultAesthetics, DefaultParam, DefaultParamValue, GeomTrait, GeomType};
3+
use super::types::POSITION_VALUES;
4+
use super::{
5+
DefaultAesthetics, DefaultParam, DefaultParamValue, GeomTrait, GeomType, ParamConstraint,
6+
};
47
use crate::plot::types::DefaultAestheticValue;
58

69
/// ErrorBar geom - error bars (confidence intervals)
@@ -30,16 +33,19 @@ impl GeomTrait for ErrorBar {
3033
}
3134

3235
fn default_params(&self) -> &'static [DefaultParam] {
33-
&[
36+
const PARAMS: &[DefaultParam] = &[
3437
DefaultParam {
3538
name: "position",
3639
default: DefaultParamValue::String("identity"),
40+
constraint: ParamConstraint::string_enum(POSITION_VALUES),
3741
},
3842
DefaultParam {
3943
name: "width",
4044
default: DefaultParamValue::Number(10.0),
45+
constraint: ParamConstraint::number_min(0.0),
4146
},
42-
]
47+
];
48+
PARAMS
4349
}
4450
}
4551

src/plot/layer/geom/histogram.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
33
use std::collections::HashMap;
44

5-
use super::types::get_column_name;
6-
use super::{DefaultAesthetics, DefaultParam, DefaultParamValue, GeomTrait, GeomType, StatResult};
5+
use super::types::{get_column_name, CLOSED_VALUES, POSITION_VALUES};
6+
use super::{
7+
DefaultAesthetics, DefaultParam, DefaultParamValue, GeomTrait, GeomType, ParamConstraint,
8+
StatResult,
9+
};
710
use crate::naming;
811
use crate::plot::types::{DefaultAestheticValue, ParameterValue};
912
use crate::reader::SqlDialect;
@@ -49,24 +52,29 @@ impl GeomTrait for Histogram {
4952
}
5053

5154
fn default_params(&self) -> &'static [DefaultParam] {
52-
&[
55+
const PARAMS: &[DefaultParam] = &[
5356
DefaultParam {
5457
name: "bins",
5558
default: DefaultParamValue::Number(30.0),
59+
constraint: ParamConstraint::number_min(1.0),
5660
},
5761
DefaultParam {
5862
name: "closed",
5963
default: DefaultParamValue::String("right"),
64+
constraint: ParamConstraint::string_enum(CLOSED_VALUES),
6065
},
6166
DefaultParam {
6267
name: "binwidth",
6368
default: DefaultParamValue::Null,
69+
constraint: ParamConstraint::number_min_exclusive(0.0),
6470
},
6571
DefaultParam {
6672
name: "position",
6773
default: DefaultParamValue::String("stack"),
74+
constraint: ParamConstraint::string_enum(POSITION_VALUES),
6875
},
69-
]
76+
];
77+
PARAMS
7078
}
7179

7280
fn stat_consumed_aesthetics(&self) -> &'static [&'static str] {

src/plot/layer/geom/label.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Label geom implementation
22
3-
use super::{DefaultAesthetics, DefaultParam, DefaultParamValue, GeomTrait, GeomType};
3+
use super::types::POSITION_VALUES;
4+
use super::{
5+
DefaultAesthetics, DefaultParam, DefaultParamValue, GeomTrait, GeomType, ParamConstraint,
6+
};
47
use crate::plot::types::DefaultAestheticValue;
58

69
/// Label geom - text labels with background
@@ -31,10 +34,12 @@ impl GeomTrait for Label {
3134
}
3235

3336
fn default_params(&self) -> &'static [DefaultParam] {
34-
&[DefaultParam {
37+
const PARAMS: &[DefaultParam] = &[DefaultParam {
3538
name: "position",
3639
default: DefaultParamValue::String("identity"),
37-
}]
40+
constraint: ParamConstraint::string_enum(POSITION_VALUES),
41+
}];
42+
PARAMS
3843
}
3944
}
4045

src/plot/layer/geom/line.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! Line geom implementation
22
3-
use super::{DefaultAesthetics, DefaultParam, DefaultParamValue, GeomTrait, GeomType, StatResult};
4-
use crate::plot::layer::orientation::ALIGNED;
3+
use super::{
4+
DefaultAesthetics, DefaultParam, DefaultParamValue, GeomTrait, GeomType, ParamConstraint,
5+
StatResult,
6+
};
7+
use crate::plot::layer::orientation::{ALIGNED, ORIENTATION_VALUES};
58
use crate::plot::types::DefaultAestheticValue;
69
use crate::{naming, Mappings};
710

@@ -28,10 +31,12 @@ impl GeomTrait for Line {
2831
}
2932

3033
fn default_params(&self) -> &'static [DefaultParam] {
31-
&[DefaultParam {
34+
const PARAMS: &[DefaultParam] = &[DefaultParam {
3235
name: "orientation",
3336
default: DefaultParamValue::String(ALIGNED),
34-
}]
37+
constraint: ParamConstraint::string_enum(ORIENTATION_VALUES),
38+
}];
39+
PARAMS
3540
}
3641

3742
fn needs_stat_transform(&self, _aesthetics: &Mappings) -> bool {

src/plot/layer/geom/linear.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! Linear geom implementation
22
3-
use super::{DefaultAesthetics, DefaultParam, DefaultParamValue, GeomTrait, GeomType};
4-
use crate::plot::layer::orientation::ALIGNED;
3+
use super::{
4+
DefaultAesthetics, DefaultParam, DefaultParamValue, GeomTrait, GeomType, ParamConstraint,
5+
};
6+
use crate::plot::layer::orientation::{ALIGNED, ORIENTATION_VALUES};
57
use crate::plot::types::DefaultAestheticValue;
68

79
/// Linear geom - lines with coefficient and intercept
@@ -27,10 +29,12 @@ impl GeomTrait for Linear {
2729
}
2830

2931
fn default_params(&self) -> &'static [DefaultParam] {
30-
&[DefaultParam {
32+
const PARAMS: &[DefaultParam] = &[DefaultParam {
3133
name: "orientation",
3234
default: DefaultParamValue::String(ALIGNED),
33-
}]
35+
constraint: ParamConstraint::string_enum(ORIENTATION_VALUES),
36+
}];
37+
PARAMS
3438
}
3539
}
3640

0 commit comments

Comments
 (0)