-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from yassun7010/move_export
Move export
- Loading branch information
Showing
17 changed files
with
164 additions
and
133 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
#[cfg(feature = "jsonschema")] | ||
pub mod jsonschema; | ||
|
||
#[cfg(feature = "fluent")] | ||
pub mod fluent; |
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,20 @@ | ||
use unic_langid_impl::LanguageIdentifier; | ||
|
||
type FluentBundle = | ||
serde_valid::export::fluent::FluentBundle<serde_valid::export::fluent::FluentResource>; | ||
|
||
pub trait FluentState { | ||
fn get_fluent_bundle(&self) -> Option<&FluentBundle>; | ||
|
||
fn get_fluent_bundle_on_lang(&self, lang: LanguageIdentifier) -> Option<&FluentBundle>; | ||
} | ||
|
||
impl<T> FluentState for T { | ||
fn get_fluent_bundle(&self) -> Option<&FluentBundle> { | ||
None | ||
} | ||
|
||
fn get_fluent_bundle_on_lang(&self, _lang: LanguageIdentifier) -> Option<&FluentBundle> { | ||
None | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,61 @@ | ||
pub mod context; | ||
use std::{ | ||
any::{type_name, TypeId}, | ||
cell::RefCell, | ||
collections::{HashMap, VecDeque}, | ||
}; | ||
|
||
use jsonschema::{ | ||
output::{BasicOutput, ErrorDescription, OutputUnit}, | ||
JSONSchema, | ||
}; | ||
use schemars::gen::{SchemaGenerator, SchemaSettings}; | ||
use serde_json::{Map, Value}; | ||
|
||
thread_local! { | ||
static CONTEXT: RefCell<SchemaContext> = RefCell::new(SchemaContext::new()); | ||
} | ||
|
||
pub(crate) struct SchemaContext { | ||
pub generator: SchemaGenerator, | ||
pub schemas: HashMap<TypeId, JSONSchema>, | ||
} | ||
|
||
impl SchemaContext { | ||
pub fn new() -> Self { | ||
Self { | ||
generator: SchemaSettings::draft07() | ||
.with(|settings| settings.inline_subschemas = true) | ||
.into_generator(), | ||
schemas: HashMap::default(), | ||
} | ||
} | ||
|
||
pub fn validate<T>(value: &Value) -> Result<(), VecDeque<OutputUnit<ErrorDescription>>> | ||
where | ||
T: crate::validated::Deserialize + schemars::JsonSchema + 'static, | ||
{ | ||
CONTEXT.with(|ctx| { | ||
let ctx = &mut *ctx.borrow_mut(); | ||
let schema = ctx.schemas.entry(TypeId::of::<T>()).or_insert_with(|| { | ||
match jsonschema::JSONSchema::compile( | ||
&serde_json::to_value(ctx.generator.root_schema_for::<T>()).unwrap(), | ||
) { | ||
Ok(s) => s, | ||
Err(error) => { | ||
tracing::error!( | ||
%error, | ||
type_name = type_name::<T>(), | ||
"invalid JSON schema for type" | ||
); | ||
JSONSchema::compile(&Value::Object(Map::default())).unwrap() | ||
} | ||
} | ||
}); | ||
|
||
match schema.apply(value).basic() { | ||
BasicOutput::Valid(_) => Ok(()), | ||
BasicOutput::Invalid(v) => Err(v), | ||
} | ||
}) | ||
} | ||
} |
This file was deleted.
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
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
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,17 @@ | ||
#[cfg(feature = "fluent")] | ||
mod inner { | ||
use crate::fluent::FluentState; | ||
|
||
pub trait State: Send + Sync + FluentState {} | ||
|
||
impl<T> State for T where T: Send + Sync + FluentState {} | ||
} | ||
|
||
#[cfg(not(feature = "fluent"))] | ||
mod inner { | ||
pub trait State: Send + Sync {} | ||
|
||
impl<T> State for T where T: Send + Sync {} | ||
} | ||
|
||
pub use inner::State; |
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
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
Oops, something went wrong.