-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: thiserror, tracingクレートの導入 (#8)
- Loading branch information
Showing
5 changed files
with
286 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use crate::err::ClockerError; | ||
|
||
use std::fs::File; | ||
use std::io::BufReader; | ||
use tokio_rustls::rustls::pki_types::{CertificateDer, PrivateKeyDer}; | ||
use tracing::instrument; | ||
use tracing_spanned::SpanErr; | ||
|
||
#[instrument(skip_all, name = "read_cert_file", level = "trace")] | ||
pub fn read_cert_file( | ||
cert_path: String, | ||
) -> Result<Vec<CertificateDer<'static>>, SpanErr<ClockerError>> { | ||
let mut cert_file = File::open(cert_path).map_err(ClockerError::LoadCertFile)?; | ||
|
||
let cert = rustls_pemfile::certs(&mut BufReader::new(&mut cert_file)) | ||
.collect::<Result<Vec<_>, _>>() | ||
.map_err(ClockerError::LoadCertFile)?; | ||
|
||
Ok(cert) | ||
} | ||
|
||
#[instrument(skip_all, name = "read_private_key_file", level = "trace")] | ||
pub fn read_private_key_file( | ||
private_key_path: String, | ||
) -> Result<PrivateKeyDer<'static>, SpanErr<ClockerError>> { | ||
let mut private_key_file = | ||
File::open(private_key_path).map_err(ClockerError::LoadPrivateKeyFile)?; | ||
|
||
let private_key = rustls_pemfile::private_key(&mut BufReader::new(&mut private_key_file)) | ||
.map_err(ClockerError::LoadPrivateKeyFile)? | ||
.ok_or(ClockerError::ReadPrivateKeyPEMSection)?; | ||
|
||
Ok(private_key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use std::{io, string::FromUtf8Error}; | ||
|
||
use thiserror::Error; | ||
use tokio_rustls::rustls; | ||
use tracing_subscriber::util::TryInitError; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum ClockerError { | ||
#[error("initialize tracing subscriber error: {0}")] | ||
InitializeTracingSubscriber(TryInitError), | ||
#[error("create tcp listener error: {0}, port: {1}")] | ||
CreateTCPListener(io::Error, u16), | ||
#[error("failed to accept new connection")] | ||
AcceptNewConnection(io::Error), | ||
#[error("failed to accept new stream")] | ||
AcceptNewStream(io::Error), | ||
#[error("private key pem section not found")] | ||
ReadPrivateKeyPEMSection, | ||
#[error("build tls server error: {0}")] | ||
BuildServer(rustls::Error), | ||
#[error("failed to load cert file: {0}")] | ||
LoadCertFile(io::Error), | ||
#[error("failed to load private key file: {0}")] | ||
LoadPrivateKeyFile(io::Error), | ||
#[error("failed to read buffer: {0}")] | ||
ReadBuffer(io::Error), | ||
#[error("failed to convert buffer to string: {0}")] | ||
ConvertBufferToString(FromUtf8Error), | ||
} |
Oops, something went wrong.