Class ExpectationToRustSerializer

java.lang.Object
org.mockserver.serialization.code.ExpectationToRustSerializer

public class ExpectationToRustSerializer extends Object
Generates copy-paste-ready Rust expectation code for the MockServer Rust client (mockserver-client crate, module mockserver_client).

Following the same JSON-wrap pattern as the JavaScript/Python generators, the generated code embeds each expectation's existing JSON serialization (the same bytes produced by format=JSON) in a Rust raw string literal and deserializes it with serde_json::from_str::<Expectation>(...), then passes it to client.upsert(&[...]).

Verified against the Rust client source:

  • ClientBuilder::new(host, port).build()?Result<MockServerClient>
  • pub fn upsert(&self, expectations: &[Expectation]) -> Result<Vec<Expectation>>
  • Expectation derives Deserialize (so serde_json::from_str works)

The JSON is embedded in a Rust raw string literal r#"..."#. A raw literal opened with N # hashes is terminated by a double-quote followed by N hashes, so the generator chooses the smallest N such that the JSON never contains " followed by N # characters, guaranteeing the literal cannot be terminated early by expectation content. Example output:

 use mockserver_client::{ClientBuilder, Expectation};

 fn main() -> Result<(), Box<dyn std::error::Error>> {
     let client = ClientBuilder::new("localhost", 1080).build()?;

     client.upsert(&[serde_json::from_str::<Expectation>(r#"{ ... }"#)?])?;

     Ok(())
 }
 
Author:
jamesdbloom
  • Constructor Details

    • ExpectationToRustSerializer

      public ExpectationToRustSerializer(ExpectationSerializer expectationSerializer)
  • Method Details