Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions mitmproxy-highlight/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use anyhow::bail;
use std::fmt;
use std::fmt::Formatter;
use std::str::FromStr;

pub mod common;
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand All @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions mitmproxy-rs/mitmproxy_rs/syntax_highlight.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
4 changes: 2 additions & 2 deletions mitmproxy-rs/src/contentviews.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> {
Expand Down
2 changes: 2 additions & 0 deletions mitmproxy-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
12 changes: 9 additions & 3 deletions mitmproxy-rs/src/syntax_highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,24 @@ pub fn highlight(text: String, language: &str) -> PyResult<Vec<(&'static str, St
.map(|chunks| {
chunks
.into_iter()
.map(|(tag, text)| (tag.to_str(), text))
.map(|(tag, text)| (tag.as_str(), text))
.collect()
})
.map_err(|e| PyValueError::new_err(format!("{:?}", e)))
}

/// Return the list of all possible tag names for a given language.
/// Return the list of all possible syntax highlight tags.
#[pyfunction]
pub fn tags() -> PyResult<Vec<&'static str>> {
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<Vec<&'static str>> {
Ok(Language::VALUES.iter().map(|lang| lang.as_str()).collect())
}
Loading