Skip to main content

next_rust_core/
mode.rs

1use serde::{Deserialize, Serialize};
2
3/// How a page is rendered.
4///
5/// | mode      | behaviour                                                        |
6/// |-----------|------------------------------------------------------------------|
7/// | `static`  | rendered at build time (optionally revalidated, see ISR)         |
8/// | `dynamic` | rendered on every request (`server` is accepted as an alias)     |
9/// | `auto`    | the build decides: static unless the route needs request data    |
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
11#[serde(rename_all = "lowercase")]
12pub enum RenderingMode {
13    #[default]
14    Auto,
15    Static,
16    #[serde(alias = "server")]
17    Dynamic,
18}
19
20impl RenderingMode {
21    pub fn as_str(self) -> &'static str {
22        match self {
23            RenderingMode::Auto => "auto",
24            RenderingMode::Static => "static",
25            RenderingMode::Dynamic => "dynamic",
26        }
27    }
28
29    pub fn parse(s: &str) -> Option<Self> {
30        match s.trim().to_ascii_lowercase().as_str() {
31            "auto" => Some(Self::Auto),
32            "static" => Some(Self::Static),
33            "dynamic" | "server" => Some(Self::Dynamic),
34            _ => None,
35        }
36    }
37}
38
39/// Runtime environment.
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
41#[serde(rename_all = "lowercase")]
42pub enum Environment {
43    Development,
44    Production,
45    Test,
46}
47
48impl Environment {
49    /// Read from `NEXT_RUST_ENV` (falls back to `production` for release
50    /// builds and `development` for debug builds).
51    pub fn from_env() -> Self {
52        match std::env::var("NEXT_RUST_ENV").ok().as_deref() {
53            Some("development" | "dev") => Self::Development,
54            Some("production" | "prod") => Self::Production,
55            Some("test") => Self::Test,
56            _ if cfg!(debug_assertions) => Self::Development,
57            _ => Self::Production,
58        }
59    }
60
61    pub fn as_str(self) -> &'static str {
62        match self {
63            Self::Development => "development",
64            Self::Production => "production",
65            Self::Test => "test",
66        }
67    }
68
69    pub fn is_dev(self) -> bool {
70        self == Self::Development
71    }
72}