1+ use anyhow:: { anyhow, Result } ;
2+ use mitmproxy:: contentviews:: { Prettify , Reencode } ;
13use pyo3:: prelude:: * ;
2- use anyhow:: { Result } ;
3- use mitmproxy:: contentviews:: Contentview ;
44
5- #[ pyclass]
6- pub struct PyContentview ( & ' static dyn Contentview ) ;
5+ #[ pyclass( frozen , module = "mitmproxy_rs.contentviews" , subclass ) ]
6+ pub struct Contentview ( & ' static dyn Prettify ) ;
77
8- impl PyContentview {
9- pub fn new < ' py > ( py : Python < ' py > , contentview : & ' static dyn Contentview ) -> PyResult < Bound < ' py , Self > > {
10- PyContentview ( contentview) . into_pyobject ( py)
8+ impl Contentview {
9+ pub fn new < ' py > (
10+ py : Python < ' py > ,
11+ contentview : & ' static dyn Prettify ,
12+ ) -> PyResult < Bound < ' py , Self > > {
13+ Contentview ( contentview) . into_pyobject ( py)
1114 }
1215}
1316
1417#[ pymethods]
15- impl PyContentview {
16-
18+ impl Contentview {
19+ /// The name of this contentview.
1720 #[ getter]
1821 pub fn name ( & self ) -> & str {
1922 self . 0 . name ( )
2023 }
2124
22- pub fn deserialize < ' py > (
23- & self ,
24- data : Vec < u8 >
25- ) -> Result < String > {
26- self . 0 . deserialize ( data)
25+ /// Pretty-print an (encoded) message.
26+ pub fn deserialize < ' py > ( & self , data : Vec < u8 > ) -> Result < String > {
27+ self . 0 . deserialize ( data) . map_err ( |e| anyhow ! ( "{e}" ) )
2728 }
2829
2930 fn __repr__ ( & self ) -> PyResult < String > {
30- Ok ( format ! ( "<{} Contentview>" , self . 0 . name( ) ) )
31+ Ok ( format ! (
32+ "<mitmproxy_rs.contentview.Contentview: {}>" ,
33+ self . 0 . name( )
34+ ) )
35+ }
36+ }
37+
38+ #[ pyclass( frozen, module = "mitmproxy_rs.contentviews" , extends=Contentview ) ]
39+ pub struct InteractiveContentview ( & ' static dyn Reencode ) ;
40+
41+ impl InteractiveContentview {
42+ /// Argument passed twice because of https://github.com/rust-lang/rust/issues/65991
43+ pub fn new < ' py , T : Prettify + Reencode > (
44+ py : Python < ' py > ,
45+ cv : & ' static T ,
46+ ) -> PyResult < Bound < ' py , Self > > {
47+ let cls =
48+ PyClassInitializer :: from ( Contentview ( cv) ) . add_subclass ( InteractiveContentview ( cv) ) ;
49+ Bound :: new ( py, cls)
50+ }
51+ }
52+
53+ #[ pymethods]
54+ impl InteractiveContentview {
55+ pub fn serialize < ' py > ( & self , data : String ) -> Result < Vec < u8 > > {
56+ self . 0 . serialize ( data) . map_err ( |e| anyhow ! ( "{e}" ) )
3157 }
32- }
58+
59+ fn __repr__ ( self_ : PyRef < ' _ , Self > ) -> PyResult < String > {
60+ Ok ( format ! (
61+ "<mitmproxy_rs.contentview.InteractiveContentview: {}>" ,
62+ self_. as_super( ) . name( )
63+ ) )
64+ }
65+ }
0 commit comments