Skip to content

Commit 033c760

Browse files
committed
refactor: move more types into the "types" module
1 parent 83ad3b1 commit 033c760

File tree

6 files changed

+65
-52
lines changed

6 files changed

+65
-52
lines changed

src/cmd/clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct Clean {
1818
pub clean: ConfigOptsClean,
1919
/// Optionally clean any cached tools used by Trunk
2020
///
21-
/// These tools are cached in a platform dependent "projects" dir. Removing them will cause
21+
/// These tools are cached in a platform-dependent "projects" dir. Removing them will cause
2222
/// them to be downloaded by Trunk next time they are needed.
2323
#[arg(short, long)]
2424
pub tools: bool,

src/config/models/mod.rs

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1+
//! The configuration model
2+
//!
3+
//! This is what the user provides, and which gets converted into the runtime model.
4+
15
use crate::config::{RtcBuild, RtcClean, RtcServe, RtcWatch};
26
use anyhow::{Context, Result};
3-
use axum::http::Uri;
4-
use serde::{Deserialize, Deserializer};
5-
use std::fmt::{Display, Formatter};
7+
use serde::Deserialize;
68
use std::path::PathBuf;
7-
use std::str::FromStr;
89
use std::sync::Arc;
910

10-
#[cfg(test)]
11-
mod test;
12-
1311
mod build;
1412
mod clean;
1513
mod core;
@@ -30,17 +28,8 @@ pub use tools::*;
3028
pub use types::*;
3129
pub use watch::*;
3230

33-
/// Deserialize a Uri from a string.
34-
fn deserialize_uri<'de, D, T>(data: D) -> std::result::Result<T, D::Error>
35-
where
36-
D: Deserializer<'de>,
37-
T: From<Uri>,
38-
{
39-
let val = String::deserialize(data)?;
40-
Uri::from_str(val.as_str())
41-
.map(Into::into)
42-
.map_err(|err| serde::de::Error::custom(err.to_string()))
43-
}
31+
#[cfg(test)]
32+
mod test;
4433

4534
/// A model of all potential configuration options for the Trunk CLI system.
4635
#[derive(Clone, Debug, Default, Deserialize)]
@@ -461,36 +450,3 @@ impl ConfigOpts {
461450
greater
462451
}
463452
}
464-
465-
/// Cross origin setting
466-
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
467-
pub enum CrossOrigin {
468-
#[default]
469-
Anonymous,
470-
UseCredentials,
471-
}
472-
473-
impl CrossOrigin {
474-
pub fn from_str(s: &str) -> Result<Self, CrossOriginParseError> {
475-
Ok(match s {
476-
"" | "anonymous" => CrossOrigin::Anonymous,
477-
"use-credentials" => CrossOrigin::UseCredentials,
478-
_ => return Err(CrossOriginParseError::InvalidValue),
479-
})
480-
}
481-
}
482-
483-
impl Display for CrossOrigin {
484-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
485-
match self {
486-
Self::Anonymous => write!(f, "anonymous"),
487-
Self::UseCredentials => write!(f, "use-credentials"),
488-
}
489-
}
490-
}
491-
492-
#[derive(Debug, thiserror::Error)]
493-
pub enum CrossOriginParseError {
494-
#[error("invalid value")]
495-
InvalidValue,
496-
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::fmt::{Display, Formatter};
2+
3+
/// Cross origin setting
4+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
5+
pub enum CrossOrigin {
6+
#[default]
7+
Anonymous,
8+
UseCredentials,
9+
}
10+
11+
impl CrossOrigin {
12+
pub fn from_str(s: &str) -> anyhow::Result<Self, CrossOriginParseError> {
13+
Ok(match s {
14+
"" | "anonymous" => CrossOrigin::Anonymous,
15+
"use-credentials" => CrossOrigin::UseCredentials,
16+
_ => return Err(CrossOriginParseError::InvalidValue),
17+
})
18+
}
19+
}
20+
21+
impl Display for CrossOrigin {
22+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23+
match self {
24+
Self::Anonymous => write!(f, "anonymous"),
25+
Self::UseCredentials => write!(f, "use-credentials"),
26+
}
27+
}
28+
}
29+
30+
#[derive(Debug, thiserror::Error)]
31+
pub enum CrossOriginParseError {
32+
#[error("invalid value")]
33+
InvalidValue,
34+
}

src/config/models/types/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
mod address_family;
22
mod base_url;
3+
mod cross_origin;
34
mod duration;
45
mod minify;
6+
mod serde;
57
mod ws;
68

79
pub use address_family::*;
810
pub use base_url::*;
11+
pub use cross_origin::*;
912
pub use duration::*;
1013
pub use minify::*;
14+
pub use serde::*;
1115
pub use ws::*;

src/config/models/types/serde.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use axum::http::Uri;
2+
use serde::{Deserialize, Deserializer};
3+
use std::str::FromStr;
4+
5+
/// Deserialize a Uri from a string.
6+
pub fn deserialize_uri<'de, D, T>(data: D) -> Result<T, D::Error>
7+
where
8+
D: Deserializer<'de>,
9+
T: From<Uri>,
10+
{
11+
let val = String::deserialize(data)?;
12+
Uri::from_str(val.as_str())
13+
.map(Into::into)
14+
.map_err(|err| serde::de::Error::custom(err.to_string()))
15+
}

src/config/rt/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! The runtime configuration
2+
//!
3+
//! This is what the system actually uses.
4+
15
mod build;
26
mod clean;
37
mod core;

0 commit comments

Comments
 (0)