pub struct Translator<C> { /* private fields */ }Expand description
Turns a reply into one the downstream client can read.
Implementations§
Source§impl<C: ToolResultCodec> Translator<C>
impl<C: ToolResultCodec> Translator<C>
Sourcepub fn translate(
&self,
reply: Map<String, Value>,
from: ProtocolVersion,
to: &Leg,
policy: DegradePolicy,
) -> Result<Translated, LowerError>
pub fn translate( &self, reply: Map<String, Value>, from: ProtocolVersion, to: &Leg, policy: DegradePolicy, ) -> Result<Translated, LowerError>
Translates reply — which arrived in from’s encoding — into something
the to leg negotiated for.
Returns the reply untouched with an empty report whenever there is nothing to decide. That is the common case and it is deliberate: see the module docs for why rebuilding an untranslated reply is a bug rather than a thoroughness.
§Errors
LowerError when the reply needs a downgrade policy does not
authorise, or when the codec cannot write the lowered value at to. The
caller refuses on the error rather than forwarding a result the client
cannot read.
§Examples
A result the client can read is handed back exactly as it arrived.
use protocol_droid::application::bridge::{ToolResultCodec, Translator};
use protocol_droid::domain::ir::{LowerError, ToolResult, TranslationReport};
use protocol_droid::domain::{
CapabilitySet, DegradePolicy, Leg, Negotiation, ProtocolVersion, TransportKind, resolve,
};
use serde_json::{Map, Value, json};
struct ReadsNothing;
impl ToolResultCodec for ReadsNothing {
fn lift(&self, _version: ProtocolVersion, _wire: &Value) -> Option<ToolResult> {
None
}
fn lower(
&self,
_version: ProtocolVersion,
_value: ToolResult,
_policy: DegradePolicy,
_report: &mut TranslationReport,
) -> Result<Value, LowerError> {
Ok(Value::Null)
}
}
let version = ProtocolVersion::V2025_03_26;
let caps = CapabilitySet::all_for(version);
let leg = Leg::new(
version,
resolve(Negotiation {
version,
client_caps: caps,
server_caps: caps,
transport: TransportKind::Stdio,
}),
);
let reply: Map<String, Value> = serde_json::from_value(json!({
"jsonrpc": "2.0",
"id": 1,
"result": { "content": [{ "type": "text", "text": "hi" }] },
}))?;
let translator = Translator::new(ReadsNothing);
let translated =
translator.translate(reply.clone(), version, &leg, DegradePolicy::strict())?;
assert_eq!(translated.reply, reply);
assert!(translated.report.is_empty());Trait Implementations§
Auto Trait Implementations§
impl<C> Freeze for Translator<C>where
C: Freeze,
impl<C> RefUnwindSafe for Translator<C>where
C: RefUnwindSafe,
impl<C> Send for Translator<C>where
C: Send,
impl<C> Sync for Translator<C>where
C: Sync,
impl<C> Unpin for Translator<C>where
C: Unpin,
impl<C> UnsafeUnpin for Translator<C>where
C: UnsafeUnpin,
impl<C> UnwindSafe for Translator<C>where
C: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more