add template tags struct (#7)

This commit is contained in:
Josh Thomas 2024-12-07 21:37:36 -06:00 committed by GitHub
parent fce343f44d
commit 81199d1699
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 42 additions and 12 deletions

View file

@ -41,11 +41,11 @@ impl Apps {
}
pub fn has_app(&self, name: &str) -> bool {
self.0.iter().any(|app| app.0 == name)
self.apps().iter().any(|app| app.0 == name)
}
pub fn iter(&self) -> impl Iterator<Item = &App> {
self.0.iter()
self.apps().iter()
}
pub fn check_installed(py: &Python, app: &str) -> Result<bool, RunnerError> {

View file

@ -1,7 +1,7 @@
use crate::apps::Apps;
use crate::gis::{check_gis_setup, GISError};
use crate::scripts;
use crate::templates::TemplateTag;
use crate::templates::TemplateTags;
use djls_python::{ImportCheck, Python, RunnerError, ScriptRunner};
use serde::Deserialize;
use std::fmt;
@ -11,13 +11,13 @@ pub struct DjangoProject {
py: Python,
settings_module: String,
installed_apps: Apps,
templatetags: Vec<TemplateTag>,
templatetags: TemplateTags,
}
#[derive(Debug, Deserialize)]
struct DjangoSetup {
installed_apps: Vec<String>,
templatetags: Vec<TemplateTag>,
templatetags: TemplateTags,
}
impl ScriptRunner for DjangoSetup {
@ -29,7 +29,7 @@ impl DjangoProject {
py: Python,
settings_module: String,
installed_apps: Apps,
templatetags: Vec<TemplateTag>,
templatetags: TemplateTags,
) -> Self {
Self {
py,
@ -60,7 +60,7 @@ impl DjangoProject {
py,
settings_module,
installed_apps: Apps::default(),
templatetags: Vec::new(),
templatetags: TemplateTags::default(),
});
}
@ -70,7 +70,7 @@ impl DjangoProject {
py,
settings_module,
Apps::from_strings(setup.installed_apps.to_vec()),
setup.templatetags.to_vec(),
setup.templatetags,
))
}
@ -90,10 +90,7 @@ impl fmt::Display for DjangoProject {
writeln!(f, "Installed Apps:")?;
write!(f, "{}", self.installed_apps)?;
writeln!(f, "Template Tags:")?;
for tag in &self.templatetags {
write!(f, "{}", tag)?;
writeln!(f)?;
}
write!(f, "{}", self.templatetags)?;
Ok(())
}
}

View file

@ -28,3 +28,36 @@ impl fmt::Display for TemplateTag {
Ok(())
}
}
#[derive(Debug, Default, Deserialize)]
pub struct TemplateTags(Vec<TemplateTag>);
impl TemplateTags {
pub fn tags(&self) -> &Vec<TemplateTag> {
&self.0
}
fn iter(&self) -> impl Iterator<Item = &TemplateTag> {
self.tags().iter()
}
pub fn filter_by_prefix<'a>(
&'a self,
prefix: &'a str,
) -> impl Iterator<Item = &'a TemplateTag> {
self.iter().filter(move |tag| tag.name.starts_with(prefix))
}
pub fn get_by_name(&self, name: &str) -> Option<&TemplateTag> {
self.iter().find(|tag| tag.name == name)
}
}
impl fmt::Display for TemplateTags {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for tag in &self.0 {
writeln!(f, " {}", tag)?;
}
Ok(())
}
}