Skip to content

Commit b2551ae

Browse files
add syntax_highlight.languages() (#265)
* add `syntax_highlight.languages()` * [autofix.ci] apply automated fixes * add type annotations --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 286aba0 commit b2551ae

File tree

5 files changed

+29
-23
lines changed

5 files changed

+29
-23
lines changed

mitmproxy-highlight/src/lib.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use anyhow::bail;
2-
use std::fmt;
3-
use std::fmt::Formatter;
42
use std::str::FromStr;
53

64
pub mod common;
@@ -31,6 +29,17 @@ impl Language {
3129
)]),
3230
}
3331
}
32+
33+
pub const VALUES: [Self; 4] = [Self::Xml, Self::Yaml, Self::Error, Self::None];
34+
35+
pub fn as_str(&self) -> &'static str {
36+
match self {
37+
Self::Xml => "xml",
38+
Self::Yaml => "yaml",
39+
Self::Error => "error",
40+
Self::None => "none",
41+
}
42+
}
3443
}
3544

3645
impl FromStr for Language {
@@ -47,21 +56,6 @@ impl FromStr for Language {
4756
}
4857
}
4958

50-
impl fmt::Display for Language {
51-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
52-
write!(
53-
f,
54-
"{}",
55-
match self {
56-
Language::Xml => "xml",
57-
Language::Yaml => "yaml",
58-
Language::Error => "error",
59-
Language::None => "none",
60-
}
61-
)
62-
}
63-
}
64-
6559
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
6660
pub enum Tag {
6761
Text, // Text that shouldn't be emphasized.
@@ -84,7 +78,7 @@ impl Tag {
8478
Self::Error,
8579
];
8680

87-
pub fn to_str(self) -> &'static str {
81+
pub fn as_str(&self) -> &'static str {
8882
match self {
8983
Tag::Text => "",
9084
Tag::Name => "name",

mitmproxy-rs/mitmproxy_rs/syntax_highlight.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ from typing import Literal
66
def highlight(text: str, language: Literal["xml", "yaml", "error", "none"]) -> list[tuple[str, str]]:
77
pass
88

9+
def languages() -> list[str]:
10+
pass
11+
912
def tags() -> list[str]:
1013
pass
1114

1215
__all__ = [
1316
"highlight",
17+
"languages",
1418
"tags",
1519
]

mitmproxy-rs/src/contentviews.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ impl Contentview {
117117

118118
/// Optional syntax highlighting that should be applied to the prettified output.
119119
#[getter]
120-
pub fn syntax_highlight(&self) -> String {
121-
self.0.syntax_highlight().to_string()
120+
pub fn syntax_highlight(&self) -> &'static str {
121+
self.0.syntax_highlight().as_str()
122122
}
123123

124124
fn __lt__(&self, py: Python<'_>, other: PyObject) -> PyResult<bool> {

mitmproxy-rs/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ mod mitmproxy_rs {
136136
#[pymodule_export]
137137
use crate::syntax_highlight::highlight;
138138
#[pymodule_export]
139+
use crate::syntax_highlight::languages;
140+
#[pymodule_export]
139141
use crate::syntax_highlight::tags;
140142
}
141143
}

mitmproxy-rs/src/syntax_highlight.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,24 @@ pub fn highlight(text: String, language: &str) -> PyResult<Vec<(&'static str, St
2222
.map(|chunks| {
2323
chunks
2424
.into_iter()
25-
.map(|(tag, text)| (tag.to_str(), text))
25+
.map(|(tag, text)| (tag.as_str(), text))
2626
.collect()
2727
})
2828
.map_err(|e| PyValueError::new_err(format!("{:?}", e)))
2929
}
3030

31-
/// Return the list of all possible tag names for a given language.
31+
/// Return the list of all possible syntax highlight tags.
3232
#[pyfunction]
3333
pub fn tags() -> PyResult<Vec<&'static str>> {
3434
Ok(Tag::VALUES
3535
.iter()
36-
.map(|tag| tag.to_str())
36+
.map(|tag| tag.as_str())
3737
.filter(|&x| !x.is_empty())
3838
.collect())
3939
}
40+
41+
/// Return the list of all supported languages for syntax highlighting.
42+
#[pyfunction]
43+
pub fn languages() -> PyResult<Vec<&'static str>> {
44+
Ok(Language::VALUES.iter().map(|lang| lang.as_str()).collect())
45+
}

0 commit comments

Comments
 (0)