pub struct StaticDocumentWriter { /* private fields */ }Expand description
Writes complete documents to route-derived index.html files.
Applications enumerate routes and provide page data; this writer owns the
filesystem mapping from each route to its static output path. This
server-side API is omitted from wasm32 builds.
Implementations§
Source§impl StaticDocumentWriter
impl StaticDocumentWriter
Sourcepub fn new(output_directory: impl Into<PathBuf>) -> Self
pub fn new(output_directory: impl Into<PathBuf>) -> Self
Creates a writer rooted at output_directory.
Sourcepub fn from_env() -> Result<Self, ViewError>
pub fn from_env() -> Result<Self, ViewError>
Creates a writer using the output directory supplied by serenade-ssg.
§Errors
Returns an error when the environment variable is missing or empty.
Sourcepub fn output_path(&self, route: &str) -> Result<PathBuf, ViewError>
pub fn output_path(&self, route: &str) -> Result<PathBuf, ViewError>
Resolves a normalized URL route to its static index.html path.
/ maps to <output>/index.html; /about and /about/ map to
<output>/about/index.html. Query strings, fragments, and unsafe path
segments are rejected.
§Errors
Returns an error when route is not a safe absolute URL path.
Sourcepub fn write(
&self,
route: &str,
document: RenderedDocument,
) -> Result<PathBuf, ViewError>
pub fn write( &self, route: &str, document: RenderedDocument, ) -> Result<PathBuf, ViewError>
Writes a rendered document at the static path for route.
Route directories below the output directory are created one level at a
time and must be real directories: a symbolic link there is rejected, so
a document is never written outside the output directory. The document
is written to a uniquely named temporary file that then replaces
index.html (on Unix and Windows), so an existing symbolic link is
replaced instead of written through and concurrent writes of the same
route do not interfere; the last completed write wins.
§Errors
Returns an error when the route is unsafe, a route directory is a symbolic link or not a directory, or the document cannot be written.