Skip to content

Commit

Permalink
support no formatted tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane Osbourne committed Jun 4, 2024
1 parent ac9f8be commit 440fa40
Show file tree
Hide file tree
Showing 21 changed files with 385 additions and 52 deletions.
7 changes: 7 additions & 0 deletions crates/bsnext_client/generated/dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ export interface StoppedWatching {
paths: string[];
}

export type StartupEvent =
| { kind: "Started", payload?: undefined }
| { kind: "FailedStartup", payload: StartupErrorDTO };

export type StartupErrorDTO =
| { kind: "InputError", payload: InputErrorDTO };

export type InputErrorDTO =
| { kind: "MissingInputs", payload: string }
| { kind: "InvalidInput", payload: string }
Expand Down
16 changes: 16 additions & 0 deletions crates/bsnext_client/generated/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,29 @@ export const debounceDTOSchema = z.object({
ms: z.string(),
});

export const startupErrorDTOSchema = z.object({
kind: z.literal("InputError"),
payload: inputErrorDTOSchema,
});

export const changeKindSchema = z.nativeEnum(ChangeKind);

export const watchingSchema = z.object({
paths: z.array(z.string()),
debounce: debounceDTOSchema,
});

export const startupEventSchema = z.union([
z.object({
kind: z.literal("Started"),
payload: z.undefined().optional(),
}),
z.object({
kind: z.literal("FailedStartup"),
payload: startupErrorDTOSchema,
}),
]);

export const changeDTOSchema: z.ZodSchema<ChangeDTO> = z.lazy(() =>
z.union([
z.object({
Expand Down
14 changes: 14 additions & 0 deletions crates/bsnext_client/inject/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6428,11 +6428,25 @@ var debounceDTOSchema = z.object({
kind: z.string(),
ms: z.string()
});
var startupErrorDTOSchema = z.object({
kind: z.literal("InputError"),
payload: inputErrorDTOSchema
});
var changeKindSchema = z.nativeEnum(ChangeKind);
var watchingSchema = z.object({
paths: z.array(z.string()),
debounce: debounceDTOSchema
});
var startupEventSchema = z.union([
z.object({
kind: z.literal("Started"),
payload: z.undefined().optional()
}),
z.object({
kind: z.literal("FailedStartup"),
payload: startupErrorDTOSchema
})
]);
var changeDTOSchema = z.lazy(
() => z.union([
z.object({
Expand Down
24 changes: 24 additions & 0 deletions crates/bsnext_dto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::path::Path;

use bsnext_fs::Debounce;
use bsnext_input::route::{DirRoute, ProxyRoute, Route, RouteKind};
use bsnext_input::startup::StartupError;
use typeshare::typeshare;

#[typeshare]
Expand Down Expand Up @@ -101,6 +102,29 @@ pub enum ExternalEvents {
InputError(InputErrorDTO),
}

#[typeshare]
#[derive(Debug, serde::Serialize)]
#[serde(tag = "kind", content = "payload")]
pub enum StartupEvent {
Started,
FailedStartup(StartupErrorDTO),
}

#[typeshare]
#[derive(Debug, PartialEq, Hash, Eq, Clone, serde::Deserialize, serde::Serialize)]
#[serde(tag = "kind", content = "payload")]
pub enum StartupErrorDTO {
InputError(InputErrorDTO),
}

impl From<&StartupError> for StartupErrorDTO {
fn from(value: &StartupError) -> Self {
match value {
StartupError::InputError(e) => StartupErrorDTO::InputError(e.into()),
}
}
}

#[typeshare]
#[derive(Debug, serde::Serialize)]
pub struct InputAccepted {
Expand Down
14 changes: 8 additions & 6 deletions crates/bsnext_input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub mod target;
pub mod watch_opt_test;
pub mod yml;

pub mod startup;

#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
pub struct Input {
pub servers: Vec<server_config::ServerConfig>,
Expand Down Expand Up @@ -79,13 +81,13 @@ pub enum InputError {
MissingExtension(PathBuf),
#[error("Unsupported extension: {0}")]
UnsupportedExtension(String),
#[error("InputWriteError prevented startup {0}")]
#[error("{0}")]
InputWriteError(#[from] InputWriteError),
#[error("Input path error prevented startup {0}")]
#[error("{0}")]
PathError(#[from] PathError),
#[error("Input port error prevented startup {0}")]
#[error("{0}")]
PortError(#[from] PortError),
#[error("Input directory error prevented startup {0}")]
#[error("{0}")]
DirError(#[from] DirError),
#[error("Markdown error: {0}")]
MarkdownError(#[from] MarkdownError),
Expand Down Expand Up @@ -142,8 +144,8 @@ struct PathDefinition {
impl Display for PathDefs {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
for pd in self.0.iter() {
writeln!(f, " cwd: {}", pd.cwd.display())?;
writeln!(f, " input: {}", pd.input)?;
writeln!(f, "cwd: {}", pd.cwd.display())?;
writeln!(f, "input: {}", pd.input)?;
}
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bsnext_input::{Input, InputError};
use crate::{Input, InputError};
use std::env::current_dir;
use std::path::PathBuf;

Expand All @@ -7,6 +7,7 @@ pub type StartupResult = Result<DidStart, StartupError>;
pub struct Startup {
pub tasks: Vec<StartupTask>,
}

#[derive(Debug)]
pub struct StartupContext {
pub cwd: PathBuf,
Expand Down Expand Up @@ -58,7 +59,7 @@ pub enum DidStart {

#[derive(Debug, thiserror::Error)]
pub enum StartupError {
#[error("An input error prevented startup")]
#[error("{0}")]
InputError(#[from] InputError),
}

Expand Down
5 changes: 5 additions & 0 deletions crates/bsnext_output/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ bsnext_core = { path = "../bsnext_core" }
bsnext_dto = { path = "../bsnext_dto" }

ansi_term = { version = "0.12.1" }
indent = { version = "0.1.1" }

anyhow = { workspace = true }
clap = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
insta = { workspace = true }

[dev-dependencies]
strip-ansi-escapes = { version = "0.2.0" }
16 changes: 14 additions & 2 deletions crates/bsnext_output/src/json.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
use crate::OutputWriter;
use bsnext_dto::ExternalEvents;
use bsnext_dto::{ExternalEvents, StartupEvent};
use std::io::Write;

pub struct JsonPrint;

impl OutputWriter for JsonPrint {
fn handle_event<W: Write>(&self, sink: &mut W, evt: &ExternalEvents) -> anyhow::Result<()> {
fn handle_external_event<W: Write>(
&self,
sink: &mut W,
evt: &ExternalEvents,
) -> anyhow::Result<()> {
write!(sink, "{}", serde_json::to_string(&evt)?).map_err(|e| anyhow::anyhow!(e.to_string()))
}

fn handle_startup_event<W: Write>(
&self,
sink: &mut W,
evt: &StartupEvent,
) -> anyhow::Result<()> {
write!(sink, "{}", serde_json::to_string(&evt)?).map_err(|e| anyhow::anyhow!(e.to_string()))
}
}
36 changes: 31 additions & 5 deletions crates/bsnext_output/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
use crate::json::JsonPrint;
use crate::pretty::PrettyPrint;
use bsnext_dto::ExternalEvents;
use bsnext_dto::{ExternalEvents, StartupEvent};
use std::io::Write;

pub mod json;
pub mod pretty;
#[cfg(test)]
mod tests;

pub trait OutputWriter {
fn handle_event<W: Write>(&self, sink: &mut W, evt: &ExternalEvents) -> anyhow::Result<()>;
fn handle_external_event<W: Write>(
&self,
sink: &mut W,
evt: &ExternalEvents,
) -> anyhow::Result<()>;
fn handle_startup_event<W: Write>(
&self,
sink: &mut W,
evt: &StartupEvent,
) -> anyhow::Result<()>;
}

pub enum Writers {
Expand All @@ -16,10 +27,25 @@ pub enum Writers {
}

impl OutputWriter for Writers {
fn handle_event<W: Write>(&self, sink: &mut W, evt: &ExternalEvents) -> anyhow::Result<()> {
fn handle_external_event<W: Write>(
&self,
sink: &mut W,
evt: &ExternalEvents,
) -> anyhow::Result<()> {
match self {
Writers::Pretty => PrettyPrint.handle_event(sink, evt),
Writers::Json => JsonPrint.handle_event(sink, evt),
Writers::Pretty => PrettyPrint.handle_external_event(sink, evt),
Writers::Json => JsonPrint.handle_external_event(sink, evt),
}
}

fn handle_startup_event<W: Write>(
&self,
sink: &mut W,
evt: &StartupEvent,
) -> anyhow::Result<()> {
match self {
Writers::Pretty => PrettyPrint.handle_startup_event(sink, evt),
Writers::Json => JsonPrint.handle_startup_event(sink, evt),
}
}
}
Loading

0 comments on commit 440fa40

Please sign in to comment.