diff options
author | vallentin <mail@vallentin.dev> | 2021-03-09 06:38:56 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2021-03-10 13:09:56 +0100 |
commit | 7291494e6a79928cce6ae8844d6ec3092ce51663 (patch) | |
tree | 2535e58286c55000d185807a5e73d863522eab9c /askama_shared/src/generator.rs | |
parent | 96a4328d642191e4925a8f822c9d2cf311f2f9ec (diff) | |
download | askama-7291494e6a79928cce6ae8844d6ec3092ce51663.tar.gz askama-7291494e6a79928cce6ae8844d6ec3092ce51663.tar.bz2 askama-7291494e6a79928cce6ae8844d6ec3092ce51663.zip |
Refactored getting path extension
Diffstat (limited to 'askama_shared/src/generator.rs')
-rw-r--r-- | askama_shared/src/generator.rs | 43 |
1 files changed, 8 insertions, 35 deletions
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index b4363a8..f8e2d8f 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -156,10 +156,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { buf.writeln("}")?; buf.writeln("fn extension(&self) -> Option<&'static str> {")?; - buf.writeln(&format!( - "{:?}", - self.input.path.extension().map(|s| s.to_str().unwrap()) - ))?; + buf.writeln(&format!("{:?}", self.input.extension()))?; buf.writeln("}")?; buf.writeln("fn size_hint(&self) -> usize {")?; @@ -175,10 +172,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { buf.writeln("}")?; buf.writeln("fn extension() -> Option<&'static str> {")?; - buf.writeln(&format!( - "{:?}", - self.input.path.extension().map(|s| s.to_str().unwrap()) - ))?; + buf.writeln(&format!("{:?}", self.input.extension()))?; buf.writeln("}")?; buf.writeln("}")?; @@ -235,10 +229,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { "fn into_response(self, _state: &::askama_gotham::State)\ -> ::askama_gotham::Response<::askama_gotham::Body> {", )?; - let ext = match self.input.path.extension() { - Some(s) => s.to_str().unwrap(), - None => "txt", - }; + let ext = self.input.extension().unwrap_or("txt"); buf.writeln(&format!("::askama_gotham::respond(&self, {:?})", ext))?; buf.writeln("}")?; buf.writeln("}") @@ -256,12 +247,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { "res.body = Some(Box::new(::askama_iron::Template::render(&self).unwrap().into_bytes()));", )?; - let ext = self - .input - .path - .extension() - .map_or("", |s| s.to_str().unwrap_or("")); - match ext { + match self.input.extension().unwrap_or("") { "html" | "htm" => { buf.writeln("::askama_iron::ContentType::html().0.modify(res);")?; } @@ -314,7 +300,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { buf.writeln(&format!( "::mendes::askama::into_response(app, req, &self, {:?})", - self.input.path.extension() + self.input.extension() ))?; buf.writeln("}")?; buf.writeln("}")?; @@ -335,10 +321,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { -> ::askama_rocket::Result<'askama> {", )?; - let ext = match self.input.path.extension() { - Some(s) => s.to_str().unwrap(), - None => "txt", - }; + let ext = self.input.extension().unwrap_or("txt"); buf.writeln(&format!("::askama_rocket::respond(&self, {:?})", ext))?; buf.writeln("}")?; @@ -347,12 +330,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { } fn impl_tide_integrations(&mut self, buf: &mut Buffer) -> Result<(), CompileError> { - let ext = self - .input - .path - .extension() - .and_then(|s| s.to_str()) - .unwrap_or("txt"); + let ext = self.input.extension().unwrap_or("txt"); self.write_header( buf, @@ -375,12 +353,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { fn impl_warp_reply(&mut self, buf: &mut Buffer) -> Result<(), CompileError> { self.write_header(buf, "::askama_warp::warp::reply::Reply", None)?; buf.writeln("fn into_response(self) -> ::askama_warp::warp::reply::Response {")?; - let ext = self - .input - .path - .extension() - .and_then(|s| s.to_str()) - .unwrap_or("txt"); + let ext = self.input.extension().unwrap_or("txt"); buf.writeln(&format!("::askama_warp::reply(&self, {:?})", ext))?; buf.writeln("}")?; buf.writeln("}") |