diff --git a/mitmproxy-highlight/src/lib.rs b/mitmproxy-highlight/src/lib.rs index 70ec6815..0cfa1526 100644 --- a/mitmproxy-highlight/src/lib.rs +++ b/mitmproxy-highlight/src/lib.rs @@ -1,6 +1,4 @@ use anyhow::bail; -use std::fmt; -use std::fmt::Formatter; use std::str::FromStr; pub mod common; @@ -31,6 +29,17 @@ impl Language { )]), } } + + pub const VALUES: [Self; 4] = [Self::Xml, Self::Yaml, Self::Error, Self::None]; + + pub fn as_str(&self) -> &'static str { + match self { + Self::Xml => "xml", + Self::Yaml => "yaml", + Self::Error => "error", + Self::None => "none", + } + } } impl FromStr for Language { @@ -47,21 +56,6 @@ impl FromStr for Language { } } -impl fmt::Display for Language { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!( - f, - "{}", - match self { - Language::Xml => "xml", - Language::Yaml => "yaml", - Language::Error => "error", - Language::None => "none", - } - ) - } -} - #[derive(Debug, Eq, PartialEq, Copy, Clone)] pub enum Tag { Text, // Text that shouldn't be emphasized. @@ -84,7 +78,7 @@ impl Tag { Self::Error, ]; - pub fn to_str(self) -> &'static str { + pub fn as_str(&self) -> &'static str { match self { Tag::Text => "", Tag::Name => "name", diff --git a/mitmproxy-rs/mitmproxy_rs/syntax_highlight.pyi b/mitmproxy-rs/mitmproxy_rs/syntax_highlight.pyi index 61ef248b..222fa648 100644 --- a/mitmproxy-rs/mitmproxy_rs/syntax_highlight.pyi +++ b/mitmproxy-rs/mitmproxy_rs/syntax_highlight.pyi @@ -6,10 +6,14 @@ from typing import Literal def highlight(text: str, language: Literal["xml", "yaml", "error", "none"]) -> list[tuple[str, str]]: pass +def languages() -> list[str]: + pass + def tags() -> list[str]: pass __all__ = [ "highlight", + "languages", "tags", ] diff --git a/mitmproxy-rs/src/contentviews.rs b/mitmproxy-rs/src/contentviews.rs index ae685667..5fbe0c9f 100644 --- a/mitmproxy-rs/src/contentviews.rs +++ b/mitmproxy-rs/src/contentviews.rs @@ -117,8 +117,8 @@ impl Contentview { /// Optional syntax highlighting that should be applied to the prettified output. #[getter] - pub fn syntax_highlight(&self) -> String { - self.0.syntax_highlight().to_string() + pub fn syntax_highlight(&self) -> &'static str { + self.0.syntax_highlight().as_str() } fn __lt__(&self, py: Python<'_>, other: PyObject) -> PyResult { diff --git a/mitmproxy-rs/src/lib.rs b/mitmproxy-rs/src/lib.rs index ccd6c6d5..81ae8444 100644 --- a/mitmproxy-rs/src/lib.rs +++ b/mitmproxy-rs/src/lib.rs @@ -136,6 +136,8 @@ mod mitmproxy_rs { #[pymodule_export] use crate::syntax_highlight::highlight; #[pymodule_export] + use crate::syntax_highlight::languages; + #[pymodule_export] use crate::syntax_highlight::tags; } } diff --git a/mitmproxy-rs/src/syntax_highlight.rs b/mitmproxy-rs/src/syntax_highlight.rs index 5efb241f..9b366cfa 100644 --- a/mitmproxy-rs/src/syntax_highlight.rs +++ b/mitmproxy-rs/src/syntax_highlight.rs @@ -22,18 +22,24 @@ pub fn highlight(text: String, language: &str) -> PyResult PyResult> { Ok(Tag::VALUES .iter() - .map(|tag| tag.to_str()) + .map(|tag| tag.as_str()) .filter(|&x| !x.is_empty()) .collect()) } + +/// Return the list of all supported languages for syntax highlighting. +#[pyfunction] +pub fn languages() -> PyResult> { + Ok(Language::VALUES.iter().map(|lang| lang.as_str()).collect()) +}