Skip to main content

monty_types/
args.rs

1//! [`ToArgs`] / [`ToMontyObject`] — projection of typed args structs into
2//! the `(positional, keyword)` [`MontyObject`] pairs host callbacks consume.
3//! The `#[derive(ToArgs)]` macro in `monty-macros` emits impls of these
4//! traits via `crate::args::…` paths, which resolve in this crate.
5
6use crate::{file_mode::FileMode, object::MontyObject};
7/// Projects a typed args struct into the `(positional, keyword)` `MontyObject`
8/// pair host callbacks expect. Consumes `self` to avoid cloning owned fields.
9///
10/// Inverse of `monty`'s internal `FromArgs` (`ArgValues` → struct); `ToArgs`
11/// is struct → host-facing `(args, kwargs)`. Driven by
12/// [`crate::os::OsFunctionCall::to_args`] for the monty-python / monty-js bindings.
13pub trait ToArgs {
14    fn to_args(self) -> (Vec<MontyObject>, Vec<(MontyObject, MontyObject)>);
15}
16/// Consume `self` into a [`MontyObject`].
17///
18/// `MontyObject` is the host-facing, heap-free representation. Implementers
19/// just shape themselves into the most natural `MontyObject` variant —
20/// `String` → `MontyObject::String`, `Vec<u8>` → `MontyObject::Bytes`, etc.
21pub trait ToMontyObject {
22    fn into_monty_object(self) -> MontyObject;
23}
24
25impl ToMontyObject for MontyObject {
26    fn into_monty_object(self) -> MontyObject {
27        self
28    }
29}
30
31impl ToMontyObject for String {
32    fn into_monty_object(self) -> MontyObject {
33        MontyObject::String(self)
34    }
35}
36
37impl ToMontyObject for Vec<u8> {
38    fn into_monty_object(self) -> MontyObject {
39        MontyObject::Bytes(self)
40    }
41}
42
43impl ToMontyObject for bool {
44    fn into_monty_object(self) -> MontyObject {
45        MontyObject::Bool(self)
46    }
47}
48
49impl ToMontyObject for FileMode {
50    fn into_monty_object(self) -> MontyObject {
51        MontyObject::String(self.as_str().to_owned())
52    }
53}