1+ //! Provides interfaces to generate a regular expression based on a given JSON schema.
2+ //!
3+ //! An optional custom pattern could be passed as well to handle whitespace within the regex.
4+ //! If `None`, the default [WHITESPACE] pattern is used.
5+ //!
6+ //! Returns errors if the JSON schema content is invalid or some feature is not yet supported
7+ //! for regex generation.
8+
19use serde_json:: Value ;
210
311mod parsing;
@@ -7,12 +15,78 @@ pub use types::*;
715
816use crate :: Result ;
917
10- pub fn build_regex_from_schema ( json : & str , whitespace_pattern : Option < & str > ) -> Result < String > {
18+ /// Generates a regular expression string from given JSON schema string.
19+ ///
20+ /// # Example
21+ ///
22+ /// ```rust
23+ /// # use outlines_core::Error;
24+ /// use outlines_core::prelude::*;
25+ ///
26+ /// # fn main() -> Result<(), Error> {
27+ /// // Define a JSON schema
28+ /// let schema = r#"{
29+ /// "type": "object",
30+ /// "properties": {
31+ /// "name": { "type": "string" },
32+ /// "age": { "type": "integer" }
33+ /// },
34+ /// "required": ["name", "age"]
35+ /// }"#;
36+ ///
37+ /// // Generate regex from schema
38+ /// let regex = json_schema::regex_from_str(&schema, None)?;
39+ /// println!("Generated regex: {}", regex);
40+ ///
41+ /// // Custom whitespace pattern could be passed as well
42+ /// let whitespace_pattern = Some(r#"[\n ]*"#);
43+ /// let regex = json_schema::regex_from_str(&schema, whitespace_pattern)?;
44+ /// println!("Generated regex with custom whitespace pattern: {}", regex);
45+ ///
46+ /// # Ok(())
47+ /// }
48+ /// ```
49+ pub fn regex_from_str ( json : & str , whitespace_pattern : Option < & str > ) -> Result < String > {
1150 let json_value: Value = serde_json:: from_str ( json) ?;
12- to_regex ( & json_value, whitespace_pattern)
51+ regex_from_value ( & json_value, whitespace_pattern)
1352}
1453
15- pub fn to_regex ( json : & Value , whitespace_pattern : Option < & str > ) -> Result < String > {
54+ /// Generates a regular expression string from `serde_json::Value` type of JSON schema.
55+ ///
56+ /// # Example
57+ ///
58+ /// ```rust
59+ /// # use outlines_core::Error;
60+ /// use serde_json::Value;
61+ /// use outlines_core::prelude::*;
62+ ///
63+ /// # fn main() -> Result<(), Error> {
64+ /// // Define a JSON schema
65+ /// let schema = r#"{
66+ /// "type": "object",
67+ /// "properties": {
68+ /// "name": { "type": "string" },
69+ /// "age": { "type": "integer" }
70+ /// },
71+ /// "required": ["name", "age"]
72+ /// }"#;
73+ ///
74+ /// // If schema's `Value` was already parsed
75+ /// let schema_value: Value = serde_json::from_str(schema)?;
76+ ///
77+ /// // It's possible to generate a regex from schema value
78+ /// let regex = json_schema::regex_from_value(&schema_value, None)?;
79+ /// println!("Generated regex: {}", regex);
80+ ///
81+ /// // Custom whitespace pattern could be passed as well
82+ /// let whitespace_pattern = Some(r#"[\n ]*"#);
83+ /// let regex = json_schema::regex_from_value(&schema_value, whitespace_pattern)?;
84+ /// println!("Generated regex with custom whitespace pattern: {}", regex);
85+ ///
86+ /// # Ok(())
87+ /// }
88+ /// ```
89+ pub fn regex_from_value ( json : & Value , whitespace_pattern : Option < & str > ) -> Result < String > {
1690 let mut parser = parsing:: Parser :: new ( json) ;
1791 if let Some ( pattern) = whitespace_pattern {
1892 parser = parser. with_whitespace_pattern ( pattern)
@@ -1001,7 +1075,7 @@ mod tests {
10011075 ] ,
10021076 ) ,
10031077 ] {
1004- let result = build_regex_from_schema ( schema, None ) . expect ( "To regex failed" ) ;
1078+ let result = regex_from_str ( schema, None ) . expect ( "To regex failed" ) ;
10051079 assert_eq ! ( result, regex, "JSON Schema {} didn't match" , schema) ;
10061080
10071081 let re = Regex :: new ( & result) . expect ( "Regex failed" ) ;
@@ -1057,7 +1131,7 @@ mod tests {
10571131 ] ,
10581132 ) ,
10591133 ] {
1060- let regex = build_regex_from_schema ( schema, None ) . expect ( "To regex failed" ) ;
1134+ let regex = regex_from_str ( schema, None ) . expect ( "To regex failed" ) ;
10611135 let re = Regex :: new ( & regex) . expect ( "Regex failed" ) ;
10621136 for m in a_match {
10631137 should_match ( & re, m) ;
@@ -1110,8 +1184,7 @@ mod tests {
11101184 vec ! [ r#"{SPACE"date"SPACE:SPACE"2018-11-13"SPACE}"# ] ,
11111185 ) ,
11121186 ] {
1113- let regex =
1114- build_regex_from_schema ( schema, whitespace_pattern) . expect ( "To regex failed" ) ;
1187+ let regex = regex_from_str ( schema, whitespace_pattern) . expect ( "To regex failed" ) ;
11151188 assert_eq ! ( regex, expected_regex) ;
11161189
11171190 let re = Regex :: new ( & regex) . expect ( "Regex failed" ) ;
@@ -1135,7 +1208,7 @@ mod tests {
11351208 }
11361209 }"## ;
11371210
1138- let regex = build_regex_from_schema ( schema, None ) ;
1211+ let regex = regex_from_str ( schema, None ) ;
11391212 assert ! ( regex. is_ok( ) , "{:?}" , regex) ;
11401213
11411214 // Confirm the depth of 3 recursion levels by default, recursion level starts
@@ -1268,7 +1341,7 @@ mod tests {
12681341 "$ref": "#/definitions/typeA"
12691342 }"## ;
12701343
1271- let regex = build_regex_from_schema ( schema, None ) ;
1344+ let regex = regex_from_str ( schema, None ) ;
12721345 assert ! ( regex. is_ok( ) , "{:?}" , regex) ;
12731346 }
12741347}
0 commit comments