@@ -1110,11 +1110,36 @@ pub fn parse_template(template: &TemplateInput) -> BackendResult<TomlDocumentNod
11101110 parse_template_with_profile ( template, TomlProfile :: default ( ) )
11111111}
11121112
1113+ pub fn parse_validated_template_with_profile (
1114+ template : & TemplateInput ,
1115+ profile : TomlProfile ,
1116+ ) -> BackendResult < TomlDocumentNode > {
1117+ // TOML does not add format-specific post-parse validation yet. Keep the
1118+ // validated entry point aligned with the other backends so callers can rely
1119+ // on one API shape as backend-specific validation rules are introduced.
1120+ parse_template_with_profile ( template, profile)
1121+ }
1122+
1123+ pub fn parse_validated_template ( template : & TemplateInput ) -> BackendResult < TomlDocumentNode > {
1124+ parse_validated_template_with_profile ( template, TomlProfile :: default ( ) )
1125+ }
1126+
1127+ pub fn validate_template_with_profile (
1128+ template : & TemplateInput ,
1129+ profile : TomlProfile ,
1130+ ) -> BackendResult < ( ) > {
1131+ parse_validated_template_with_profile ( template, profile) . map ( |_| ( ) )
1132+ }
1133+
1134+ pub fn validate_template ( template : & TemplateInput ) -> BackendResult < ( ) > {
1135+ validate_template_with_profile ( template, TomlProfile :: default ( ) )
1136+ }
1137+
11131138pub fn check_template_with_profile (
11141139 template : & TemplateInput ,
11151140 profile : TomlProfile ,
11161141) -> BackendResult < ( ) > {
1117- parse_template_with_profile ( template, profile) . map ( |_| ( ) )
1142+ validate_template_with_profile ( template, profile)
11181143}
11191144
11201145pub fn check_template ( template : & TemplateInput ) -> BackendResult < ( ) > {
@@ -1125,7 +1150,7 @@ pub fn format_template_with_profile(
11251150 template : & TemplateInput ,
11261151 profile : TomlProfile ,
11271152) -> BackendResult < String > {
1128- let document = parse_template_with_profile ( template, profile) ?;
1153+ let document = parse_validated_template_with_profile ( template, profile) ?;
11291154 format_toml_document ( template, & document)
11301155}
11311156
@@ -1390,7 +1415,7 @@ fn normalize_time(value: toml::value::Time) -> NormalizedTime {
13901415
13911416#[ cfg( test) ]
13921417mod tests {
1393- use super :: { parse_template , TomlKeySegmentValue , TomlStatementNode , TomlValueNode } ;
1418+ use super :: { TomlKeySegmentValue , TomlStatementNode , TomlValueNode , parse_template } ;
13941419 use pyo3:: prelude:: * ;
13951420 use tstring_pyo3_bindings:: { extract_template, toml:: render_document} ;
13961421 use tstring_syntax:: { BackendError , BackendResult , ErrorKind } ;
@@ -1901,10 +1926,12 @@ mod tests {
19011926 ) ;
19021927 let special_floats = table[ "special_float_array" ] . as_array ( ) . expect ( "array" ) ;
19031928 assert ! ( special_floats[ 0 ] . as_float( ) . expect( "float" ) . is_infinite( ) ) ;
1904- assert ! ( special_floats[ 1 ]
1905- . as_float( )
1906- . expect( "float" )
1907- . is_sign_negative( ) ) ;
1929+ assert ! (
1930+ special_floats[ 1 ]
1931+ . as_float( )
1932+ . expect( "float" )
1933+ . is_sign_negative( )
1934+ ) ;
19081935 assert ! ( special_floats[ 2 ] . as_float( ) . expect( "float" ) . is_nan( ) ) ;
19091936 assert_eq ! (
19101937 table[ "special_float_nested_arrays" ]
@@ -1916,33 +1943,43 @@ mod tests {
19161943 let special_float_deeper_arrays = table[ "special_float_deeper_arrays" ]
19171944 . as_array ( )
19181945 . expect ( "array" ) ;
1919- assert ! ( special_float_deeper_arrays[ 0 ] [ 0 ] [ 0 ]
1920- . as_float( )
1921- . expect( "float" )
1922- . is_infinite( ) ) ;
1923- assert ! ( special_float_deeper_arrays[ 1 ] [ 0 ] [ 0 ]
1924- . as_float( )
1925- . expect( "float" )
1926- . is_sign_negative( ) ) ;
1927- assert ! ( special_float_deeper_arrays[ 2 ] [ 0 ] [ 0 ]
1928- . as_float( )
1929- . expect( "float" )
1930- . is_nan( ) ) ;
1946+ assert ! (
1947+ special_float_deeper_arrays[ 0 ] [ 0 ] [ 0 ]
1948+ . as_float( )
1949+ . expect( "float" )
1950+ . is_infinite( )
1951+ ) ;
1952+ assert ! (
1953+ special_float_deeper_arrays[ 1 ] [ 0 ] [ 0 ]
1954+ . as_float( )
1955+ . expect( "float" )
1956+ . is_sign_negative( )
1957+ ) ;
1958+ assert ! (
1959+ special_float_deeper_arrays[ 2 ] [ 0 ] [ 0 ]
1960+ . as_float( )
1961+ . expect( "float" )
1962+ . is_nan( )
1963+ ) ;
19311964 assert_eq ! (
19321965 table[ "upper_exp_nested_mixed" ]
19331966 . as_array( )
19341967 . expect( "array" )
19351968 . len( ) ,
19361969 2
19371970 ) ;
1938- assert ! ( table[ "special_float_inline_table" ] [ "pos" ]
1939- . as_float( )
1940- . expect( "float" )
1941- . is_infinite( ) ) ;
1942- assert ! ( table[ "special_float_inline_table" ] [ "nan" ]
1943- . as_float( )
1944- . expect( "float" )
1945- . is_nan( ) ) ;
1971+ assert ! (
1972+ table[ "special_float_inline_table" ] [ "pos" ]
1973+ . as_float( )
1974+ . expect( "float" )
1975+ . is_infinite( )
1976+ ) ;
1977+ assert ! (
1978+ table[ "special_float_inline_table" ] [ "nan" ]
1979+ . as_float( )
1980+ . expect( "float" )
1981+ . is_nan( )
1982+ ) ;
19461983 assert_eq ! (
19471984 table[ "special_float_mixed_nested" ]
19481985 . as_array( )
@@ -2026,9 +2063,10 @@ mod tests {
20262063 Err ( err) => err,
20272064 } ;
20282065 assert_eq ! ( err. kind, ErrorKind :: Parse ) ;
2029- assert ! ( err
2030- . message
2031- . contains( "single-line basic strings cannot contain newlines" ) ) ;
2066+ assert ! (
2067+ err. message
2068+ . contains( "single-line basic strings cannot contain newlines" )
2069+ ) ;
20322070 } ) ;
20332071 }
20342072
@@ -2920,30 +2958,42 @@ mod tests {
29202958 rendered. text,
29212959 "special_float_inline_table = { pos = +inf, neg = -inf, nan = nan }\n special_float_mixed_nested = [[+inf, -inf], [nan]]"
29222960 ) ;
2923- assert ! ( rendered. data[ "special_float_inline_table" ] [ "pos" ]
2924- . as_float( )
2925- . expect( "pos float" )
2926- . is_infinite( ) ) ;
2927- assert ! ( rendered. data[ "special_float_inline_table" ] [ "neg" ]
2928- . as_float( )
2929- . expect( "neg float" )
2930- . is_sign_negative( ) ) ;
2931- assert ! ( rendered. data[ "special_float_inline_table" ] [ "nan" ]
2932- . as_float( )
2933- . expect( "nan float" )
2934- . is_nan( ) ) ;
2935- assert ! ( rendered. data[ "special_float_mixed_nested" ] [ 0 ] [ 0 ]
2936- . as_float( )
2937- . expect( "nested pos" )
2938- . is_infinite( ) ) ;
2939- assert ! ( rendered. data[ "special_float_mixed_nested" ] [ 0 ] [ 1 ]
2940- . as_float( )
2941- . expect( "nested neg" )
2942- . is_sign_negative( ) ) ;
2943- assert ! ( rendered. data[ "special_float_mixed_nested" ] [ 1 ] [ 0 ]
2944- . as_float( )
2945- . expect( "nested nan" )
2946- . is_nan( ) ) ;
2961+ assert ! (
2962+ rendered. data[ "special_float_inline_table" ] [ "pos" ]
2963+ . as_float( )
2964+ . expect( "pos float" )
2965+ . is_infinite( )
2966+ ) ;
2967+ assert ! (
2968+ rendered. data[ "special_float_inline_table" ] [ "neg" ]
2969+ . as_float( )
2970+ . expect( "neg float" )
2971+ . is_sign_negative( )
2972+ ) ;
2973+ assert ! (
2974+ rendered. data[ "special_float_inline_table" ] [ "nan" ]
2975+ . as_float( )
2976+ . expect( "nan float" )
2977+ . is_nan( )
2978+ ) ;
2979+ assert ! (
2980+ rendered. data[ "special_float_mixed_nested" ] [ 0 ] [ 0 ]
2981+ . as_float( )
2982+ . expect( "nested pos" )
2983+ . is_infinite( )
2984+ ) ;
2985+ assert ! (
2986+ rendered. data[ "special_float_mixed_nested" ] [ 0 ] [ 1 ]
2987+ . as_float( )
2988+ . expect( "nested neg" )
2989+ . is_sign_negative( )
2990+ ) ;
2991+ assert ! (
2992+ rendered. data[ "special_float_mixed_nested" ] [ 1 ] [ 0 ]
2993+ . as_float( )
2994+ . expect( "nested nan" )
2995+ . is_nan( )
2996+ ) ;
29472997 } ) ;
29482998 }
29492999
0 commit comments