@@ -1791,6 +1791,122 @@ impl From<Vec<MessageFromClient>> for MessagesFromClient {
17911791 }
17921792}
17931793
1794+ #[ derive( Debug ) ]
1795+ pub struct StringSchemaFormatError {
1796+ invalid_value : String ,
1797+ }
1798+
1799+ impl core:: fmt:: Display for StringSchemaFormatError {
1800+ fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
1801+ write ! ( f, "Invalid string schema format: '{}'" , self . invalid_value)
1802+ }
1803+ }
1804+
1805+ impl std:: error:: Error for StringSchemaFormatError { }
1806+
1807+ impl FromStr for StringSchemaFormat {
1808+ type Err = StringSchemaFormatError ;
1809+
1810+ fn from_str ( s : & str ) -> std:: result:: Result < Self , Self :: Err > {
1811+ match s {
1812+ "date" => Ok ( Self :: Date ) ,
1813+ "date-time" => Ok ( Self :: DateTime ) ,
1814+ "email" => Ok ( Self :: Email ) ,
1815+ "uri" => Ok ( Self :: Uri ) ,
1816+ _ => Err ( StringSchemaFormatError {
1817+ invalid_value : s. to_string ( ) ,
1818+ } ) ,
1819+ }
1820+ }
1821+ }
1822+
1823+ impl TryFrom < & serde_json:: Map < String , Value > > for PrimitiveSchemaDefinition {
1824+ type Error = RpcError ;
1825+
1826+ fn try_from ( value : & serde_json:: Map < String , serde_json:: Value > ) -> result:: Result < Self , Self :: Error > {
1827+ let input_type = value
1828+ . get ( "type" )
1829+ . and_then ( |v| v. as_str ( ) )
1830+ . or_else ( || value. get ( "oneOf" ) . map ( |_| "enum" ) ) // if "oneOf" exists, return "enum"
1831+ . ok_or_else ( || {
1832+ RpcError :: parse_error ( ) . with_message ( "'type' is missing and data type is not supported!" . to_string ( ) )
1833+ } ) ?;
1834+
1835+ let description = value. get ( "description" ) . and_then ( |v| v. as_str ( ) . map ( |s| s. to_string ( ) ) ) ;
1836+ let title = value. get ( "title" ) . and_then ( |v| v. as_str ( ) . map ( |s| s. to_string ( ) ) ) ;
1837+
1838+ let schema_definition: PrimitiveSchemaDefinition = match input_type {
1839+ "string" => {
1840+ let max_length = value. get ( "maxLength" ) . and_then ( |v| v. as_number ( ) . and_then ( |n| n. as_i64 ( ) ) ) ;
1841+ let min_length = value. get ( "minLength" ) . and_then ( |v| v. as_number ( ) . and_then ( |n| n. as_i64 ( ) ) ) ;
1842+
1843+ let format_str = value. get ( "format" ) . and_then ( |v| v. as_str ( ) ) ;
1844+ let format = format_str. and_then ( |s| StringSchemaFormat :: from_str ( s) . ok ( ) ) ;
1845+
1846+ PrimitiveSchemaDefinition :: StringSchema ( StringSchema :: new (
1847+ description,
1848+ format,
1849+ max_length,
1850+ min_length,
1851+ title,
1852+ ) )
1853+ }
1854+ "number" | "integer" => {
1855+ let maximum = value. get ( "maximum" ) . and_then ( |v| v. as_number ( ) . and_then ( |n| n. as_i64 ( ) ) ) ;
1856+ let minimum = value. get ( "minimum" ) . and_then ( |v| v. as_number ( ) . and_then ( |n| n. as_i64 ( ) ) ) ;
1857+ PrimitiveSchemaDefinition :: NumberSchema ( NumberSchema {
1858+ description,
1859+ maximum,
1860+ minimum,
1861+ title,
1862+ type_ : if input_type == "integer" {
1863+ NumberSchemaType :: Integer
1864+ } else {
1865+ NumberSchemaType :: Number
1866+ } ,
1867+ } )
1868+ }
1869+ "boolean" => {
1870+ let default = value. get ( "default" ) . and_then ( |v| v. as_bool ( ) . map ( |s| s. to_owned ( ) ) ) ;
1871+ PrimitiveSchemaDefinition :: BooleanSchema ( BooleanSchema :: new ( default, description, title) )
1872+ }
1873+
1874+ "enum" => {
1875+ let mut enum_values: :: std:: vec:: Vec < :: std:: string:: String > = vec ! [ ] ;
1876+ let mut enum_names: :: std:: vec:: Vec < :: std:: string:: String > = vec ! [ ] ;
1877+ let values = value. get ( "oneOf" ) . and_then ( |v| v. as_array ( ) ) . ok_or_else ( || {
1878+ RpcError :: parse_error ( )
1879+ . with_message ( "Unsupported enum type, only simple enums are supported!" . to_string ( ) )
1880+ } ) ?;
1881+
1882+ for v in values {
1883+ let title = v. get ( "title" ) . and_then ( |v| v. as_str ( ) . map ( |s| s. to_string ( ) ) ) ;
1884+ let enum_value = v. get ( "enum" ) . and_then ( |v| v. as_array ( ) ) . ok_or_else ( || {
1885+ RpcError :: parse_error ( )
1886+ . with_message ( "Unsupported enum type. only simple enums are supported!" . to_string ( ) )
1887+ } ) ?;
1888+ let enum_value = enum_value
1889+ . first ( )
1890+ . and_then ( |s| s. as_str ( ) . map ( |s| s. to_string ( ) ) )
1891+ . ok_or_else ( || {
1892+ RpcError :: parse_error ( )
1893+ . with_message ( "Unsupported enum value, only simple enums are supported!" . to_string ( ) )
1894+ } ) ?;
1895+
1896+ enum_values. push ( enum_value. to_owned ( ) ) ;
1897+ enum_names. push ( title. unwrap_or ( enum_value) ) ;
1898+ }
1899+ PrimitiveSchemaDefinition :: EnumSchema ( EnumSchema :: new ( enum_values, enum_names, description, title) )
1900+ }
1901+ other => {
1902+ panic ! ( "'{other}' type is not currently supported" ) ;
1903+ }
1904+ } ;
1905+
1906+ Ok ( schema_definition)
1907+ }
1908+ }
1909+
17941910#[ deprecated( since = "0.4.0" , note = "This trait was renamed to RpcMessage. Use RpcMessage instead." ) ]
17951911pub type RPCMessage = ( ) ;
17961912#[ deprecated( since = "0.4.0" , note = "This trait was renamed to McpMessage. Use McpMessage instead." ) ]
0 commit comments